def run(command=None, args=None, output=None, image_id=None, environ=None, cwd=None):
"""Run a command and return the output. Supports string and py.path paths.
:raise CalledProcessError: Command exits non-zero.
:param iter command: Command to run.
:param iter args: List of command line arguments to insert to command.
:param str output: Path to bind mount to /output when `command` is None.
:param str image_id: Docker image to use instead of robpol86/makemkv.
:param dict environ: Environment variables to set/override in the command.
:param str cwd: Current working directory. Default is tests directory.
:return: Command stdout and stderr output.
:rtype: tuple
"""
if command is None:
command = ['docker', 'run', '--device=/dev/cdrom', '-e', 'DEBUG=true', image_id or 'robpol86/makemkv']
if output:
command = command[:-1] + ['-v', '{}:/output'.format(output)] + command[-1:]
if args:
command = command[:-1] + list(args) + command[-1:]
env = os.environ.copy()
if environ:
env.update(environ)
# Simulate stdin pty so 'docker run -it' doesn't complain.
if command[0] in ('bash', 'docker'):
master, slave = pty.openpty()
else:
master, slave = 0, 0
# Determine timeout.
if command[0] in ('docker',):
timeout = 120
else:
timeout = 30
# Run command.
try:
result = subprocess.run(
[str(i) for i in command], check=True, cwd=cwd or HERE, env=env,
stderr=subprocess.PIPE, stdin=slave or None, stdout=subprocess.PIPE,
timeout=timeout
)
finally:
if slave:
os.close(slave)
if master:
os.close(master)
return result.stdout, result.stderr
评论列表
文章目录