Prevent Python stack trace when an external command failed.

This catches the OSError thrown by the subprocess module and wraps it so that in the end only an error message is printed, explaining which command failed.
This commit is contained in:
Michael Schwarz 2015-09-07 00:53:22 +02:00
parent ab26e5a8d5
commit 57c9e9dc90

View file

@ -2,7 +2,8 @@ import contextlib, subprocess, tempfile, shutil, re, os
class UserError(Exception): class UserError(Exception):
pass def __init__(self, message, *args):
super(UserError, self).__init__(message.format(*args))
def rename_atomic(source_path, target_path): def rename_atomic(source_path, target_path):
@ -43,11 +44,14 @@ def command(args, remove_env = [], set_env = { }):
for k, v in set_env.items(): for k, v in set_env.items():
env[k] = v env[k] = v
process = subprocess.Popen(args, env = env) try:
process.wait() process = subprocess.Popen(args, env = env)
process.wait()
except OSError as e:
raise UserError('Error running {}: {}', args[0], e)
if process.returncode: if process.returncode:
raise UserError('Command failed: {}'.format(' '.join(args))) raise UserError('Command failed: {}', ' '.join(args))
def bash_escape_string(string): def bash_escape_string(string):