Bug 1286799 - Abstract mozboot version checking. draft
authorRalph Giles <giles@mozilla.com>
Tue, 15 Nov 2016 14:31:59 -0800
changeset 439821 e20b05068cf200042411c5518f33455237882b4a
parent 439820 d962e7b65ae57510cbf53814e71f5d4bb34fb679
child 439822 880b5803a8e3d2373764f718cae81abb4c488815
push id36111
push userbmo:giles@thaumas.net
push dateWed, 16 Nov 2016 18:04:14 +0000
bugs1286799
milestone53.0a1
Bug 1286799 - Abstract mozboot version checking. Move version parsing to a helper method so it can be used for more than one executable. MozReview-Commit-ID: 4gOgdgYFbFx
python/mozboot/mozboot/base.py
--- a/python/mozboot/mozboot/base.py
+++ b/python/mozboot/mozboot/base.py
@@ -316,16 +316,43 @@ class BaseBootstrapper(object):
         self.package_manager_updated = True
 
     def _update_package_manager(self):
         """Updates the package manager's manifests/package list.
 
         This should be defined in child classes.
         """
 
+    def _parse_version(self, path, name=None, env=None):
+        '''Execute the given path, returning the version.
+
+        Invokes the path argument with the --version switch
+        and returns a LooseVersion representing the output
+        if successful. If not, returns None.
+
+        An optional name argument gives the expected program
+        name returned as part of the version string, if it's
+        different from the basename of the executable.
+
+        An optional env argument allows modifying environment
+        variable during the invocation to set options, PATH,
+        etc.
+        '''
+        if not name:
+            name = os.path.basename(path)
+
+        info = self.check_output([path, '--version'],
+                                 stderr=subprocess.STDOUT)
+        match = re.search(name + ' ([a-z0-9\.]+)', info)
+        if not match:
+            print('ERROR! Unable to identify %s version.' % name)
+            return None
+
+        return LooseVersion(match.group(1))
+
     def _hgplain_env(self):
         """ Returns a copy of the current environment updated with the HGPLAIN
         environment variable.
 
         HGPLAIN prevents Mercurial from applying locale variations to the output
         making it suitable for use in scripts.
         """
         env = os.environ.copy()
@@ -394,25 +421,20 @@ class BaseBootstrapper(object):
 
         for test in ['python2.7', 'python']:
             python = self.which(test)
             if python:
                 break
 
         assert python
 
-        info = self.check_output([python, '--version'],
-                                 stderr=subprocess.STDOUT)
-        match = re.search('Python ([a-z0-9\.]+)', info)
-        if not match:
-            print('ERROR Unable to identify Python version.')
+        our = self._parse_version(python, 'Python')
+        if not our:
             return False, None
 
-        our = LooseVersion(match.group(1))
-
         return our >= MODERN_PYTHON_VERSION, our
 
     def ensure_python_modern(self):
         modern, version = self.is_python_modern()
 
         if modern:
             print('Your version of Python (%s) is new enough.' % version)
             return