def test_log_tee(): with temporary_dir() as td: taskpath = make_taskpath(td) sandbox = setup_sandbox(td, taskpath) # Create file stdout for capturing output. We can't use StringIO mock # because TestProcess is running fork. with open(os.path.join(td, 'sys_stdout'), 'w+') as stdout: with open(os.path.join(td, 'sys_stderr'), 'w+') as stderr: with mutable_sys(): sys.stdout, sys.stderr = stdout, stderr p = TestProcess('process', 'echo hello world; echo >&2 hello stderr', 0, taskpath, sandbox, logger_destination=LoggerDestination.BOTH) p.start() rc = wait_for_rc(taskpath.getpath('process_checkpoint')) assert rc == 0 # Check log files were created in std path with correct content assert_log_content(taskpath, 'stdout', 'hello world\n') assert_log_content(taskpath, 'stderr', 'hello stderr\n') # Check mock stdout stdout.seek(0) assert stdout.read() == 'hello world\n' # Check mock stderr stderr.seek(0) assert stderr.read() == 'hello stderr\n'
def test_log_tee(): with temporary_dir() as td: taskpath = make_taskpath(td) sandbox = setup_sandbox(td, taskpath) # Create file stdout for capturing output. We can't use StringIO mock # because TestProcess is running fork. with open(os.path.join(td, "sys_stdout"), "w+") as stdout: with open(os.path.join(td, "sys_stderr"), "w+") as stderr: with mutable_sys(): sys.stdout, sys.stderr = stdout, stderr p = TestProcess( "process", "echo hello world; echo >&2 hello stderr", 0, taskpath, sandbox, logger_destination=LoggerDestination.BOTH, ) p.start() rc = wait_for_rc(taskpath.getpath("process_checkpoint")) assert rc == 0 # Check log files were created in std path with correct content assert_log_content(taskpath, "stdout", "hello world\n") assert_log_content(taskpath, "stderr", "hello stderr\n") # Check mock stdout stdout.seek(0) assert stdout.read() == "hello world\n" # Check mock stderr stderr.seek(0) assert stderr.read() == "hello stderr\n"
def execute(self, args=()): entry_point = self.entry() with mutable_sys(): sys.path, sys.path_importer_cache = self.minimum_path() self._env.activate() if 'PEX_COVERAGE' in os.environ: PEX.start_coverage() TRACER.log('PYTHONPATH now %s' % ':'.join(sys.path)) force_interpreter = 'PEX_INTERPRETER' in os.environ if entry_point and not force_interpreter: self.execute_entry(entry_point, args) else: os.unsetenv('PEX_INTERPRETER') TRACER.log('%s, dropping into interpreter' % ( 'PEX_INTERPRETER specified' if force_interpreter else 'No entry point specified.')) if sys.argv[1:]: try: with open(sys.argv[1]) as fp: ast = compile(fp.read(), fp.name, 'exec') except IOError as e: print("Could not open %s in the environment [%s]: %s" % (sys.argv[1], sys.argv[0], e)) sys.exit(1) sys.argv = sys.argv[1:] old_name = globals()['__name__'] try: globals()['__name__'] = '__main__' Compatibility.exec_function(ast, globals()) finally: globals()['__name__'] = old_name else: import code code.interact()
def test_log_console(): with temporary_dir() as td: taskpath = make_taskpath(td) sandbox = setup_sandbox(td, taskpath) # Create file stdout for capturing output. We can't use StringIO mock # because TestProcess is running fork. with open(os.path.join(td, 'sys_stdout'), 'w+') as stdout: with open(os.path.join(td, 'sys_stderr'), 'w+') as stderr: with mutable_sys(): sys.stdout, sys.stderr = stdout, stderr p = TestProcess('process', 'echo hello world; echo >&2 hello stderr', 0, taskpath, sandbox, logger_destination=LoggerDestination.CONSOLE) p.start() rc = wait_for_rc(taskpath.getpath('process_checkpoint')) assert rc == 0 # Check no log files were created in std path assert_log_dne(taskpath, 'stdout') assert_log_dne(taskpath, 'stderr') # Check mock stdout stdout.seek(0) assert stdout.read() == 'hello world\n' # Check mock stderr stderr.seek(0) assert stderr.read() == 'hello stderr\n'
def execute(self, args=()): entry_point = self.entry() with mutable_sys(): sys.path, sys.path_importer_cache = self.minimum_path() self._env.activate() if 'PEX_COVERAGE' in os.environ: PEX.start_coverage() self.debug('PYTHONPATH now %s' % ':'.join(sys.path)) force_interpreter = 'PEX_INTERPRETER' in os.environ if entry_point and not force_interpreter: self.execute_entry(entry_point, args) else: self.debug('%s, dropping into interpreter' % ('PEX_INTERPRETER specified' if force_interpreter else 'No entry point specified.')) if sys.argv[1:]: try: with open(sys.argv[1]) as fp: ast = compile(fp.read(), fp.name, 'exec') except IOError as e: print("Could not open %s in the environment [%s]: %s" % (sys.argv[1], sys.argv[0], e)) sys.exit(1) sys.argv = sys.argv[1:] old_name = globals()['__name__'] try: globals()['__name__'] = '__main__' Compatibility.exec_function(ast, globals()) finally: globals()['__name__'] = old_name else: import code code.interact()
def _execute(self, *args, **kwargs): args = self._create_command(*args, **kwargs) with mutable_sys(): sys.stdout = sys.stderr = output = StringIO() try: devpi(args) return output.getvalue() except SystemExit: raise DevpiClientError(output.getvalue())
def _execute(self, *args, **kwargs): kwargs = OrderedDict(kwargs) kwargs.update({'--clientdir': self._client_dir}) args = ['devpi'] + list(args) + ['{}={}'.format(k, v) for k,v in iteritems(kwargs)] with mutable_sys(): sys.stdout = sys.stderr = output = StringIO() try: devpi(args) return output.getvalue() except SystemExit: raise DevpiClientError(output.getvalue())