Switch subprocess.Popen to stoneridge.Process

This is an override of subprocess.Popen that provides sensible defaults
for stone ridge programs that want to call subprocesses, namely:
 * stdout is redirected to a pipe by default
 * stderr is redirected to stdout by default
 * universal_newlines (translate '\r' or '\r\n' to '\n') is always on
This commit is contained in:
Nick Hurley 2013-02-26 12:16:28 -08:00
Родитель 1780c2faba
Коммит be462866a2
3 изменённых файлов: 31 добавлений и 35 удалений

Просмотреть файл

@ -10,7 +10,6 @@ import re
import shutil
import SocketServer
import struct
import subprocess
import tempfile
import stoneridge
@ -67,8 +66,7 @@ class BaseDnsModifier(SocketServer.BaseRequestHandler):
class MacDnsModifier(BaseDnsModifier):
def setup(self):
logging.debug('Initializing Mac handler')
p = subprocess.Popen(['networksetup', '-listnetworkserviceorder'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p = stoneridge.Process(['networksetup', '-listnetworkserviceorder'])
stdout, _ = p.communicate()
lines = stdout.split('\n')
logging.debug('networksetup -listnetworkserviceorder => %s' % (lines,))
@ -92,8 +90,7 @@ class MacDnsModifier(BaseDnsModifier):
def _set_dns(self, dnsservers):
args = ['networksetup', '-setdnsservers', self.main_if] + dnsservers
logging.debug('Setting dns using command line %s' % (args,))
p = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p = stoneridge.Process(args)
p.communicate()
def reset_dns(self):
@ -121,8 +118,7 @@ class MacDnsModifier(BaseDnsModifier):
args = ['networksetup', '-getdnsservers', self.main_if]
logging.debug('Getting original DNS server(s) using command '
'line %s' % (args,))
p = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p = stoneridge.Process(args)
stdout, _ = p.communicate()
dns_servers = stdout.split('\n')
@ -200,18 +196,14 @@ class WindowsDnsModifier(BaseDnsModifier):
def reset_dns(self):
logging.debug('About to kill DNS on StoneRidge interface')
netsh = subprocess.Popen(['netsh.exe', 'ipv4', 'set', 'dnsservers',
'StoneRidge', 'static', 'none',
'validate=no'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
netsh = stoneridge.Process(['netsh.exe', 'ipv4', 'set', 'dnsservers',
'StoneRidge', 'static', 'none',
'validate=no'])
netsh.communicate()
logging.debug('About to resurrect WAN interface')
netsh = subprocess.Popen(['netsh.exe', 'interface', 'set', 'interface',
'name=WAN', 'admin=ENABLED'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
netsh = stoneridge.Process(['netsh.exe', 'interface', 'set',
'interface', 'name=WAN', 'admin=ENABLED'])
netsh.communicate()
logging.debug('About to reset search suffix')
@ -219,21 +211,17 @@ class WindowsDnsModifier(BaseDnsModifier):
def set_dns(self, dnsserver):
logging.debug('About to kill WAN interface')
netsh = subprocess.Popen(['netsh.exe', 'interface', 'set', 'interface',
'name=WAN', 'admin=DISABLED'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
netsh = stoneridge.Process(['netsh.exe', 'interface', 'set',
'interface', 'name=WAN', 'admin=DISABLED'])
netsh.communicate()
logging.debug('About to clear search suffix')
winreg.SetValue(self.key, 'SearchList', winreg.REG_SZ, '')
logging.debug('About to set DNS on StoneRidge interface')
netsh = subprocess.Popen(['netsh.exe', 'ipv4', 'set', 'dnsservers',
'StoneRidge', 'static', dnsserver,
'validate=no'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
netsh = stoneridge.Process(['netsh.exe', 'ipv4', 'set', 'dnsservers',
'StoneRidge', 'static', dnsserver,
'validate=no'])
netsh.communicate()

Просмотреть файл

@ -6,7 +6,6 @@
import glob
import logging
import os
import subprocess
import stoneridge
@ -112,12 +111,11 @@ class StoneRidgeRunner(object):
tcpdump_out_file = os.path.join(outdir, tcpdump_out_file)
logging.debug('tcpdump output at %s' % (tcpdump_out_file,))
tcpdump_out = file(tcpdump_out_file, 'wb')
tcpdump = subprocess.Popen([tcpdump_exe, '-s', '2000',
'-U', '-p',
'-w', tcpdump_output,
'-i', tcpdump_if],
stdout=tcpdump_out,
stderr=subprocess.STDOUT)
tcpdump = stoneridge.Process([tcpdump_exe, '-s', '2000',
'-U', '-p',
'-w', tcpdump_output,
'-i', tcpdump_if],
stdout=tcpdump_out)
xpcshell_out_file = '%s.xpcshell.out' % (test,)
xpcshell_out_file = os.path.join(outdir, xpcshell_out_file)
logging.debug('xpcshell output at %s' % (xpcshell_out_file,))

Просмотреть файл

@ -111,6 +111,17 @@ class cwd(object):
os.chdir(self.oldcwd)
class Process(subprocess.Popen):
"""A subclass of subprocess.Popen that does the right things by default for
capturing stdout and stderr from programs run as part of stone ridge.
"""
def __init__(self, args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
**kwargs):
kwargs['universal_newlines'] = True
subprocess.Popen.__init__(self, args, stdout=stdout, stderr=stderr,
**kwargs)
_cp = None
_srconf = None
_runconf = None
@ -222,9 +233,8 @@ def run_xpcshell(args, stdout=subprocess.PIPE):
xpcshell_timeout = get_config_int('xpcshell', 'timeout')
xpcshell_start = int(time.time())
proc = subprocess.Popen(xpcargs, stdout=stdout,
stderr=subprocess.STDOUT, cwd=bindir,
env=_xpcshell_environ)
proc = Process(xpcargs, stdout=stdout, cwd=bindir, env=_xpcshell_environ)
while (int(time.time()) - xpcshell_start) < xpcshell_timeout:
time.sleep(5)