我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用subprocess32.STDOUT。
def test_should_trigger_on_connect_if_client_connect_valid(server_with_mocks): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient new SubscriptionClient('ws://localhost:{1}/socket') '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) try: subprocess.check_output( ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2) except: mock = server_with_mocks.get_nowait() assert mock.name == 'on_connect' mock.assert_called_once()
def test_should_trigger_on_connect_with_correct_cxn_params(server_with_mocks): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const connectionParams = {{test: true}} new SubscriptionClient('ws://localhost:{1}/socket', {{ connectionParams, }}) '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) try: subprocess.check_output( ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2) except: mock = server_with_mocks.get_nowait() assert mock.name == 'on_connect' mock.assert_called_once() mock.assert_called_with({'test': True})
def open_process(command, cwd=None, shell=True, _popen_lock=threading.Lock()): kwargs = { "shell": shell, "stdout": subprocess.PIPE, "stderr": subprocess.STDOUT, "stdin": subprocess.PIPE, "bufsize": 1, # Line buffered "universal_newlines": True, } if cwd is not None: kwargs["cwd"] = cwd # Prevent signal propagation from parent process try: # Windows kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP except AttributeError: # Unix kwargs["preexec_fn"] = os.setpgrp with _popen_lock: # Work around Python 2 Popen race condition return subprocess.Popen(command, **kwargs)
def nginx_server_process(prefix='', echo=False): args = [ os.path.join(prefix, 'nginx'), '-c', resource_filename('snovault', 'nginx-dev.conf'), '-g', 'daemon off;' ] process = subprocess.Popen( args, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) if not echo: process.stdout.close() if echo: print('Started: http://localhost:8000') return process
def test_should_call_unsubscribe_when_client_closes_cxn(server_with_mocks): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const client = new SubscriptionClient('ws://localhost:{1}/socket') client.subscribe({{ query: `subscription useInfo($id: String) {{ user(id: $id) {{ id name }} }}`, operationName: 'useInfo', variables: {{ id: 3, }}, }}, function (error, result) {{ // nothing }} ) setTimeout(() => {{ client.client.close() }}, 500) '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) try: subprocess.check_output( ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=1) except: while True: mock = server_with_mocks.get_nowait() if mock.name == 'on_unsubscribe': mock.assert_called_once() break
def test_should_trigger_on_subscribe_when_client_subscribes(server_with_mocks): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const client = new SubscriptionClient('ws://localhost:{1}/socket') client.subscribe({{ query: `subscription useInfo($id: String) {{ user(id: $id) {{ id name }} }}`, operationName: 'useInfo', variables: {{ id: 3, }}, }}, function (error, result) {{ // nothing }}) '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) try: subprocess.check_output( ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2) except: while True: mock = server_with_mocks.get_nowait() if mock.name == 'on_subscribe': mock.assert_called_once() break
def test_should_trigger_on_unsubscribe_when_client_unsubscribes( server_with_mocks): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const client = new SubscriptionClient('ws://localhost:{1}/socket') const subId = client.subscribe({{ query: `subscription useInfo($id: String) {{ user(id: $id) {{ id name }} }}`, operationName: 'useInfo', variables: {{ id: 3, }}, }}, function (error, result) {{ // nothing }}) client.unsubscribe(subId) '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) try: subprocess.check_output( ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2) except: while True: mock = server_with_mocks.get_nowait() if mock.name == 'on_unsubscribe': mock.assert_called_once() break
def test_passes_through_websocket_request_to_on_subscribe(server): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const client = new SubscriptionClient('ws://localhost:{1}/socket') client.subscribe({{ query: `subscription context {{ context }}`, variables: {{}}, }}, (error, result) => {{ if (error) {{ console.log(JSON.stringify(error)); }} }} ); '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) try: subprocess.check_output( ['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2) except: while True: mock = server.get_nowait() if mock.name == 'on_subscribe': mock.assert_called_once() mock.assert_called_with_contains('websocket') break
def test_rejects_client_that_does_not_specifiy_a_supported_protocol(server): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const client = new WebSocket('ws://localhost:{1}/socket') client.on('close', (code) => {{ console.log(JSON.stringify(code)) }} ); '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) p = subprocess.Popen( ['node', '-e', node_script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) q = queue.Queue() t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() time.sleep(.2) ret_values = [] while True: try: _line = q.get_nowait() if isinstance(_line, bytes): line = _line.decode() line = json.loads(line) ret_values.append(line) except ValueError: pass except queue.Empty: break assert ret_values[0] == 1002 or 1006
def _call_phantom(self, token, arns, output_file): """ shells out to phantomjs. - Writes ARNs to a file that phantomjs will read as an input. - Phantomjs exchanges the token for session cookies. - Phantomjs then navigates to the IAM page and executes JavaScript to call GenerateServiceLastAccessedDetails for each ARN. - Every 10 seconds, Phantomjs calls GetServiceLastAccessedDetails - Phantom saves output to a file that is used by `persist()` :return: Exit code from phantomjs subprocess32 """ path = os.path.dirname(__file__) console_js = os.path.join(path, 'awsconsole.js') with tempfile.NamedTemporaryFile() as f: json.dump(arns, f) f.seek(0) try: p = subprocess32.Popen([ self.current_app.config.get('PHANTOMJS'), console_js, token, f.name, output_file], stdout=subprocess32.PIPE, stderr=subprocess32.STDOUT) output, errs = p.communicate(timeout=1200) # 20 mins self.current_app.logger.debug('Phantom Output: \n{}'.format(output)) self.current_app.logger.debug('Phantom Errors: \n{}'.format(errs)) except subprocess32.TimeoutExpired: self.current_app.logger.error('PhantomJS timed out') return 1 # return code 1 for timeout except CalledProcessError: self.current_app.logger.error('PhantomJS exited: {}' ''.format(p.returncode)) return p.returncode else: self.current_app.logger.info('PhantomJS exited: 0') return 0
def background_test(self): self.running = True proc = subprocess.Popen('echo hello', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: line = proc.stdout.readline() if line == '': break print line for client in ProgressHandler.clients: client.write_message(line) self.output = self.output + line self.running = False
def check_output(cmds, shell=False): try: output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=shell) return output except subprocess.CalledProcessError: # logger.warn('Failed to run command: %s', ' '.join(cmds)) # logger.warn('Error output:\n%s', e.output) raise
def idevice(name, *args): exec_name = 'idevice' + name exec_path = look_exec(exec_name) if not exec_path: raise EnvironmentError('Necessary binary ("%s") not found.' % exec_name) cmds = [exec_path] + list(args) try: output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=False) return output except subprocess.CalledProcessError: raise
def __init__(self, device_url, bundle_id=None): DeviceMixin.__init__(self) self.__device_url = device_url self.__scale = None self._wda = wda.Client(device_url) self._session = None self._bundle_id = None if bundle_id: self.start_app(bundle_id) # ioskit.Device.__init__(self, udid) # # xcodebuild -project -scheme WebDriverAgentRunner -destination "id=1002c0174e481a651d71e3d9a89bd6f90d253446" test # # Test Case '-[UITestingUITests testRunner]' started. # xproj_dir = os.getenv('WEBDRIVERAGENT_DIR') # if not xproj_dir: # raise RuntimeError("env-var WEBDRIVERAGENT_DIR need to be set") # proc = self._xcproc = subprocess.Popen(['/usr/bin/xcodebuild', # '-project', 'WebDriverAgent.xcodeproj', # '-scheme', 'WebDriverAgentRunner', # '-destination', 'id='+self.udid, 'test'], # stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=xproj_dir, bufsize=1, universal_newlines=True) # for line in iter(proc.stdout.readline, b""): # print 'STDOUT:', line.strip() # if 'TEST FAILED' in line: # raise RuntimeError("webdriver start test failed, maybe need to unlock the keychain, try\n" + # '$ security unlock-keychain ~/Library/Keychains/login.keychain') # elif "Successfully wrote Manifest cache" in line: # print 'GOOD ^_^, wait 5s' # time.sleep(5.0) # break
def _which(editor): try: editor_path = subprocess.check_output(['which', editor], stderr=subprocess.STDOUT).strip() if six.PY3: editor_path = editor_path.decode() except subprocess.CalledProcessError: editor_path = None return editor_path
def __init__(self, device_url, bundle_id=None): DeviceMixin.__init__(self) self.__device_url = device_url self.__display = None self.__scale = None self._wda = wda.Client(device_url) self._session = None self._bundle_id = None if bundle_id: self.start_app(bundle_id) # ioskit.Device.__init__(self, udid) # # xcodebuild -project -scheme WebDriverAgentRunner -destination "id=1002c0174e481a651d71e3d9a89bd6f90d253446" test # # Test Case '-[UITestingUITests testRunner]' started. # xproj_dir = os.getenv('WEBDRIVERAGENT_DIR') # if not xproj_dir: # raise RuntimeError("env-var WEBDRIVERAGENT_DIR need to be set") # proc = self._xcproc = subprocess.Popen(['/usr/bin/xcodebuild', # '-project', 'WebDriverAgent.xcodeproj', # '-scheme', 'WebDriverAgentRunner', # '-destination', 'id='+self.udid, 'test'], # stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=xproj_dir, bufsize=1, universal_newlines=True) # for line in iter(proc.stdout.readline, b""): # print 'STDOUT:', line.strip() # if 'TEST FAILED' in line: # raise RuntimeError("webdriver start test failed, maybe need to unlock the keychain, try\n" + # '$ security unlock-keychain ~/Library/Keychains/login.keychain') # elif "Successfully wrote Manifest cache" in line: # print 'GOOD ^_^, wait 5s' # time.sleep(5.0) # break
def parent_identical_or_crashes(self, crash, parent): # Base names cbasename = os.path.basename(crash) pbasename = os.path.basename(parent) ## Filter queue filenames with sig info if self.find_crash_parent_regex.match(pbasename): self.logr("Parent ({}) looks like crashing input!".format(pbasename)) return True try: diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent), stderr=subprocess.STDOUT, shell=True) except Exception, e: diff_out = e.output if not diff_out.rstrip("\n"): self.logr("Crash file ({}) and parent ({}) are identical!" .format(cbasename, pbasename)) return True cov_cmd = self.args.coverage_cmd.replace('AFL_FILE', parent) ### Dry-run to make sure parent doesn't cause a crash if self.does_dry_run_throw_error(cov_cmd): self.logr("Parent ({}) crashes binary!".format(pbasename)) return True return False
def does_dry_run_throw_error(self, cmd): try: out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True) except Exception, e: return (e.returncode > 128) return False
def run_cmd(self, cmd, collect, env=None): out = [] if self.args.verbose: self.logr(" CMD: %s" % cmd) fh = None if self.args.disable_cmd_redirection or collect == self.Want_Output: fh = open(self.cov_paths['tmp_out'], 'w') else: fh = open(os.devnull, 'w') if env is None: subprocess.call(cmd, stdin=None, stdout=fh, stderr=subprocess.STDOUT, shell=True, executable='/bin/bash') else: subprocess.call(cmd, stdin=None, stdout=fh, stderr=subprocess.STDOUT, shell=True, env=env, executable='/bin/bash') fh.close() if self.args.disable_cmd_redirection or collect == self.Want_Output: with open(self.cov_paths['tmp_out'], 'r') as f: for line in f: out.append(line.rstrip('\n')) return out
def run_proc_with_quit(proc_id, quit_dict, args, logfile=None, append=False, env=None, cwd=None): if logfile is None: logfile = os.devnull mode = 'ab' if append else 'wb' with open(logfile, mode) as logf: if proc_id in quit_dict: return None proc = subprocess.Popen(args, stdout=logf, stderr=subprocess.STDOUT, env=env, cwd=cwd) retcode = None num_kill = 0 timeout = 0.05 while retcode is None and num_kill <= 2: try: retcode = proc.wait(timeout=timeout) except subprocess.TimeoutExpired: if proc_id in quit_dict: if num_kill == 0: proc.terminate() timeout = quit_dict[proc_id] elif num_kill == 1: proc.kill() num_kill += 1 return proc.returncode
def check_call_realtime(args): """Run command with arguments and yield the output as they come. Stderr is piped into stdout. :raises subprocess.CalledProcessError: if exit code is non-zero """ p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while p.poll() is None: yield p.stdout.read() yield p.stdout.read() if p.returncode != 0: raise subprocess.CalledProcessError(p.returncode, args)
def spawn(self): """Spawn the fake executable using subprocess.Popen.""" self._process = subprocess.Popen( [self.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) self.addCleanup(self._process_kill)
def parent_identical_or_crashes(self, crash, parent): # Base names cbasename = os.path.basename(crash) pbasename = os.path.basename(parent) ## Filter queue filenames with sig info if self.find_crash_parent_regex.match(pbasename): self.logr("Parent ({}) looks like crashing input!".format(pbasename)) return True try: diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent), stderr=subprocess.STDOUT, shell=True) except Exception, e: diff_out = e.output if not diff_out.rstrip("\n"): self.logr("Crash file ({}) and parent ({}) are identical!" .format(cbasename, pbasename)) return True cov_cmd = self.coverage_cmd.replace('AFL_FILE', parent) ### Dry-run to make sure parent doesn't cause a crash if self.does_dry_run_throw_error(cov_cmd): self.logr("Parent ({}) crashes binary!".format(pbasename)) return True return False
def does_dry_run_throw_error(self, cmd): env = os.environ.copy() if self.sanitizer == 'asan': spectrum_asan_options(env) try: out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True, env=env) except Exception, e: # OR condition is bug fix for compute shell returning negative instead of positive code return (e.returncode > 128 or e.returncode < 0) return False
def subproc_call(cmd, timeout=None): try: output = subprocess.check_output( cmd, stderr=subprocess.STDOUT, shell=True, timeout=timeout) return output except subprocess.TimeoutExpired as e: logger.warn("Command timeout!") logger.warn(e.output) except subprocess.CalledProcessError as e: logger.warn("Commnad failed: {}".format(e.returncode)) logger.warn(e.output)
def initdb(datadir, prefix='', echo=False): init_args = [ os.path.join(prefix, 'initdb'), '-D', datadir, '-U', 'postgres', '--auth=trust', ] output = subprocess.check_output( init_args, close_fds=True, stderr=subprocess.STDOUT, ) if echo: print(output.decode('utf-8'))
def test_correctly_sets_the_context_in_on_subscribe(server): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const CTX = 'testContext'; const client = new SubscriptionClient('ws://localhost:{1}/socket') client.subscribe({{ query: `subscription context {{ context }}`, variables: {{}}, context: CTX, }}, (error, result) => {{ client.unsubscribeAll(); if (error) {{ console.log(JSON.stringify(error)); }} if (result) {{ console.log(JSON.stringify({{ client: {{ result: result, }} }})); }} else {{ // pass }} }} ); '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) p = subprocess.Popen( ['node', '-e', node_script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) time.sleep(.2) requests.post( 'http://localhost:{0}/publish'.format(TEST_PORT), json=['context', {}]) q = queue.Queue() t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() time.sleep(.2) ret_values = {} while True: try: _line = q.get_nowait() if isinstance(_line, bytes): line = _line.decode() line = json.loads(line) ret_values[list(line.keys())[0]] = line[list(line.keys())[0]] except ValueError: pass except queue.Empty: break client = ret_values['client'] assert client['result']['context'] assert client['result']['context'] == 'testContext'
def test_rejects_unparsable_message(server): node_script = ''' module.paths.push('{0}'); WebSocket = require('ws'); const GRAPHQL_SUBSCRIPTIONS = 'graphql-subscriptions'; const client = new WebSocket('ws://localhost:{1}/socket', GRAPHQL_SUBSCRIPTIONS); client.onmessage = (message) => {{ let msg = JSON.parse(message.data) console.log(JSON.stringify({{[msg.type]: msg}})) client.close(); }}; client.onopen = () => {{ client.send('HI'); }} '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) p = subprocess.Popen( ['node', '-e', node_script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) q = queue.Queue() t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() time.sleep(.2) ret_values = {} while True: try: _line = q.get_nowait() if isinstance(_line, bytes): line = _line.decode() line = json.loads(line) ret_values[list(line.keys())[0]] = line[list(line.keys())[0]] except ValueError: pass except queue.Empty: break assert ret_values['subscription_fail'] assert len(ret_values['subscription_fail']['payload']['errors']) > 0
def test_rejects_nonsense_message(server): node_script = ''' module.paths.push('{0}'); WebSocket = require('ws'); const GRAPHQL_SUBSCRIPTIONS = 'graphql-subscriptions'; const client = new WebSocket('ws://localhost:{1}/socket', GRAPHQL_SUBSCRIPTIONS); client.onmessage = (message) => {{ let msg = JSON.parse(message.data) console.log(JSON.stringify({{[msg.type]: msg}})) client.close(); }}; client.onopen = () => {{ client.send(JSON.stringify({{}})); }} '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) p = subprocess.Popen( ['node', '-e', node_script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) q = queue.Queue() t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() time.sleep(.2) ret_values = {} while True: try: _line = q.get_nowait() if isinstance(_line, bytes): line = _line.decode() line = json.loads(line) ret_values[list(line.keys())[0]] = line[list(line.keys())[0]] except ValueError: pass except queue.Empty: break assert ret_values['subscription_fail'] assert len(ret_values['subscription_fail']['payload']['errors']) > 0
def test_sends_back_any_type_of_error(server): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const client = new SubscriptionClient('ws://localhost:{1}/socket') client.subscribe({{ query: `invalid useInfo {{ error }}`, variables: {{}}, }}, function (errors, result) {{ client.unsubscribeAll(); if (errors) {{ console.log(JSON.stringify({{'errors': errors}})) }} if (result) {{ console.log(JSON.stringify({{'result': result}})) }} }} ); '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) p = subprocess.Popen( ['node', '-e', node_script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) time.sleep(5) q = queue.Queue() t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() time.sleep(.2) ret_values = {} while True: try: _line = q.get_nowait() if isinstance(_line, bytes): line = _line.decode() line = json.loads(line) ret_values[list(line.keys())[0]] = line[list(line.keys())[0]] except ValueError: pass except queue.Empty: break assert len(ret_values['errors']) > 0
def test_handles_errors_prior_to_graphql_execution(server_with_on_sub_handler): node_script = ''' module.paths.push('{0}') WebSocket = require('ws') const SubscriptionClient = require('subscriptions-transport-ws').SubscriptionClient const client = new SubscriptionClient('ws://localhost:{1}/socket') client.subscribe({{ query: `subscription context {{ context }}`, variables: {{}}, context: {{}}, }}, function (errors, result) {{ client.unsubscribeAll(); if (errors) {{ console.log(JSON.stringify({{'errors': errors}})) }} if (result) {{ console.log(JSON.stringify({{'result': result}})) }} }} ); '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) p = subprocess.Popen( ['node', '-e', node_script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) time.sleep(.2) requests.post( 'http://localhost:{0}/publish'.format(TEST_PORT), json=['context', {}]) q = queue.Queue() t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() time.sleep(.2) ret_values = {} while True: try: _line = q.get_nowait() if isinstance(_line, bytes): line = _line.decode() line = json.loads(line) ret_values[list(line.keys())[0]] = line[list(line.keys())[0]] except ValueError: pass except queue.Empty: break assert isinstance(ret_values['errors'], list) assert ret_values['errors'][0]['message'] == 'bad'
def test_sends_a_keep_alive_signal_in_the_socket(server_with_keep_alive): node_script = ''' module.paths.push('{0}'); WebSocket = require('ws'); const GRAPHQL_SUBSCRIPTIONS = 'graphql-subscriptions'; const KEEP_ALIVE = 'keepalive'; const client = new WebSocket('ws://localhost:{1}/socket', GRAPHQL_SUBSCRIPTIONS); let yieldCount = 0; client.onmessage = (message) => {{ let msg = JSON.parse(message.data) if (msg.type === KEEP_ALIVE) {{ yieldCount += 1; if (yieldCount > 1) {{ let returnMsg = {{'type': msg.type, 'yieldCount': yieldCount}} console.log(JSON.stringify(returnMsg)) client.close(); }} }} }}; '''.format( os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT) p = subprocess.Popen( ['node', '-e', node_script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) q = queue.Queue() t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) t.daemon = True t.start() time.sleep(.5) while True: try: _line = q.get_nowait() if isinstance(_line, bytes): line = _line.decode() ret_value = json.loads(line) except ValueError: pass except queue.Empty: break assert ret_value['type'] == 'keepalive' assert ret_value['yieldCount'] > 1
def run_cmd(cmd, args=None, tool=None): stats = {'timed_out': False, 'output': ''} timer = None out = None out_file = None friendly_cmd = ' '.join(cmd) if args and args.verbose and args.log_to_stderr: out = sys.stderr elif tool: out_file = os.path.join(args.output_directory, tool + ".log") out = open(out_file, 'a') def output(line): if out: out.write(line) out.flush() def kill_proc(proc, stats): output("Timed out after {} seconds on {}\n".format(args.timeout, friendly_cmd)) stats['timed_out'] = True proc.kill() output("Running {}\n\n".format(friendly_cmd)) try: start_time = timeit.default_timer() process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if args and args.timeout: timer = Timer(args.timeout, kill_proc, [process, stats]) timer.start() for line in iter(process.stdout.readline, b''): stats['output'] = stats['output'] + line output(line) process.stdout.close() process.wait() stats['time'] = timeit.default_timer() - start_time stats['return_code'] = process.returncode if timer: timer.cancel() except: output('calling {cmd} failed\n{trace}\n'.format(cmd=friendly_cmd,trace=traceback.format_exc())) if out_file: out.close() return stats
def get_parent(self, filepath, isCrash=True): dirname, basename = os.path.split(filepath) if isCrash: match = self.find_crash_parent_regex.match(basename) # (_, _, session, _, syncname, src_id) = match.groups() (_, _, _, session, _, syncname, src_id) = match.groups() searchdir = self.args.afl_fuzzing_dir # if syncname: # searchdir += '/' + syncname + '/queue' if session: searchdir += '/' + session + '/queue' else: assert False, "Parent of crash file {} cannot be found".format(basename) else: match = self.find_queue_parent_regex.match(basename) if not match: self.logr("No parent could be found for {}".format(basename)) return None (_, syncname, src_id) = match.groups() searchdir = dirname if syncname: searchdir += '/../../' + syncname + '/queue' search_cmd = "find " + searchdir + " -maxdepth 1" + " -name id:" + src_id + "*" parent_fname = subprocess.check_output(search_cmd, stderr=subprocess.STDOUT, shell=True) parent_list = filter(None, parent_fname.split("\n")) if (len(parent_list) == 0): self.logr("No parents found for file {}".format(basename)) return None if (len(parent_list) > 1): self.logr("Multiple parents found for file {}. Selecting first.".format(basename)) return os.path.abspath(parent_list[0].rstrip("\n"))
def run_and_wait(args, timeout=None, logfile=None, append=False, env=None, cwd=None): """Run a command in a subprocess, then wait for it to finish. Parameters ---------- args : string or list[string] the command to run. Should be either a command string or a list of command string and its arguments as strings. A list is preferred; see Python subprocess documentation. timeout : float or None the amount of time to wait for the command to finish, in seconds. If None, waits indefinitely. logfile : string or None If given, stdout and stderr will be written to this file. append : bool True to append to the logfile. Defaults to False. env : dict[string, any] If not None, environment variables of the subprocess will be set according to this dictionary instead of inheriting from current process. cwd : string or None The current working directory of the subprocess. Returns ------- output : string the standard output and standard error from the command. Raises ------ subprocess.CalledProcessError if any error occurred in the subprocess. """ output = subprocess.check_output(args, stderr=subprocess.STDOUT, timeout=timeout, env=env, cwd=cwd) output = output.decode(encoding=bag_encoding, errors=bag_codec_error) if logfile is not None: write_file(logfile, output, append=append) return output
def get_parent(self, filepath, isCrash=True): dirname, basename = os.path.split(filepath) if isCrash: match = self.find_crash_parent_regex.match(basename) # (_, _, session, _, syncname, src_id) = match.groups() (_, _, session, _, syncname, src_id) = match.groups() searchdir = self.afl_fuzzing_dir # if syncname: # searchdir += '/' + syncname + '/queue' if session: searchdir += '/' + session + '/queue' else: assert False, "Parent of crash file {} cannot be found".format(basename) else: match = self.find_queue_parent_regex.match(basename) if not match: self.logr("No parent could be found for {}".format(basename)) return None (_, syncname, src_id) = match.groups() searchdir = dirname if syncname: searchdir += '/../../' + syncname + '/queue' search_cmd = "find " + searchdir + " -maxdepth 1" + " -name id:" + src_id + "*" parent_fname = subprocess.check_output(search_cmd, stderr=subprocess.STDOUT, shell=True) parent_list = filter(None, parent_fname.split("\n")) if (len(parent_list) == 0): self.logr("No parents found for file {}".format(basename)) return None if (len(parent_list) > 1): self.logr("Multiple parents found for file {}. Selecting first.".format(basename)) return os.path.abspath(parent_list[0].rstrip("\n"))
def server_process(datadir, host='127.0.0.1', port=9201, prefix='', echo=False): args = [ os.path.join(prefix, 'elasticsearch'), '-Enetwork.host=%s' % host, '-Ehttp.port=%d' % port, '-Epath.data=%s' % os.path.join(datadir, 'data'), '-Epath.logs=%s' % os.path.join(datadir, 'logs'), ] if os.environ.get('TRAVIS'): print('IN TRAVIS') echo=True args.append('-Epath.conf=%s/conf' % os.environ['TRAVIS_BUILD_DIR']) elif os.path.exists('/etc/elasticsearch'): print('NOT IN TRAVIS') args.append('-Epath.conf=/etc/elasticsearch') print(args) process = subprocess.Popen( args, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) SUCCESS_LINE = b'started\n' lines = [] for line in iter(process.stdout.readline, b''): if echo: sys.stdout.write(line.decode('utf-8')) lines.append(line) if line.endswith(SUCCESS_LINE): print('detected start, broke') break else: code = process.wait() msg = ('Process return code: %d\n' % code) + b''.join(lines).decode('utf-8') raise Exception(msg) if not echo: process.stdout.close() print('returning process') return process