Bug 1286799 - mozboot: Abstract version checking. r=gps draft
authorRalph Giles <giles@mozilla.com>
Tue, 15 Nov 2016 14:31:59 -0800
changeset 442120 5172cf24453d0e43824d38c55f083eab49384679
parent 442068 0534254e9a40b4bade2577c631fe4cfa0b5db41d
child 442121 006d347050789dd4fdd370a1267d17067365cd66
push id36589
push userbmo:giles@thaumas.net
push dateMon, 21 Nov 2016 19:39:40 +0000
reviewersgps
bugs1286799
milestone53.0a1
Bug 1286799 - mozboot: Abstract version checking. r=gps 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