示例#1
0
class InSubProcess:
	"""
	Important: You need to call `synchronize_with_parent_process()` in your sub-
	class's `main` method.
	"""
	def __init__(self):
		self.sub_process = None
	def __enter__(self):
		self.sub_process = Popen(
			['python', '-m', self.__class__.__module__],
			stdin=PIPE, stdout=PIPE, stderr=STDOUT, universal_newlines=True,
			cwd=os.getcwd(), env=os.environ
		)
		self.sub_process.__enter__()
		self.wait_for_sub_process()
	def wait_for_sub_process(self):
		line = self.sub_process.stdout.readline()
		assert 'Sub process started.\n' == line, \
			'Sub process invocation failed:\n' + \
			line + self.sub_process.stdout.read()
	@classmethod
	def synchronize_with_parent_process(cls):
		# Let parent process know we've started:
		sys.stdout.write('Sub process started.\n')
		sys.stdout.flush()
		# Wait until parent process is finished:
		input('')
	def __exit__(self, *args):
		self.sub_process.stdin.write('\n')
		self.sub_process.stdin.flush()
		self.sub_process.__exit__(*args)
		self.sub_process.wait()
		assert self.sub_process.returncode == 0, \
			repr(self.sub_process.returncode)
示例#2
0
    def run(*popenargs, **kwargs):
        """Run command with arguments and return a CompletedProcess instance.
        The returned instance will have attributes args, returncode, stdout and
        stderr. By default, stdout and stderr are not captured, and those attributes
        will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.
        If check is True and the exit code was non-zero, it raises a
        CalledProcessError. The CalledProcessError object will have the return code
        in the returncode attribute, and output & stderr attributes if those streams
        were captured.
        If timeout is given, and the process takes too long, a TimeoutExpired
        exception will be raised.
        There is an optional argument "input", allowing you to
        pass a string to the subprocess's stdin.  If you use this argument
        you may not also use the Popen constructor's "stdin" argument, as
        it will be used internally.
        The other arguments are the same as for the Popen constructor.
        If universal_newlines=True is passed, the "input" argument must be a
        string and stdout/stderr in the returned object will be strings rather than
        bytes.
        """
        input = kwargs.pop("input", None)
        timeout = kwargs.pop("timeout", None)
        check = kwargs.pop("check", False)
        if input is not None:
            if "stdin" in kwargs:
                raise ValueError(
                    "stdin and input arguments may not both be used.")
            kwargs["stdin"] = PIPE

        process = Popen(*popenargs, **kwargs)
        try:
            process.__enter__()  # No-Op really... illustrate "with in 2.4"
            try:
                stdout, stderr = process.communicate(input, timeout=timeout)
            except TimeoutExpired:
                process.kill()
                stdout, stderr = process.communicate()
                raise TimeoutExpired(process.args,
                                     timeout,
                                     output=stdout,
                                     stderr=stderr)
            except:
                process.kill()
                process.wait()
                raise
            retcode = process.poll()
            if check and retcode:
                raise CalledProcessError(retcode,
                                         process.args,
                                         output=stdout,
                                         stderr=stderr)
        finally:
            # None because our context manager __exit__ does not use them.
            process.__exit__(None, None, None)

        return CompletedProcess(process.args, retcode, stdout, stderr)
示例#3
0
class Caffine:
    """
    Manages requests for disabling sleep.
    """
    def __init__(self):
        self._current_id = 0
        self._requests = []
        self._process: Popen = None

    def _start_process(self):
        file_path = which("caf")

        if not file_path:
            return

        self._process = Popen([file_path], stdin=PIPE)
        self._process.__enter__()

    def _kill_process(self):
        self._process.communicate(" ")
        self._process.kill()
        self._process.__exit__()

    def request(self) -> int:
        """
        Submits a request to prevent sleep.  Returns an identifier for the request.
        """

        current_id = self._current_id
        self._current_id += 1

        if not self._requests:
            self._start_process()

        self._requests.append(self._current_id)

        return current_id

    def release(self, identity: int):
        """
        Takes the ID from a previous requests and removes it from the list of requests.
        If it is the last request, then stop requesting to prevent sleep.
        """
        if identity not in self._requests:
            return

        self._requests.remove(identity)

        if not self._requests:
            self._kill_process()
示例#4
0
    def test_encode_decode(self):
        process = Popen([sys.executable, '../communicate.py'],
                        stdin=PIPE,
                        stdout=PIPE)
        plugins = ['firefox']

        for plugin in plugins:
            if not config.get(plugin + 'Enabled'):
                print('Skipped test for ' + plugin)
                continue

            with self.subTest('Returned message should be correct',
                              plugin=plugin):
                # build call
                call_encoded = json.dumps(plugin).encode('utf-8')
                call_encoded = struct.pack(
                    str(len(call_encoded)) + 's', call_encoded)
                msg_length = struct.pack('=I', len(call_encoded))

                # send call and get response
                process.stdin.write(msg_length)
                process.stdin.write(call_encoded)
                process.stdin.flush()
                process.stdin.close()
                response = process.stdout.readline()
                process.terminate()

                self.assertTrue(response is not None and len(response) > 0,
                                'Response should not be empty')

                # decode response
                response_length = struct.unpack('=I', response[:4])[0]
                response = response[4:]
                response_decoded = response[:response_length].decode('utf-8')
                response_decoded = json.loads(response_decoded)

                # test if correct
                message_expected = communicate.send_config(plugin)
                self.assertDictEqual(
                    message_expected, response_decoded,
                    'Returned message should be equal to the message')

        process.__exit__(None, None, None)
示例#5
0
class PlumbumLocalPopen(PopenAddons):
    iter_lines = iter_lines

    def __init__(self, *args, **kwargs):
        self._proc = Popen(*args, **kwargs)

    def __iter__(self):
        return self.iter_lines()

    def __enter__(self):
        return self._proc.__enter__()

    def __exit__(self, *args, **kwargs):
        return self._proc.__exit__(*args, **kwargs)

    def __getattr__(self, name):
        return getattr(self._proc, name)
示例#6
0
class PopenWrapperClass(object):
    """ wrapper around subprocess.Popen """
    def __init__(self, command):
        """ init """
        self.command = command
        self.pop_ = Popen(self.command, shell=True, stdout=PIPE,
                          close_fds=True)

    def __enter__(self):
        """ enter """
        return self.pop_

    def __exit__(self, exc_type, exc_value, traceback):
        """ exit """
        if hasattr(self.pop_, '__exit__'):
            return self.pop_.__exit__(exc_type, exc_value, traceback)
        self.pop_.wait()
        if exc_type or exc_value or traceback:
            return False
        else:
            return True
示例#7
0
 def __kill_Popen(self, process: Popen):
     process.kill()
     process.__exit__(None, None, None)