| 21 | import sys |
| 22 | import os |
| 23 | class bash: |
| 24 | def __init__(self, args, timeout=600): |
| 25 | self.args = args |
| 26 | logging.debug("execute:%s"%args) |
| 27 | self.timeout = timeout |
| 28 | self.process = None |
| 29 | self.success = False |
| 30 | self.run() |
| 31 | |
| 32 | def run(self): |
| 33 | class Alarm(Exception): |
| 34 | pass |
| 35 | def alarm_handler(signum, frame): |
| 36 | raise Alarm |
| 37 | |
| 38 | try: |
| 39 | self.process = Popen(self.args, shell=True, stdout=PIPE, stderr=PIPE) |
| 40 | if self.timeout != -1: |
| 41 | signal(SIGALRM, alarm_handler) |
| 42 | alarm(self.timeout) |
| 43 | |
| 44 | try: |
| 45 | self.stdout, self.stderr = self.process.communicate() |
| 46 | if self.timeout != -1: |
| 47 | alarm(0) |
| 48 | except Alarm: |
| 49 | os.kill(self.process.pid, SIGKILL) |
| 50 | raise CloudRuntimeException("Timeout during command execution") |
| 51 | |
| 52 | self.success = self.process.returncode == 0 |
| 53 | except: |
| 54 | raise CloudRuntimeException(formatExceptionInfo()) |
| 55 | |
| 56 | if not self.success: |
| 57 | logging.debug("Failed to execute:" + self.getErrMsg()) |
| 58 | |
| 59 | def isSuccess(self): |
| 60 | return self.success |
| 61 | |
| 62 | def getStdout(self): |
| 63 | return self.stdout.decode('utf-8').strip('\n') |
| 64 | |
| 65 | def getLines(self): |
| 66 | return self.stdout.decode('utf-8').strip('\n').split('\n') |
| 67 | |
| 68 | def getStderr(self): |
| 69 | return self.stderr.decode('utf-8').strip('\n') |
| 70 | |
| 71 | def getErrMsg(self): |
| 72 | if self.isSuccess(): |
| 73 | return "" |
| 74 | |
| 75 | if self.getStderr() is None or self.getStderr() == "": |
| 76 | return self.getStdout() |
| 77 | else: |
| 78 | return self.getStderr() |
| 79 | |
| 80 | def initLoging(logFile=None): |
no outgoing calls
no test coverage detected