Bug 1401309 - [mozversioncontrol] Add an option to make failed subprocess commands non-fatal, r?gps draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Mon, 25 Sep 2017 16:30:13 -0400
changeset 671958 ea4c86bcb01c486e947c71164e69c707895abdb3
parent 671957 770fe8a9981a39d2891913b297710e6b676a4c6f
child 671959 e350889b001a917c8db4df8298c31d27c7c27899
push id82100
push userahalberstadt@mozilla.com
push dateThu, 28 Sep 2017 14:54:56 +0000
reviewersgps
bugs1401309
milestone58.0a1
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
python/mozversioncontrol/mozversioncontrol/__init__.py
--- 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):