|
|
@@ -1,48 +1,48 @@
|
|
|
import subprocess, logging
|
|
|
|
|
|
class Proc:
|
|
|
- encoding = 'utf-8'
|
|
|
-
|
|
|
- def __init__(self, stdout=subprocess.PIPE, stderr=subprocess.PIPE, logLevel=logging.WARN):
|
|
|
- self.logger = logging.getLogger(__name__)
|
|
|
- self.logger.setLevel(logLevel)
|
|
|
-
|
|
|
- self.stdout = stdout
|
|
|
- self.stderr = stderr
|
|
|
-
|
|
|
- def run(self, cmd):
|
|
|
- # Return dict
|
|
|
- ret = {'out': None, 'err': None, 'code': int(-111) }
|
|
|
-
|
|
|
- # Sanity
|
|
|
- if not cmd or len(cmd) < 1:
|
|
|
- self.logger.error(f"Empty cmd")
|
|
|
- return ret
|
|
|
-
|
|
|
- # Exec
|
|
|
- self.logger.debug(f"Cmd: {cmd}")
|
|
|
- proc = subprocess.run(cmd, stdout=self.stdout, stderr=self.stderr)
|
|
|
- self.logger.debug(f"Result: {proc}")
|
|
|
-
|
|
|
- # Exit code
|
|
|
- try:
|
|
|
- ret['code'] = proc.returncode
|
|
|
- except:
|
|
|
- pass
|
|
|
-
|
|
|
- # Decode outputs
|
|
|
- if hasattr(proc, 'stdout') and len(proc.stdout) > 0:
|
|
|
- try:
|
|
|
- ret['out'] = proc.stdout.decode(self.encoding)
|
|
|
- except:
|
|
|
- pass
|
|
|
- if hasattr(proc, 'stderr') and len(proc.stderr) > 0:
|
|
|
- try:
|
|
|
- ret['err'] = proc.stderr.decode(self.encoding)
|
|
|
- except:
|
|
|
- pass
|
|
|
-
|
|
|
- # Return
|
|
|
- if ret['code'] != 0:
|
|
|
- self.logger.error(f"Error {cmd[0]}({ret['code']}): {ret['err']}")
|
|
|
- return ret
|
|
|
+ encoding = 'utf-8'
|
|
|
+
|
|
|
+ def __init__(self, stdout=subprocess.PIPE, stderr=subprocess.PIPE, logLevel=logging.WARN):
|
|
|
+ self.logger = logging.getLogger(__name__)
|
|
|
+ self.logger.setLevel(logLevel)
|
|
|
+
|
|
|
+ self.stdout = stdout
|
|
|
+ self.stderr = stderr
|
|
|
+
|
|
|
+ def run(self, cmd):
|
|
|
+ # Return dict
|
|
|
+ ret = {'out': None, 'err': None, 'code': int(-111) }
|
|
|
+
|
|
|
+ # Sanity
|
|
|
+ if not cmd or len(cmd) < 1:
|
|
|
+ self.logger.error(f"Empty cmd")
|
|
|
+ return ret
|
|
|
+
|
|
|
+ # Exec
|
|
|
+ self.logger.debug(f"Cmd: {cmd}")
|
|
|
+ proc = subprocess.run(cmd, stdout=self.stdout, stderr=self.stderr)
|
|
|
+ self.logger.debug(f"Result: {proc}")
|
|
|
+
|
|
|
+ # Exit code
|
|
|
+ try:
|
|
|
+ ret['code'] = proc.returncode
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+
|
|
|
+ # Decode outputs
|
|
|
+ if hasattr(proc, 'stdout') and len(proc.stdout) > 0:
|
|
|
+ try:
|
|
|
+ ret['out'] = proc.stdout.decode(self.encoding)
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+ if hasattr(proc, 'stderr') and len(proc.stderr) > 0:
|
|
|
+ try:
|
|
|
+ ret['err'] = proc.stderr.decode(self.encoding)
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+
|
|
|
+ # Return
|
|
|
+ if ret['code'] != 0:
|
|
|
+ self.logger.error(f"Error {cmd[0]}({ret['code']}): {ret['err']}")
|
|
|
+ return ret
|