Skip to content

Commit

Permalink
reformat with yapf
Browse files Browse the repository at this point in the history
  • Loading branch information
oconnor663 committed Apr 2, 2018
1 parent afaaa86 commit 6200d83
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
32 changes: 22 additions & 10 deletions duct.py
Expand Up @@ -161,15 +161,19 @@ def _exec(self, context):
prog_str = stringify_with_dot_if_path(self._prog)
maybe_absolute_prog = maybe_canonicalize_exe_path(prog_str, context)
args_strs = tuple(stringify_if_path(arg) for arg in self._args)
argv = (maybe_absolute_prog,) + args_strs
argv = (maybe_absolute_prog, ) + args_strs
proc = safe_popen(
argv, cwd=context.dir, env=context.env, stdin=context.stdin,
stdout=context.stdout, stderr=context.stderr)
argv,
cwd=context.dir,
env=context.env,
stdin=context.stdin,
stdout=context.stdout,
stderr=context.stderr)
code = proc.wait()
return ExecStatus(code=code, checked=True)

def __repr__(self):
argv = (self._prog,) + tuple(self._args)
argv = (self._prog, ) + tuple(self._args)
return 'cmd({0})'.format(', '.join(repr(arg) for arg in argv))


Expand All @@ -181,8 +185,13 @@ def __init__(self, shell_cmd):

def _exec(self, context):
proc = safe_popen(
self._shell_cmd, shell=True, cwd=context.dir, env=context.env,
stdin=context.stdin, stdout=context.stdout, stderr=context.stderr)
self._shell_cmd,
shell=True,
cwd=context.dir,
env=context.env,
stdin=context.stdin,
stdout=context.stdout,
stderr=context.stderr)
code = proc.wait()
return ExecStatus(code=code, checked=True)

Expand Down Expand Up @@ -236,6 +245,7 @@ def _exec(self, context):
def do_left():
with write_pipe:
return self._left._exec(left_context)

left_thread = ThreadWithReturn(target=do_left)
left_thread.start()

Expand Down Expand Up @@ -482,8 +492,8 @@ class FullEnv(IORedirectExpression):
def __init__(self, inner, env_dict):
super(FullEnv, self).__init__(inner, "full_env", [env_dict])
# Windows needs special handling of env var names.
self._env_dict = dict((convert_env_var_name(k), v)
for (k, v) in env_dict.items())
self._env_dict = dict(
(convert_env_var_name(k), v) for (k, v) in env_dict.items())

@contextmanager
def _update_context(self, context):
Expand Down Expand Up @@ -572,6 +582,7 @@ def write_thread():
write.write(input_bytes)
except PIPE_CLOSED_ERROR:
pass

thread = ThreadWithReturn(write_thread)
thread.start()
with read:
Expand Down Expand Up @@ -619,6 +630,7 @@ class ThreadWithReturn(threading.Thread):
value of the target function, or to see any exceptions that might've gotten
thrown. This is a thin wrapper around Thread that enhances the join
function to return values and reraise exceptions.'''

def __init__(self, target, args=(), kwargs=None, **thread_kwargs):
threading.Thread.__init__(self, **thread_kwargs)
self._target = target
Expand Down Expand Up @@ -673,8 +685,8 @@ def maybe_canonicalize_exe_path(exe_name, iocontext):
thing Windows users have to watch out for instead is local files shadowing
global program names, which I don't think we can or should prevent.)'''

has_sep = (os.path.sep in exe_name or
(os.path.altsep is not None and os.path.altsep in exe_name))
has_sep = (os.path.sep in exe_name
or (os.path.altsep is not None and os.path.altsep in exe_name))

if has_sep and iocontext.dir is not None and not os.path.isabs(exe_name):
return os.path.realpath(exe_name)
Expand Down
26 changes: 11 additions & 15 deletions test_duct.py
Expand Up @@ -19,10 +19,10 @@

NEWLINE = os.linesep.encode()


# Windows-compatible commands to mimic Unix
# -----------------------------------------


def exit_cmd(n):
return cmd('python', '-c', 'import sys; sys.exit({0})'.format(n))

Expand Down Expand Up @@ -77,6 +77,7 @@ def replace(a, b):
# utilities
# ---------


def mktemp():
fd, path = tempfile.mkstemp()
os.close(fd)
Expand All @@ -86,6 +87,7 @@ def mktemp():
# tests
# -----


def test_hello_world():
out = sh('echo hello world').read()
assert "hello world" == out
Expand All @@ -112,8 +114,8 @@ def test_start():


def test_bytes():
out = head_bytes(10).input(b'\x00'*100).read()
assert '\x00'*10 == out
out = head_bytes(10).input(b'\x00' * 100).read()
assert '\x00' * 10 == out


def test_nonzero_status_throws():
Expand Down Expand Up @@ -209,7 +211,7 @@ def test_dir_with_relative_paths():
interpreter_path = sys.executable
interpreter_dir = os.path.dirname(interpreter_path)
interpreter_relative_path = os.path.join(
".", os.path.basename(interpreter_path))
".", os.path.basename(interpreter_path))
current_dir = os.getcwd()
try:
os.chdir(interpreter_dir)
Expand Down Expand Up @@ -297,11 +299,8 @@ def test_stdout():
out = sh('echo hi').stdout_null().read()
assert '' == out
# to stderr
result = (sh('echo hi')
.stdout_to_stderr()
.stdout_capture()
.stderr_capture()
.run())
result = (sh('echo hi').stdout_to_stderr().stdout_capture()
.stderr_capture().run())
assert b'' == result.stdout
assert b'hi' + NEWLINE == result.stderr

Expand All @@ -328,12 +327,8 @@ def test_stderr():
out = sh('echo hi').stdout_to_stderr().stderr_null().read()
assert '' == out
# to stdout
result = (sh('echo hi')
.stdout_to_stderr()
.stderr_to_stdout()
.stdout_capture()
.stderr_capture()
.run())
result = (sh('echo hi').stdout_to_stderr().stderr_to_stdout()
.stdout_capture().stderr_capture().run())
assert b'hi' + NEWLINE == result.stdout
assert b'' == result.stderr

Expand Down Expand Up @@ -381,6 +376,7 @@ def test_checked_error_contains_status():
def test_ThreadWithReturn_reraises_exceptions():
def t():
raise ZeroDivisionError

thread = duct.ThreadWithReturn(t)
thread.start()
with raises(ZeroDivisionError):
Expand Down

0 comments on commit 6200d83

Please sign in to comment.