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
--- 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