27 |
|
def __configure_subprocess(self): |
28 |
|
'''Prevent subprocess module failure in frozen scripts on Windows. |
29 |
|
Prevent console windows from popping up when not console based. |
30 |
< |
Make sure we use the version specific string types. |
30 |
> |
Make sure we use the version-specific string types. |
31 |
|
''' |
32 |
|
# On Windows, sys.stdxxx may not be available when: |
33 |
|
# - built as *.exe with "pyinstaller --noconsole" |
66 |
|
return s |
67 |
|
return ' '.join([_q(s) for s in sl]) |
68 |
|
|
69 |
+ |
def __parse_args(self, _in, out): |
70 |
+ |
try: self.__proc_mixin_setup |
71 |
+ |
except AttributeError: self.__configure_subprocess() |
72 |
+ |
instr = '' |
73 |
+ |
if _in == PIPE: |
74 |
+ |
stdin = _in |
75 |
+ |
elif isinstance(_in, self._strtypes): |
76 |
+ |
stdin = open(_in, 'rb') |
77 |
+ |
instr = ' < "%s"' % _in |
78 |
+ |
elif hasattr(_in, 'read'): |
79 |
+ |
stdin = _in |
80 |
+ |
instr = ' < "%s"' % _in.name |
81 |
+ |
else: stdin = self._stdin |
82 |
+ |
outstr = '' |
83 |
+ |
if out == PIPE: |
84 |
+ |
stdout = out |
85 |
+ |
elif isinstance(out, self._strtypes): |
86 |
+ |
stdout = open(out, 'wb') |
87 |
+ |
outstr = ' > "%s"' % out |
88 |
+ |
elif hasattr(out, 'write'): |
89 |
+ |
stdout = out |
90 |
+ |
outstr = ' > "%s"' % out.name |
91 |
+ |
else: stdout = self._stdout |
92 |
+ |
return stdin, stdout, instr, outstr |
93 |
+ |
|
94 |
|
def call_one(self, cmdl, actstr, _in=None, out=None, |
95 |
|
universal_newlines=False): |
96 |
|
'''Create a single subprocess, possibly with an incoming and outgoing |
115 |
|
If _in or out is a PIPE, the caller should call p.wait() on the |
116 |
|
returned Popen instance after writing to and closing it. |
117 |
|
''' |
118 |
< |
try: self.__proc_mixin_setup |
94 |
< |
except AttributeError: self.__configure_subprocess() |
95 |
< |
if _in == PIPE: stdin = _in |
96 |
< |
elif isinstance(_in, self._strtypes): stdin = open(_in, 'rb') |
97 |
< |
elif hasattr(_in, 'read'): stdin = _in |
98 |
< |
else: stdin = self._stdin |
99 |
< |
if out == PIPE: stdout = out |
100 |
< |
elif isinstance(out, self._strtypes): stdout = open(out, 'wb') |
101 |
< |
elif hasattr(out, 'write'): stdout = out |
102 |
< |
else: stdout = self._stdout |
103 |
< |
displ = cmdl[:] |
104 |
< |
if isinstance(_in, self._strtypes): displ[:0] = [_in, '>'] |
105 |
< |
if isinstance(out, self._strtypes): displ.extend(['>', out]) |
118 |
> |
stdin, stdout, instr, outstr = self.__parse_args(_in, out) |
119 |
|
if getattr(self, 'verbose', None): |
120 |
|
sys.stderr.write('### %s \n' % actstr) |
121 |
< |
sys.stderr.write(self.qjoin(displ) + '\n') |
121 |
> |
sys.stderr.write(self.qjoin(cmdl) + instr + outstr + '\n') |
122 |
|
if not getattr(self, 'donothing', None): |
123 |
< |
try: p = subprocess.Popen(cmdl, stdin=stdin, stdout=stdout, |
124 |
< |
stderr=self._stderr, |
123 |
> |
try: p = subprocess.Popen(cmdl, |
124 |
> |
stdin=stdin, stdout=stdout, stderr=self._stderr, |
125 |
|
universal_newlines=universal_newlines, **self._pipeargs) |
126 |
|
except Exception as e: |
127 |
|
self.raise_on_error(actstr, str(e)) |
131 |
|
if res != 0: |
132 |
|
self.raise_on_error(actstr, |
133 |
|
'Nonzero exit (%d) from command [%s].' |
134 |
< |
% (res, self.qjoin(displ))) |
134 |
> |
% (res, self.qjoin(cmdl)+instr+outstr+'\n')) |
135 |
|
return p |
136 |
|
|
137 |
|
def call_two(self, cmdl_1, cmdl_2, actstr_1, actstr_2, _in=None, out=None, |
144 |
|
If _in or out is PIPE, the caller should call p.wait() on both |
145 |
|
returned popen instances after writing to and closing the first on . |
146 |
|
''' |
147 |
< |
try: self.__proc_mixin_setup |
135 |
< |
except AttributeError: self.__configure_subprocess() |
136 |
< |
if _in == PIPE: stdin = _in |
137 |
< |
elif isinstance(_in, self._strtypes): stdin = open(_in, 'rb') |
138 |
< |
elif hasattr(_in, 'read'): stdin = _in |
139 |
< |
else: stdin = self._stdin |
140 |
< |
outendstr = '\n' |
141 |
< |
if out == PIPE: |
142 |
< |
stdout = out |
143 |
< |
elif isinstance(out, self._strtypes): |
144 |
< |
stdout = open(out, 'wb') |
145 |
< |
outendstr = ' > "%s"\n' % out |
146 |
< |
elif hasattr(out, 'write'): |
147 |
< |
stdout = out |
148 |
< |
outendstr = ' > "%s"\n' % out.name |
149 |
< |
else: stdout = self._stdout |
147 |
> |
stdin, stdout, instr, outstr = self.__parse_args(_in, out) |
148 |
|
if getattr(self, 'verbose', None): |
149 |
|
sys.stderr.write('### %s \n' % actstr_1) |
150 |
|
sys.stderr.write('### %s \n' % actstr_2) |
151 |
< |
sys.stderr.write(self.qjoin(cmdl_1) + ' | ') |
151 |
> |
sys.stderr.write(self.qjoin(cmdl_1) + instr + ' | ') |
152 |
|
if not getattr(self, 'donothing', None): |
153 |
< |
try: p1 = subprocess.Popen(cmdl_1, stdin=stdin, |
154 |
< |
stdout=PIPE, stderr=self._stderr, **self._pipeargs) |
153 |
> |
try: p1 = subprocess.Popen(cmdl_1, |
154 |
> |
stdin=stdin, stdout=PIPE, stderr=self._stderr, |
155 |
> |
**self._pipeargs) |
156 |
|
except Exception as e: |
157 |
|
self.raise_on_error(actstr_1, str(e)) |
158 |
|
if getattr(self, 'verbose', None): |
159 |
< |
sys.stderr.write(self.qjoin(cmdl_2) + outendstr) |
159 |
> |
sys.stderr.write(self.qjoin(cmdl_2) + outstr + '\n') |
160 |
|
if not getattr(self, 'donothing', None): |
161 |
|
try: |
162 |
< |
p2 = subprocess.Popen(cmdl_2, stdin=p1.stdout, stdout=stdout, |
163 |
< |
stderr=self._stderr, |
162 |
> |
p2 = subprocess.Popen(cmdl_2, |
163 |
> |
stdin=p1.stdout, stdout=stdout, stderr=self._stderr, |
164 |
|
universal_newlines=universal_newlines, **self._pipeargs) |
165 |
|
p1.stdout.close() |
166 |
|
except Exception as e: |
179 |
|
% (res, self.qjoin(cmdl_2))) |
180 |
|
return p1, p2 |
181 |
|
|
183 |
– |
|
182 |
|
def call_many(self, cmdlines, actstr, _in=None, out=None, |
183 |
|
universal_newlines=False): |
184 |
|
'''Create a series of N processes, chained via pipes, possibly with an |
198 |
|
# other than direct call_one(), this returns a one-item tuple! |
199 |
|
return (self.call_one(cmdlines[0], actstr, _in=_in, out=out, |
200 |
|
universal_newlines=universal_newlines),) |
201 |
< |
try: self.__proc_mixin_setup |
204 |
< |
except AttributeError: self.__configure_subprocess() |
205 |
< |
if _in == PIPE: stdin = _in |
206 |
< |
elif isinstance(_in, self._strtypes): stdin = open(_in, 'rb') |
207 |
< |
elif hasattr(_in, 'read'): stdin = _in |
208 |
< |
else: stdin = self._stdin |
209 |
< |
outendstr = '\n' |
210 |
< |
if out == PIPE: |
211 |
< |
stdout = out |
212 |
< |
elif isinstance(out, self._strtypes): |
213 |
< |
stdout = open(out, 'wb') |
214 |
< |
outendstr = ' > "%s"\n' % out |
215 |
< |
elif hasattr(out, 'write'): |
216 |
< |
stdout = out |
217 |
< |
outendstr = ' > "%s"\n' % out.name |
218 |
< |
else: stdout = self._stdout |
201 |
> |
stdin, stdout, instr, outstr = self.__parse_args(_in, out) |
202 |
|
procs = [] |
203 |
|
if getattr(self, 'verbose', None): |
204 |
|
sys.stderr.write('### %s \n' % actstr) |
205 |
< |
sys.stderr.write(self.qjoin(cmdlines[0]) + ' | ') |
205 |
> |
sys.stderr.write(self.qjoin(cmdlines[0]) + instr + ' | ') |
206 |
|
if not getattr(self, 'donothing', None): |
207 |
|
try: |
208 |
|
prevproc = subprocess.Popen(cmdlines[0], stdin=stdin, |
225 |
|
self.raise_on_error(actstr, str(e)) |
226 |
|
|
227 |
|
if getattr(self, 'verbose', None): |
228 |
< |
sys.stderr.write(self.qjoin(cmdlines[-1]) + outendstr) |
228 |
> |
sys.stderr.write(self.qjoin(cmdlines[-1]) + outstr + '\n') |
229 |
|
if not getattr(self, 'donothing', None): |
230 |
|
try: |
231 |
|
lastproc = subprocess.Popen(cmdlines[-1], stdin=prevproc.stdout, |