Commit 6cd53d1e authored by Thibault VINCENT's avatar Thibault VINCENT
Browse files

add: command execution facilities

parent 8879c752
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-

import os, sys, subprocess
from threading import Lock

def enum(*sequential, **named):
@@ -144,3 +145,37 @@ class RWLock(object):
            if self._parent._readers == 0:
                self._parent._writemutex.release()
            self._parent._mutex.release()


class Exec(object):
    '''
    '''
    @staticmethod
    def call(cmd, shell=False, input=None, capture_output=True,
                                                            merge_output=True):
        '''
        '''
        stdout, stderr = None, None
        if capture_output:
            stdout = subprocess.PIPE
            if merge_output:
                stderr = subprocess.STDOUT
            else:
                stderr = subprocess.PIPE
        else:
            stdout = open(os.devnull, 'w')
            stderr = stdout
        print 'DBG>>> ' + ' '.join(cmd)
        proc = subprocess.Popen(cmd, shell=shell, stdout=stdout, stderr=stderr)
        if input is None:
            output = proc.communicate()
        else:
            output = proc.communicate(input=input)
        return (proc.returncode, output)
    
    @staticmethod
    def silent(cmd, shell=False):
        '''
        '''
        return Exec.call(cmd, shell=shell, capture_output=False)[0]