Example #1
0
 def __init__(self):
     """
     Initializes the manager's state
     Args:
         None
     """
     self.__backgroundProcesses = GenericThreadSafeList()
     self.__thread = BackgroundProcessesPollingThread(self.__backgroundProcesses)
     self.__thread.start()
Example #2
0
 def __init__(self):
     self.__backgroundProcesses = GenericThreadSafeList()
     self.__thread = BackgroundProcessesPollingThread(
         self.__backgroundProcesses)
     self.__thread.start()
Example #3
0
class ChildProcessManager(object):
    """
    These objects create child processes and manage their
    return values.
    """
    def __init__(self):
        """
        Initializes the manager's state
        Args:
            None
        """
        self.__backgroundProcesses = GenericThreadSafeList()
        self.__thread = BackgroundProcessesPollingThread(self.__backgroundProcesses)
        self.__thread.start()
        
    def waitForBackgroundChildrenToTerminate(self):
        """
        Waits for all the child processes running in background to terminate.
        Args:
            None
        Returns:
            Nothing
        """
        self.__thread.stop()
        while not self.__thread.finish() :
            sleep(0.1)
          
    @staticmethod  
    def runCommandInForeground(cmd, ExceptionClass):
        """
        Runs a command in foreground
        Args:
            cmd: a string with the command's name and its arguments
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns:
            The run command's output
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong when running the command.
        """
        p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
        code = p.wait()
        if (code != 0 and ExceptionClass != None) :            
            raise ExceptionClass(p.stdout.read())  
        else :
            return p.stdout.read()
        
    def runCommandInBackground(self, cmd):
        """
        Runs a command in background
            Args:
                cmd: a string with the command's name and its arguments
            Returns:
                the child process' PID
        """
        p = Popen(cmd, shell=False, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
        self.__backgroundProcesses.append(p)
        return p.pid
    
    @staticmethod
    def terminateProcess(pid, ExceptionClass):
        """
        Sends a SIGTERM signal to a process
        Args:
            pid: a process' PID
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns :
            Nothing
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong while killing the process.
        """        
        ChildProcessManager.runCommandInForeground(["kill -s TERM " + str(pid)], ExceptionClass)
        
    @staticmethod
    def runCommandInForegroundAsRoot(cmd, ExceptionClass):
        """
        Runs a command in foreground and as the super-user.
        Args:
            cmd: a string with the command's name and its arguments
            ExceptionClass: the exception that will be raised if something goes wrong
        Returns:
            The run command's output
        Raises:
            ExceptionClass: this exception will be raised if something goes
            wrong when running the command.
        """ 
        password = RootPasswordHandler().getRootsPassword()
        header = "echo " + password + " | sudo -S "
        return ChildProcessManager.runCommandInForeground(header + cmd, ExceptionClass)
 def __init__(self):
     self.__backgroundProcesses = GenericThreadSafeList()
     self.__thread = BackgroundProcessesPollingThread(self.__backgroundProcesses)
     self.__thread.start()