def run(self): if self._installed is not None: return self._installed with TRACER.timed('Installing %s' % self._install_tmp, V=2): env = self._interpreter.sanitized_environment() mixins = OrderedSet(['setuptools'] + self.mixins) env['PYTHONPATH'] = os.pathsep.join(third_party.expose(mixins)) env['__PEX_UNVENDORED__'] = '1' command = [self._interpreter.binary, '-s', '-'] + self.setup_command() try: Executor.execute(command, env=env, cwd=self._source_dir, stdin_payload=self.setup_py_wrapper.encode('ascii')) self._installed = True except Executor.NonZeroExit as e: self._installed = False name = os.path.basename(self._source_dir) print('**** Failed to install %s (caused by: %r\n):' % (name, e), file=sys.stderr) print('stdout:\n%s\nstderr:\n%s\n' % (e.stdout, e.stderr), file=sys.stderr) return self._installed return self._installed
def test_executor_execute_nonzero(exit_code): # type: (int) -> None with pytest.raises(Executor.NonZeroExit) as exc: Executor.execute("exit %s" % exit_code, shell=True) if exit_code > 0: assert exc.value.exit_code == exit_code
def test_executor_execute_dir(): with temporary_dir() as temp_dir: test_dir = os.path.realpath(os.path.join(temp_dir, 'tmp')) safe_mkdir(test_dir) assert os.path.isdir(test_dir) with pytest.raises(Executor.ExecutionError) as e: Executor.execute(test_dir) assert test_dir in str(e)
def test_executor_execute_stdio(): with temporary_dir() as tmp: with open(os.path.join(tmp, 'stdout'), 'w+b') as fake_stdout: with open(os.path.join(tmp, 'stderr'), 'w+b') as fake_stderr: Executor.execute('/bin/echo -n TEST | tee /dev/stderr', shell=True, stdout=fake_stdout, stderr=fake_stderr) fake_stdout.seek(0) fake_stderr.seek(0) assert fake_stdout.read().decode('utf-8') == 'TEST' assert fake_stderr.read().decode('utf-8') == 'TEST'
def compile(self, root, relpaths): """Compiles the given python source files using this compiler's interpreter. :param string root: The root path all the source files are found under. :param list relpaths: The realtive paths from the `root` of the source files to compile. :returns: A list of relative paths of the compiled bytecode files. :raises: A :class:`Compiler.Error` if there was a problem bytecode compiling any of the files. """ with named_temporary_file() as fp: fp.write( to_bytes(_COMPILER_MAIN % { 'root': root, 'relpaths': relpaths }, encoding='utf-8')) fp.flush() try: out, _ = Executor.execute( [self._interpreter.binary, '-sE', fp.name]) except Executor.NonZeroExit as e: raise self.CompilationFailure( 'encountered %r during bytecode compilation.\nstderr was:\n%s\n' % (e, e.stderr)) return out.splitlines()
def _from_binary_external(cls, binary): environ = cls.sanitized_environment() stdout, _ = Executor.execute([binary, '-sE'], env=environ, stdin_payload=_generate_identity_source()) identity = stdout.strip() if not identity: raise cls.IdentificationError('Could not establish identity of %s' % binary) return cls(binary, PythonIdentity.from_id_string(identity))
def run(self): if self._installed is not None: return self._installed with TRACER.timed('Installing %s' % self._install_tmp, V=2): command = [self._interpreter.binary, '-sE', '-'] + self._setup_command() try: Executor.execute(command, env=self._interpreter.sanitized_environment(), cwd=self._source_dir, stdin_payload=self.bootstrap_script.encode('ascii')) self._installed = True except Executor.NonZeroExit as e: self._installed = False name = os.path.basename(self._source_dir) print('**** Failed to install %s (caused by: %r\n):' % (name, e), file=sys.stderr) print('stdout:\n%s\nstderr:\n%s\n' % (e.stdout, e.stderr), file=sys.stderr) return self._installed return self._installed
def test_executor_execute(): assert Executor.execute('/bin/echo -n stdout >&1', shell=True) == ('stdout', '') assert Executor.execute('/bin/echo -n stderr >&2', shell=True) == ('', 'stderr') assert Executor.execute('/bin/echo -n TEST | tee /dev/stderr', shell=True) == ('TEST', 'TEST') assert Executor.execute(['/bin/echo', 'hello']) == ('hello\n', '') assert Executor.execute(['/bin/echo', '-n', 'hello']) == ('hello', '') assert Executor.execute('/bin/echo -n $HELLO', env={'HELLO': 'hey'}, shell=True) == ('hey', '')
def test_executor_execute(): # type: () -> None assert Executor.execute("/bin/echo -n stdout >&1", shell=True) == ("stdout", "") assert Executor.execute("/bin/echo -n stderr >&2", shell=True) == ("", "stderr") assert Executor.execute("/bin/echo -n TEST | tee /dev/stderr", shell=True) == ("TEST", "TEST") assert Executor.execute(["/bin/echo", "hello"]) == ("hello\n", "") assert Executor.execute(["/bin/echo", "-n", "hello"]) == ("hello", "") assert Executor.execute("/bin/echo -n $HELLO", env={"HELLO": "hey"}, shell=True) == ("hey", "")
def _execute(cls, binary, args=None, pythonpath=None, env=None, stdin_payload=None, **kwargs): cmd, env = cls._create_isolated_cmd(binary, args=args, pythonpath=pythonpath, env=env) stdout, stderr = Executor.execute(cmd, stdin_payload=stdin_payload, env=env, **kwargs) return cmd, stdout, stderr
def execute( self, args=None, # type: Optional[Iterable[str]] stdin_payload=None, # type: Optional[AnyStr] pythonpath=None, # type: Optional[Iterable[str]] env=None, # type: Optional[Mapping[str, str]] **kwargs # type: Any ): # type: (...) -> Tuple[Iterable[str], str, str] cmd, env = self.create_isolated_cmd(args=args, pythonpath=pythonpath, env=env) stdout, stderr = Executor.execute(cmd, stdin_payload=stdin_payload, env=env, **kwargs) return cmd, stdout, stderr
def compile(self, root, relpaths): """Compiles the given python source files using this compiler's interpreter. :param string root: The root path all the source files are found under. :param list relpaths: The realtive paths from the `root` of the source files to compile. :returns: A list of relative paths of the compiled bytecode files. :raises: A :class:`Compiler.Error` if there was a problem bytecode compiling any of the files. """ with named_temporary_file() as fp: fp.write(to_bytes(_COMPILER_MAIN % {'root': root, 'relpaths': relpaths}, encoding='utf-8')) fp.flush() try: out, _ = Executor.execute([self._interpreter.binary, '-sE', fp.name]) except Executor.NonZeroExit as e: raise self.CompilationFailure( 'encountered %r during bytecode compilation.\nstderr was:\n%s\n' % (e, e.stderr) ) return out.splitlines()
def test_executor_execute_zero(): # type: () -> None Executor.execute("exit 0", shell=True)
def test_executor_execute_nonzero(exit_code): with pytest.raises(Executor.NonZeroExit) as exc: Executor.execute('exit %s' % exit_code, shell=True) if exit_code > 0: assert exc.value.exit_code == exit_code
def test_executor_execute_zero(): Executor.execute('exit 0', shell=True)
def test_executor_execute_zero(): Executor.execute("exit 0", shell=True)