Bug 1286799 - mozboot: Abstract version checking. r?glandium draft
authorRalph Giles <giles@mozilla.com>
Tue, 15 Nov 2016 14:31:59 -0800
changeset 440670 03d9e91e4c927b89acbab4748ed04932112968ed
parent 440454 05e5b12f41df270b31955ff7e6d09245c1f83a7a
child 440671 87ff08530e0f20367e65041580b44b91a58e15ed
push id36292
push userbmo:giles@thaumas.net
push dateFri, 18 Nov 2016 00:14:02 +0000
reviewersglandium
bugs1286799
milestone53.0a1
Bug 1286799 - mozboot: Abstract version checking. r?glandium 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,39 @@ 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):
+        '''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.
+        '''
+        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 +417,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