Bug 1401309 - [mozversioncontrol] Add an option to make failed subprocess commands non-fatal, r?gps
Sometimes commands return non-zero even though everything is ok. For example,
'hg outgoing' returns 1 if there are no outgoing files. This adds a way for
specific function calls not to abort if something goes wrong. Instead, stderr
will be printed (if any) and an empty string is returned.
MozReview-Commit-ID: E089djeHrmr
--- a/python/mozversioncontrol/mozversioncontrol/__init__.py
+++ b/python/mozversioncontrol/mozversioncontrol/__init__.py
@@ -77,20 +77,28 @@ class Repository(object):
self._valid_diff_filter = ('m', 'a', 'd')
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_tb):
pass
- def _run(self, *args):
- return subprocess.check_output((self._tool, ) + args,
- cwd=self.path,
- env=self._env)
+ def _run(self, *args, **runargs):
+ return_codes = runargs.get('return_codes', [])
+
+ cmd = (self._tool,) + args
+ try:
+ return subprocess.check_output(cmd,
+ cwd=self.path,
+ env=self._env)
+ except subprocess.CalledProcessError as e:
+ if e.returncode in return_codes:
+ return ''
+ raise
@property
def tool_version(self):
'''Return the version of the VCS tool in use as a `LooseVersion`.'''
if self._version:
return self._version
info = self._run('--version').strip()
match = re.search('version ([^\+\)]+)', info)
@@ -261,17 +269,17 @@ class HgRepository(Repository):
if 'a' in df:
template += "{file_adds % '\\n{file}'}"
if 'd' in df:
template += "{file_dels % '\\n{file}'}"
if 'm' in df:
template += "{file_mods % '\\n{file}'}"
return self._run('outgoing', '-r', '.', '--quiet',
- '--template', template, upstream).split()
+ '--template', template, upstream, return_codes=(1,)).split()
def add_remove_files(self, path):
args = ['addremove', path]
if self.tool_version >= b'3.9':
args = ['--config', 'extensions.automv='] + args
self._run(*args)
def forget_add_remove_files(self, path):