1010# Standard library
1111# ----------------
1212from __future__ import print_function
13- from subprocess import check_call
13+ from subprocess import run
1414import sys
1515import os
1616import os .path
2424
2525# Copied from https://docs.python.org/3.5/library/platform.html#cross-platform.
2626is_64bits = sys .maxsize > 2 ** 32
27- #
27+
28+
2829# Support code
2930# ============
3031# xqt
3132# ---
3233# Pronounced "execute": provides a simple way to execute a system command.
3334def xqt (
34- # Commands to run. For example, ``'foo -param firstArg secondArg', 'bar |
35- # grep alpha'``.
36- * cmds ,
37- # Optional keyword arguments to pass on to `subprocess.check_call <https://docs.python.org/3/library/subprocess.html#subprocess.check_call>`_.
38- ** kwargs ):
35+ # Commands to run. For example, ``'foo -param firstArg secondArg', 'bar |
36+ # grep alpha'``.
37+ * cmds ,
38+ # Optional keyword arguments to pass on to `subprocess.check_call <https://docs.python.org/3/library/subprocess.html#subprocess.check_call>`_.
39+ ** kwargs
40+ ):
3941
42+ ret = []
4043 # Although https://docs.python.org/3/library/subprocess.html#subprocess.Popen
4144 # states, "The only time you need to specify ``shell=True`` on Windows is
4245 # when the command you wish to execute is built into the shell (e.g.
@@ -55,15 +58,21 @@ def xqt(
5558 # works. See https://docs.python.org/3/library/subprocess.html#subprocess.Popen.
5659 executable = ('/bin/bash' if is_linux or is_darwin
5760 else None )
58- check_call (_ , shell = True , executable = executable , ** kwargs )
59- #
61+ ret .append (run (_ , shell = True , capture_output = True ,
62+ executable = executable , ** kwargs ))
63+
64+ # Return a list only if there were multiple commands to execute.
65+ return ret [0 ] if len (ret ) == 1 else ret
66+
67+
6068# pushd
6169# -----
6270# A context manager for pushd.
6371class pushd :
6472 def __init__ (self ,
65- # The path to change to upon entering the context manager.
66- path ):
73+ # The path to change to upon entering the context manager.
74+ path
75+ ):
6776
6877 self .path = path
6978
@@ -76,7 +85,8 @@ def __exit__(self, type_, value, traceback):
7685 flush_print ('popd - returning to {}.' .format (self .cwd ))
7786 os .chdir (self .cwd )
7887 return False
79- #
88+
89+
8090# Common tools
8191# ============
8292#
@@ -85,13 +95,15 @@ def __exit__(self, type_, value, traceback):
8595def chdir (path ):
8696 flush_print ('cd ' + path )
8797 os .chdir (path )
88- #
98+
99+
89100# mkdir
90101# -----
91102def mkdir (path ):
92103 flush_print ('mkdir ' + path )
93104 os .mkdir (path )
94- #
105+
106+
95107# flush_print
96108# -----------
97109# Anything sent to ``print`` won't be printed until Python flushes its buffers,
@@ -102,14 +114,16 @@ def flush_print(*args, **kwargs):
102114 # Flush both buffers, just in case there's something in ``stdout``.
103115 sys .stdout .flush ()
104116 sys .stderr .flush ()
105- #
117+
118+
106119# isfile
107120# ------
108121def isfile (f ):
109122 _ = os .path .isfile (f )
110123 flush_print ('File {} {}.' .format (f , 'exists' if _ else 'does not exist' ))
111124 return _
112- #
125+
126+
113127# isfile
114128# ------
115129def isdir (f ):
0 commit comments