def _load(self, rules):
"""Load IPTables rules
Args:
list: IPTables rules
Returns:
(int, Optional[str]): A tuple where the first object is the return code and the second is an optional error string associated to the return code.
"""
tmpfile = tempfile.NamedTemporaryFile(
dir=self._sessions_dir, delete=False)
tmpfile.write("\n".join(rules))
tmpfile.close()
os.chmod(tmpfile.name, 0755)
proc = subprocess.Popen(tmpfile.name, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
os.remove(tmpfile.name)
return proc.returncode, err
python类PIPE的实例源码
def clone(cls, remote_url, path):
"""Clone the remote and return a GitVcs object pointed at the new repo.
:param str remote_url: the URL to clone from
:param str path: path to clone to
:rtype: GitVcs
:returns: a GitVcs object for the new cloned repo
:raises tigerhost.vcs.base.CommandError:
"""
args = ['git', 'clone', '--recursive', remote_url, path]
proc = Popen(args, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = proc.communicate()
if proc.returncode != 0:
raise CommandError(args[0], proc.returncode, stdout, stderr)
return cls(path=path)
def handle_eval(self, record):
# This gives a file name / directory name that no other thread can use
my_unique_filename = my_gen.next_filename()
my_unique_filename = str(my_unique_filename) + ".txt"
# Print to the input file
f = open(my_unique_filename, 'w')
f.write(array2str(record.params[0]))
f.close()
# Run the objective function and pass the filename of the input file
self.process = Popen(['./sphere_ext_files', my_unique_filename], stdout=PIPE)
out = self.process.communicate()[0]
# Parse the output
try:
val = float(out) # This raises ValueError if out is not a float
self.finish_success(record, val)
os.remove(my_unique_filename) # Remove input file
except ValueError:
logging.warning("Function evaluation crashed/failed")
self.finish_failure(record)
os.remove(my_unique_filename) # Remove input file
def _get_ntfs_drives_win(self):
'''Return list of ntfs drives using fsutil fsinfo's volumeinfo.
Result after slpit('\r\n'):
['Volume Name : Le Shwa',
'Volume Serial Number : 0xd4d56c89',
'Max Component Length : 255',
'File System Name : NTFS', --> index #3 --> split(':') --> index #1
'Is ReadWrite',.... ]'''
ntfs_drives=[]
win_drive_list=(chr(a)+ u':' for a in range(ord('A'), ord('Z')) if self.exists(chr(a)+':'))
for drive in self.drives:
volume_info=Popen(('fsutil', 'fsinfo', 'volumeInfo', drive), stdout=PIPE).communicate()[0]
file_system=volume_info.split('\r\n')[3].split(' : ')[1]
if file_system=='NTFS':
ntfs_drives.append(drive)
return ntfs_drives
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 _local_ping(self, count):
cmd = self._prepare_cmd(count)
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# check that process is alive
if p.poll() is not None:
stdout, stderr = p.communicate()
raise Exception(
'Command {!r} unexpectedly exit with message {}'.format(
cmd, stdout, stderr))
result = PingResult()
yield result
if count:
p.wait()
# Check if process still alive
elif p.poll() is None:
p.send_signal(signal.SIGINT)
stdout, stderr = p.communicate()
result.stdout = stdout
def do_run(cmd):
try:
cwd = os.getcwd() if inherit_cwd else None
if not async:
if stdin:
return subprocess.check_output(cmd, shell=True,
stderr=stderr, stdin=subprocess.PIPE, env=env_dict, cwd=cwd)
output = subprocess.check_output(cmd, shell=True, stderr=stderr, env=env_dict, cwd=cwd)
return output.decode(DEFAULT_ENCODING)
# subprocess.Popen is not thread-safe, hence use a mutex here..
try:
mutex_popen.acquire()
stdin_arg = subprocess.PIPE if stdin else None
stdout_arg = open(outfile, 'wb') if isinstance(outfile, six.string_types) else outfile
process = subprocess.Popen(cmd, shell=True, stdin=stdin_arg, bufsize=-1,
stderr=stderr, stdout=stdout_arg, env=env_dict, cwd=cwd)
return process
finally:
mutex_popen.release()
except subprocess.CalledProcessError as e:
if print_error:
print("ERROR: '%s': %s" % (cmd, e.output))
raise e
def rm(filename, sudo=False):
"""
Remove a file on the disk, not us os.rm because we want to add timeout to
the command. It's possible that the os call got hang when the disk has
some problems
"""
cmd_args = []
if sudo:
cmd_args += ['sudo']
cmd_args += ['/bin/rm', filename]
log.debug("Executing cmd: {}".format(str(cmd_args)))
proc = subprocess.Popen(cmd_args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
try:
(stdout, stderr) = proc.communicate(timeout=10)
except subprocess.TimeoutExpired:
proc.kill()
raise OSCError('SHELL_TIMEOUT', {'cmd': ' '.join(cmd_args)})
def do_run(cmd):
try:
cwd = os.getcwd() if inherit_cwd else None
if not async:
if stdin:
return subprocess.check_output(cmd, shell=True,
stderr=stderr, stdin=subprocess.PIPE, env=env_dict, cwd=cwd)
output = subprocess.check_output(cmd, shell=True, stderr=stderr, env=env_dict, cwd=cwd)
return output.decode(DEFAULT_ENCODING)
# subprocess.Popen is not thread-safe, hence use a mutex here..
try:
mutex_popen.acquire()
stdin_arg = subprocess.PIPE if stdin else None
stdout_arg = open(outfile, 'wb') if isinstance(outfile, six.string_types) else outfile
process = subprocess.Popen(cmd, shell=True, stdin=stdin_arg, bufsize=-1,
stderr=stderr, stdout=stdout_arg, env=env_dict, cwd=cwd)
return process
finally:
mutex_popen.release()
except subprocess.CalledProcessError as e:
if print_error:
print("ERROR: '%s': %s" % (cmd, e.output))
raise e
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 __init__(self, args):
self.args = args
self.process = subprocess32.Popen(
args,
stdin=subprocess32.PIPE,
stdout=subprocess32.PIPE,
stderr=subprocess32.PIPE
)
self.err = None
def start_jaer(jaer_path, jaer_exec="jAERViewer1.5_linux.sh"):
"""Start jAER from Python.
This script is written for Linux usage,
An error will be raised if it's Windows OS.
Instead, windows user needs to manually setup jAER.
Parameters
----------
jaer_path : string
absolute save path of jAER.
e.g. /Users/dgyHome/Documents/workspace/jaer/trunk
jaer_exec : string
The executable of jAER. Version 1.5 is assumed.
Returns
-------
An opened jAER viewer.
"""
# Check OS type
if os.name != "posix":
raise ValueError("The Operating System is not a POSIX platform")
commands = "cd "+jaer_path+"; bash "+jaer_exec
process = subprocess.Popen(commands, stdout=subprocess.PIPE, shell=True)
return process
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
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 _test(self, rules_file, queue):
# Import the firewall rules in a detached network namespace
unshare(CLONE_NEWNET)
proc = subprocess.Popen(rules_file,
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
if proc.returncode:
queue.put((False, err.strip()))
else:
queue.put((True, None))
def run(self, name):
"""Run a check
Raises:
CheckError
"""
if not self.exists(name):
raise CheckNotFound("Check not found")
if self._checks[name]['type'] == 'exec':
proc = subprocess.Popen(
self._checks[name]['command'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
out, err = proc.communicate(timeout=self._checks[name]['timeout'])
except subprocess.TimeoutExpired as e:
raise CheckError("Timed out")
except Exception as e:
raise CheckError(e.message)
if proc.returncode:
raise CheckError("Command failed with exitstatus {} [{}]".format(
proc.returncode, err.strip()))
elif self._checks[name]['type'] == 'tcp':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self._checks[name]['timeout'])
try:
result = sock.connect_ex(
(self._checks[name]['host'], self._checks[name]['port']))
sock.close()
if result != 0:
raise Exception("Connection failed (Errno: {})".format(result))
except socket.timeout as e:
raise CheckError("Timed out")
except Exception as e:
raise CheckError(e.message)
finally:
sock.close()
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 raw_cmd(self, *args):
'''adb command. return the subprocess.Popen object.'''
cmd_line = [self.adb()] + self.adb_host_port_options + list(args)
if os.name != "nt":
cmd_line = [" ".join(cmd_line)]
return subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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 start_app(self, bundle_id):
'''
Start app by bundle_id
Args:
- bundle_id(string): ex com.netease.my
Returns:
idevicedebug subprocess instance
'''
idevicedebug = must_look_exec('idevicedebug')
# run in background
kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
if sys.platform != 'darwin':
kwargs['close_fds'] = True
return subprocess.Popen([idevicedebug, "--udid", self.udid, 'run', bundle_id], **kwargs)
def _init_instruments(self, bundle_id):
self._bootstrap = os.path.join(__dir__, 'bootstrap.sh')
self._bundle_id = bundle_id
self._env.update({'UDID': self.udid, 'BUNDLE_ID': self._bundle_id})
# 1. remove pipe
# subprocess.check_output([self._bootstrap, 'reset'], env=self._env)
# 2. start instruments
self._proc = subprocess.Popen([self._bootstrap, 'instruments'], env=self._env, stdout=subprocess.PIPE)
self.sleep(5.0)
self._wait_instruments()
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 raw_cmd(self, *args):
'''adb command. return the subprocess.Popen object.'''
cmd_line = [self.adb()] + self.adb_host_port_options + list(args)
if os.name != "nt":
cmd_line = [" ".join(cmd_line)]
return subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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 start_app(self, bundle_id):
'''
Start app by bundle_id
Args:
- bundle_id(string): ex com.netease.my
Returns:
idevicedebug subprocess instance
'''
idevicedebug = must_look_exec('idevicedebug')
# run in background
kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
if sys.platform != 'darwin':
kwargs['close_fds'] = True
return subprocess.Popen([idevicedebug, "--udid", self.udid, 'run', bundle_id], **kwargs)
def _init_instruments(self, bundle_id):
self._bootstrap = os.path.join(__dir__, 'bootstrap.sh')
self._bundle_id = bundle_id
self._env.update({'UDID': self.udid, 'BUNDLE_ID': self._bundle_id})
# 1. remove pipe
# subprocess.check_output([self._bootstrap, 'reset'], env=self._env)
# 2. start instruments
self._proc = subprocess.Popen([self._bootstrap, 'instruments'], env=self._env, stdout=subprocess.PIPE)
self.sleep(5.0)
self._wait_instruments()
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 compile_problem(solution_spec):
"""Compiles a problem submission and generates a problem spec.
Args:
solution_spec: Specification string of a solution corresponding to the
submitted problem.
Returns:
(problem_spec, problem_size)
problem_spec: Specification string of the problem.
problem_size: Problem size.
Raises:
VerificationError: If the solution specification is invalid.
subprocess.TimeoutExpired: On judge timeout.
AssertionError: On scrape error.
"""
with make_temporary_file_with_content(solution_spec) as solution_file:
proc = subprocess.Popen(
['./akatsuki', '--logtostderr', '--compile', solution_file.name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
try:
stdout_output, stderr_output = proc.communicate(
timeout=_JUDGE_TIMEOUT_SECONDS)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait()
raise # report ISE
if proc.returncode:
m = _VERIFICATION_ERROR_RE.search(stdout_output)
assert m, stdout_output # report ISE
raise VerificationError(m.group(1))
problem_spec = stdout_output
problem_size = sum(len(s) for s in problem_spec.split())
return (problem_spec, problem_size)
def evaluate_solution(problem_spec, solution_spec):
"""Evaluates a solution submission.
Args:
problem_spec: Specification string of a problem.
solution_spec: Specification string of a solution.
Returns:
(resemblance_int, raw_evaluator_output)
Raises:
VerificationError: If any of the specifications are invalid.
subprocess.TimeoutExpired: On judge timeout.
AssertionError: On scrape error.
"""
with make_temporary_file_with_content(problem_spec) as problem_file, \
make_temporary_file_with_content(solution_spec) as solution_file:
proc = subprocess.Popen(
['./akatsuki', '--logtostderr', '--evaluate',
problem_file.name, solution_file.name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
try:
stdout_output, stderr_output = proc.communicate(
timeout=_JUDGE_TIMEOUT_SECONDS)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait()
raise # report ISE
if proc.returncode:
m = _VERIFICATION_ERROR_RE.search(stdout_output)
assert m, stdout_output # report ISE
raise VerificationError(m.group(1))
m = re.search(r'integer_resemblance: (\d+)', stdout_output)
assert m, stdout_output # report ISE
resemblance_int = int(m.group(1))
return resemblance_int, stdout_output.decode('utf-8')
def start_dot11decrypt(self, interface, decryption_key): #starts and returns dot11decrypt subprocess and interface.
print "Starting new dot11decrypt subprocess on " + interface + " with key " + decryption_key
proc = subprocess32.Popen(['sudo', 'd11decrypt/build/dot11decrypt', interface, decryption_key], stdout=subprocess32.PIPE)
read = proc.stdout.readline()
if read[0:14] == "Using device: ":
print "Currently decrypting packets and releasing to " + read[14:].rstrip()
print "Process number is " + str(proc.pid)
return proc, read[14:].rstrip()
else:
print read
raise Exception
def Run(self, command, timeout=None, env=None):
"""Run a command against this SDK installation.
Args:
command: string, list or tuple, The command to run (e.g. ['gsutil', 'cp',
...])
timeout: number, Seconds to wait before timing out the command.
env: dict or None, Extra environmental variables use with this command.
Returns:
(stdout, stderr, returncode) returned from the command.
Raises:
error.SDKError: If the command cannot be run.
"""
# Add the passed in variables to the precomputed environment (without
# altering either dictionary).
if env:
env = dict(self._env, **env)
else:
env = self._env
p = subprocess.Popen(
_PrepareCommand(command), stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=os.path.dirname(self._sdk_dir), env=env)
if TIMEOUT_ENABLED:
out, err = p.communicate(timeout=timeout)
else:
if timeout:
sys.stderr.write(
'Warning: timeout specified, but subprocess32 is not available.')
out, err = p.communicate()
# TODO(magimaster): Change this to raise an error if returncode isn't 0
return out, err, p.returncode