| 22 |
|
'''Process and pipeline management for Python Radiance scripts |
| 23 |
|
''' |
| 24 |
|
def raise_on_error(self, actstr, e): |
| 25 |
< |
raise Error('Unable to %s - %s' % (actstr, str(e))) |
| 25 |
> |
if hasattr(e, 'strerror'): eb = e.strerror |
| 26 |
> |
elif isinstance(e, self._strtypes): eb = e |
| 27 |
> |
else: eb = e |
| 28 |
> |
if isinstance(eb, type(b'')): |
| 29 |
> |
estr = eb.decode(encoding='utf-8', errors='ignore') |
| 30 |
> |
else: estr = eb |
| 31 |
> |
raise Error('Unable to %s - %s' % (actstr, estr)) # |
| 32 |
|
|
| 33 |
|
def __configure_subprocess(self): |
| 34 |
|
'''Prevent subprocess module failure in frozen scripts on Windows. |
| 35 |
|
Prevent console windows from popping up when not console based. |
| 36 |
< |
Make sure we use the version specific string types. |
| 36 |
> |
Make sure we use the version-specific string types. |
| 37 |
|
''' |
| 38 |
|
# On Windows, sys.stdxxx may not be available when: |
| 39 |
|
# - built as *.exe with "pyinstaller --noconsole" |
| 72 |
|
return s |
| 73 |
|
return ' '.join([_q(s) for s in sl]) |
| 74 |
|
|
| 75 |
+ |
def __parse_args(self, _in, out): |
| 76 |
+ |
try: self.__proc_mixin_setup |
| 77 |
+ |
except AttributeError: self.__configure_subprocess() |
| 78 |
+ |
instr = '' |
| 79 |
+ |
if _in == PIPE: |
| 80 |
+ |
stdin = _in |
| 81 |
+ |
elif isinstance(_in, self._strtypes): |
| 82 |
+ |
if self.donothing: stdin = None |
| 83 |
+ |
else: stdin = open(_in, 'rb') |
| 84 |
+ |
instr = ' < "%s"' % _in |
| 85 |
+ |
elif hasattr(_in, 'read'): |
| 86 |
+ |
stdin = _in |
| 87 |
+ |
instr = ' < "%s"' % _in.name |
| 88 |
+ |
else: stdin = self._stdin |
| 89 |
+ |
outstr = '' |
| 90 |
+ |
if out == PIPE: |
| 91 |
+ |
stdout = out |
| 92 |
+ |
elif isinstance(out, self._strtypes): |
| 93 |
+ |
if self.donothing: stdout = None |
| 94 |
+ |
else: stdout = open(out, 'wb') |
| 95 |
+ |
outstr = ' > "%s"' % out |
| 96 |
+ |
elif hasattr(out, 'write'): |
| 97 |
+ |
stdout = out |
| 98 |
+ |
outstr = ' > "%s"' % out.name |
| 99 |
+ |
else: stdout = self._stdout |
| 100 |
+ |
return stdin, stdout, instr, outstr |
| 101 |
+ |
|
| 102 |
|
def call_one(self, cmdl, actstr, _in=None, out=None, |
| 103 |
|
universal_newlines=False): |
| 104 |
|
'''Create a single subprocess, possibly with an incoming and outgoing |
| 123 |
|
If _in or out is a PIPE, the caller should call p.wait() on the |
| 124 |
|
returned Popen instance after writing to and closing it. |
| 125 |
|
''' |
| 126 |
< |
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]) |
| 126 |
> |
stdin, stdout, instr, outstr = self.__parse_args(_in, out) |
| 127 |
|
if getattr(self, 'verbose', None): |
| 128 |
|
sys.stderr.write('### %s \n' % actstr) |
| 129 |
< |
sys.stderr.write(self.qjoin(displ) + '\n') |
| 129 |
> |
sys.stderr.write(self.qjoin(cmdl) + instr + outstr + '\n') |
| 130 |
|
if not getattr(self, 'donothing', None): |
| 131 |
< |
try: p = subprocess.Popen(cmdl, stdin=stdin, stdout=stdout, |
| 132 |
< |
stderr=self._stderr, |
| 131 |
> |
try: p = subprocess.Popen(cmdl, |
| 132 |
> |
stdin=stdin, stdout=stdout, stderr=self._stderr, |
| 133 |
|
universal_newlines=universal_newlines, **self._pipeargs) |
| 134 |
|
except Exception as e: |
| 135 |
< |
self.raise_on_error(actstr, str(e)) |
| 135 |
> |
self.raise_on_error(actstr, e) |
| 136 |
|
if stdin != PIPE and stdout != PIPE: |
| 137 |
|
# caller needs to wait after reading or writing (else deadlock) |
| 138 |
|
res = p.wait() |
| 139 |
|
if res != 0: |
| 140 |
|
self.raise_on_error(actstr, |
| 141 |
|
'Nonzero exit (%d) from command [%s].' |
| 142 |
< |
% (res, self.qjoin(displ))) |
| 142 |
> |
% (res, self.qjoin(cmdl)+instr+outstr+'\n')) |
| 143 |
|
return p |
| 144 |
|
|
| 145 |
|
def call_two(self, cmdl_1, cmdl_2, actstr_1, actstr_2, _in=None, out=None, |
| 152 |
|
If _in or out is PIPE, the caller should call p.wait() on both |
| 153 |
|
returned popen instances after writing to and closing the first on . |
| 154 |
|
''' |
| 155 |
< |
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 |
| 155 |
> |
stdin, stdout, instr, outstr = self.__parse_args(_in, out) |
| 156 |
|
if getattr(self, 'verbose', None): |
| 157 |
|
sys.stderr.write('### %s \n' % actstr_1) |
| 158 |
|
sys.stderr.write('### %s \n' % actstr_2) |
| 159 |
< |
sys.stderr.write(self.qjoin(cmdl_1) + ' | ') |
| 159 |
> |
sys.stderr.write(self.qjoin(cmdl_1) + instr + ' | ') |
| 160 |
|
if not getattr(self, 'donothing', None): |
| 161 |
< |
try: p1 = subprocess.Popen(cmdl_1, stdin=stdin, |
| 162 |
< |
stdout=PIPE, stderr=self._stderr, **self._pipeargs) |
| 161 |
> |
try: p1 = subprocess.Popen(cmdl_1, |
| 162 |
> |
stdin=stdin, stdout=PIPE, stderr=self._stderr, |
| 163 |
> |
**self._pipeargs) |
| 164 |
|
except Exception as e: |
| 165 |
< |
self.raise_on_error(actstr_1, str(e)) |
| 165 |
> |
self.raise_on_error(actstr_1, e) |
| 166 |
|
if getattr(self, 'verbose', None): |
| 167 |
< |
sys.stderr.write(self.qjoin(cmdl_2) + outendstr) |
| 167 |
> |
sys.stderr.write(self.qjoin(cmdl_2) + outstr + '\n') |
| 168 |
|
if not getattr(self, 'donothing', None): |
| 169 |
|
try: |
| 170 |
< |
p2 = subprocess.Popen(cmdl_2, stdin=p1.stdout, stdout=stdout, |
| 171 |
< |
stderr=self._stderr, |
| 170 |
> |
p2 = subprocess.Popen(cmdl_2, |
| 171 |
> |
stdin=p1.stdout, stdout=stdout, stderr=self._stderr, |
| 172 |
|
universal_newlines=universal_newlines, **self._pipeargs) |
| 173 |
|
p1.stdout.close() |
| 174 |
|
except Exception as e: |
| 175 |
< |
self.raise_on_error(actstr_2, str(e)) |
| 175 |
> |
self.raise_on_error(actstr_2, e) |
| 176 |
|
if stdin != PIPE and stdout != PIPE: |
| 177 |
|
# caller needs to wait after reading or writing (else deadlock) |
| 178 |
|
res = p1.wait() |
| 187 |
|
% (res, self.qjoin(cmdl_2))) |
| 188 |
|
return p1, p2 |
| 189 |
|
|
| 183 |
– |
|
| 190 |
|
def call_many(self, cmdlines, actstr, _in=None, out=None, |
| 191 |
|
universal_newlines=False): |
| 192 |
|
'''Create a series of N processes, chained via pipes, possibly with an |
| 206 |
|
# other than direct call_one(), this returns a one-item tuple! |
| 207 |
|
return (self.call_one(cmdlines[0], actstr, _in=_in, out=out, |
| 208 |
|
universal_newlines=universal_newlines),) |
| 209 |
< |
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 |
| 209 |
> |
stdin, stdout, instr, outstr = self.__parse_args(_in, out) |
| 210 |
|
procs = [] |
| 211 |
|
if getattr(self, 'verbose', None): |
| 212 |
|
sys.stderr.write('### %s \n' % actstr) |
| 213 |
< |
sys.stderr.write(self.qjoin(cmdlines[0]) + ' | ') |
| 213 |
> |
sys.stderr.write(self.qjoin(cmdlines[0]) + instr + ' | ') |
| 214 |
|
if not getattr(self, 'donothing', None): |
| 215 |
|
try: |
| 216 |
|
prevproc = subprocess.Popen(cmdlines[0], stdin=stdin, |
| 217 |
|
stdout=PIPE, stderr=self._stderr, **self._pipeargs) |
| 218 |
|
procs.append(prevproc) |
| 219 |
|
except Exception as e: |
| 220 |
< |
self.raise_on_error(actstr, str(e)) |
| 220 |
> |
self.raise_on_error(actstr, e) |
| 221 |
|
|
| 222 |
|
for cmdl in cmdlines[1:-1]: |
| 223 |
|
if getattr(self, 'verbose', None): |
| 230 |
|
prevproc.stdout.close() |
| 231 |
|
prevproc = nextproc |
| 232 |
|
except Exception as e: |
| 233 |
< |
self.raise_on_error(actstr, str(e)) |
| 233 |
> |
self.raise_on_error(actstr, e) |
| 234 |
|
|
| 235 |
|
if getattr(self, 'verbose', None): |
| 236 |
< |
sys.stderr.write(self.qjoin(cmdlines[-1]) + outendstr) |
| 236 |
> |
sys.stderr.write(self.qjoin(cmdlines[-1]) + outstr + '\n') |
| 237 |
|
if not getattr(self, 'donothing', None): |
| 238 |
|
try: |
| 239 |
|
lastproc = subprocess.Popen(cmdlines[-1], stdin=prevproc.stdout, |
| 243 |
|
procs.append(lastproc) |
| 244 |
|
prevproc.stdout.close() |
| 245 |
|
except Exception as e: |
| 246 |
< |
self.raise_on_error(actstr, str(e)) |
| 246 |
> |
self.raise_on_error(actstr, e) |
| 247 |
|
|
| 248 |
|
if stdin != PIPE and stdout!= PIPE: |
| 249 |
|
# caller needs to wait after reading or writing (else deadlock) |