Represents a running/ran process
| 12 | |
| 13 | |
| 14 | class Process(object): |
| 15 | ''' Represents a running/ran process ''' |
| 16 | |
| 17 | @staticmethod |
| 18 | def devnull(): |
| 19 | ''' Helper method for opening devnull ''' |
| 20 | return open('/dev/null', 'w') |
| 21 | |
| 22 | @staticmethod |
| 23 | def call(command, cwd=None, shell=False): |
| 24 | ''' |
| 25 | Calls a command (either string or list of args). |
| 26 | Returns tuple: |
| 27 | (stdout, stderr) |
| 28 | ''' |
| 29 | if type(command) is not str or ' ' in command or shell: |
| 30 | shell = True |
| 31 | if Configuration.verbose > 1: |
| 32 | Color.pe('\n {C}[?] {W} Executing (Shell): {B}%s{W}' % command) |
| 33 | else: |
| 34 | shell = False |
| 35 | if Configuration.verbose > 1: |
| 36 | Color.pe('\n {C}[?]{W} Executing: {B}%s{W}' % command) |
| 37 | |
| 38 | pid = Popen(command, cwd=cwd, stdout=PIPE, stderr=PIPE, shell=shell) |
| 39 | pid.wait() |
| 40 | (stdout, stderr) = pid.communicate() |
| 41 | |
| 42 | # Python 3 compatibility |
| 43 | if type(stdout) is bytes: stdout = stdout.decode('utf-8') |
| 44 | if type(stderr) is bytes: stderr = stderr.decode('utf-8') |
| 45 | |
| 46 | |
| 47 | if Configuration.verbose > 1 and stdout is not None and stdout.strip() != '': |
| 48 | Color.pe('{P} [stdout] %s{W}' % '\n [stdout] '.join(stdout.strip().split('\n'))) |
| 49 | if Configuration.verbose > 1 and stderr is not None and stderr.strip() != '': |
| 50 | Color.pe('{P} [stderr] %s{W}' % '\n [stderr] '.join(stderr.strip().split('\n'))) |
| 51 | |
| 52 | return (stdout, stderr) |
| 53 | |
| 54 | @staticmethod |
| 55 | def exists(program): |
| 56 | ''' Checks if program is installed on this system ''' |
| 57 | p = Process(['which', program]) |
| 58 | stdout = p.stdout().strip() |
| 59 | stderr = p.stderr().strip() |
| 60 | |
| 61 | if stdout == '' and stderr == '': |
| 62 | return False |
| 63 | |
| 64 | return True |
| 65 | |
| 66 | def __init__(self, command, devnull=False, stdout=PIPE, stderr=PIPE, cwd=None, bufsize=0, stdin=PIPE): |
| 67 | ''' Starts executing command ''' |
| 68 | |
| 69 | if type(command) is str: |
| 70 | # Commands have to be a list |
| 71 | command = command.split(' ') |
no outgoing calls
no test coverage detected