7 | 7 |
import sys
|
8 | 8 |
import time
|
9 | 9 |
import timeit
|
|
10 |
import warnings
|
10 | 11 |
from multiprocessing import Pipe
|
11 | 12 |
from threading import Thread
|
12 | 13 |
|
|
300 | 301 |
return command
|
301 | 302 |
|
302 | 303 |
|
303 | |
class ReturnObject(dict, subprocess.CompletedProcess):
|
|
304 |
class ReturnObject(subprocess.CompletedProcess):
|
304 | 305 |
"""
|
305 | 306 |
A subprocess.CompletedProcess-like object containing the executed
|
306 | 307 |
command, stdout and stderr (both as bytestrings), and the exitcode.
|
|
310 | 311 |
exited with a non-zero exit code.
|
311 | 312 |
"""
|
312 | 313 |
|
313 | |
def __init__(self, *arg, **kw):
|
314 | |
super().__init__(*arg, **kw)
|
315 | |
self.args = self["command"]
|
316 | |
self.returncode = self["exitcode"]
|
317 | |
self.stdout = self["stdout"]
|
318 | |
self.stderr = self["stderr"]
|
|
314 |
def __init__(self, exitcode=None, command=None, stdout=None, stderr=None, **kw):
|
|
315 |
super().__init__(
|
|
316 |
args=command, returncode=exitcode, stdout=stdout, stderr=stderr
|
|
317 |
)
|
|
318 |
self._extras = {
|
|
319 |
"timeout": kw.get("timeout"),
|
|
320 |
"runtime": kw.get("runtime"),
|
|
321 |
"time_start": kw.get("time_start"),
|
|
322 |
"time_end": kw.get("time_end"),
|
|
323 |
}
|
|
324 |
|
|
325 |
def __getitem__(self, key):
|
|
326 |
warnings.warn(
|
|
327 |
"dictionary access to a procrunner return object is deprecated",
|
|
328 |
DeprecationWarning,
|
|
329 |
stacklevel=2,
|
|
330 |
)
|
|
331 |
if key in self._extras:
|
|
332 |
return self._extras[key]
|
|
333 |
if not hasattr(self, key):
|
|
334 |
raise KeyError(f"Unknown attribute {key}")
|
|
335 |
return getattr(self, key)
|
319 | 336 |
|
320 | 337 |
def __eq__(self, other):
|
321 | 338 |
"""Override equality operator to account for added fields"""
|
|
327 | 344 |
"""This object is not immutable, so mark it as unhashable"""
|
328 | 345 |
return None
|
329 | 346 |
|
330 | |
def __ne__(self, other):
|
331 | |
"""Overrides the default implementation (unnecessary in Python 3)"""
|
332 | |
return not self.__eq__(other)
|
|
347 |
@property
|
|
348 |
def cmd(self):
|
|
349 |
warnings.warn(
|
|
350 |
"procrunner return object .cmd is deprecated, use .args",
|
|
351 |
DeprecationWarning,
|
|
352 |
stacklevel=2,
|
|
353 |
)
|
|
354 |
return self.args
|
|
355 |
|
|
356 |
@property
|
|
357 |
def command(self):
|
|
358 |
warnings.warn(
|
|
359 |
"procrunner return object .command is deprecated, use .args",
|
|
360 |
DeprecationWarning,
|
|
361 |
stacklevel=2,
|
|
362 |
)
|
|
363 |
return self.args
|
|
364 |
|
|
365 |
@property
|
|
366 |
def exitcode(self):
|
|
367 |
warnings.warn(
|
|
368 |
"procrunner return object .exitcode is deprecated, use .returncode",
|
|
369 |
DeprecationWarning,
|
|
370 |
stacklevel=2,
|
|
371 |
)
|
|
372 |
return self.returncode
|
|
373 |
|
|
374 |
@property
|
|
375 |
def timeout(self):
|
|
376 |
warnings.warn(
|
|
377 |
"procrunner return object .timeout is deprecated",
|
|
378 |
DeprecationWarning,
|
|
379 |
stacklevel=2,
|
|
380 |
)
|
|
381 |
return self._extras["timeout"]
|
|
382 |
|
|
383 |
@property
|
|
384 |
def runtime(self):
|
|
385 |
warnings.warn(
|
|
386 |
"procrunner return object .runtime is deprecated",
|
|
387 |
DeprecationWarning,
|
|
388 |
stacklevel=2,
|
|
389 |
)
|
|
390 |
return self._extras["runtime"]
|
|
391 |
|
|
392 |
@property
|
|
393 |
def time_start(self):
|
|
394 |
warnings.warn(
|
|
395 |
"procrunner return object .time_start is deprecated",
|
|
396 |
DeprecationWarning,
|
|
397 |
stacklevel=2,
|
|
398 |
)
|
|
399 |
return self._extras["time_start"]
|
|
400 |
|
|
401 |
@property
|
|
402 |
def time_end(self):
|
|
403 |
warnings.warn(
|
|
404 |
"procrunner return object .time_end is deprecated",
|
|
405 |
DeprecationWarning,
|
|
406 |
stacklevel=2,
|
|
407 |
)
|
|
408 |
return self._extras["time_end"]
|
|
409 |
|
|
410 |
def update(self, dictionary):
|
|
411 |
self._extras.update(dictionary)
|
333 | 412 |
|
334 | 413 |
|
335 | 414 |
def run(
|
|
521 | 600 |
time_end = time.strftime("%Y-%m-%d %H:%M:%S GMT", time.gmtime())
|
522 | 601 |
|
523 | 602 |
result = ReturnObject(
|
524 | |
{
|
525 | |
"exitcode": p.returncode,
|
526 | |
"command": command,
|
527 | |
"stdout": stdout,
|
528 | |
"stderr": stderr,
|
529 | |
"timeout": timeout_encountered,
|
530 | |
"runtime": runtime,
|
531 | |
"time_start": time_start,
|
532 | |
"time_end": time_end,
|
533 | |
}
|
|
603 |
exitcode=p.returncode,
|
|
604 |
command=command,
|
|
605 |
stdout=stdout,
|
|
606 |
stderr=stderr,
|
|
607 |
timeout=timeout_encountered,
|
|
608 |
runtime=runtime,
|
|
609 |
time_start=time_start,
|
|
610 |
time_end=time_end,
|
534 | 611 |
)
|
535 | 612 |
if stdin is not None:
|
536 | 613 |
result.update(
|