xref: /aosp_15_r20/external/autotest/client/common_lib/check_version.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# This file must use Python 1.5 syntax.
2import glob
3import logging
4import os
5import re
6import sys
7
8PY_GLOBS = {
9        3: ['/usr/bin/python3*', '/usr/local/bin/python3*'],
10        2: ['/usr/bin/python2*', '/usr/local/bin/python2*']
11}
12
13
14class check_python_version:
15
16    def __init__(self, desired_version=3):
17        # In order to ease the migration to Python3, disable the restart logic
18        # when AUTOTEST_NO_RESTART is set. This makes it possible to run
19        # autotest locally as Python3 before any other environment is switched
20        # to Python3.
21        if os.getenv("AUTOTEST_NO_RESTART"):
22            return
23        self.desired_version = desired_version
24
25        # The change to prefer 2.4 really messes up any systems which have both
26        # the new and old version of Python, but where the newer is default.
27        # This is because packages, libraries, etc are all installed into the
28        # new one by default. Some things (like running under mod_python) just
29        # plain don't handle python restarting properly. I know that I do some
30        # development under ipython and whenever I run (or do anything that
31        # runs) 'import common' it restarts my shell. Overall, the change was
32        # fairly annoying for me (and I can't get around having 2.4 and 2.5
33        # installed with 2.5 being default).
34        if sys.version_info.major != self.desired_version:
35            try:
36                # We can't restart when running under mod_python.
37                from mod_python import apache
38            except ImportError:
39                self.restart()
40
41    def extract_version(self, path):
42        """Return a matching python version to the provided path."""
43        match = re.search(r'/python(\d+)\.(\d+)$', path)
44        if match:
45            return (int(match.group(1)), int(match.group(2)))
46        else:
47            return None
48
49    def find_desired_python(self):
50        """Returns the path of the desired python interpreter."""
51        pythons = []
52        for glob_str in PY_GLOBS[self.desired_version]:
53            pythons.extend(glob.glob(glob_str))
54
55        possible_versions = []
56        for python in pythons:
57            version = self.extract_version(python)
58            if not version:
59                continue
60            # Autotest in Python2 is written to 2.4 and above.
61            if self.desired_version == 2:
62                if version < (2, 4):
63                    continue
64            if self.desired_version == 3:
65                # Autotest in Python3 is written to 3.6 and above.
66                if version < (3, 6):
67                    continue
68            possible_versions.append((version, python))
69
70        possible_versions.sort()
71
72        if not possible_versions:
73            raise ValueError('Python %s.x not found' % self.desired_version)
74
75        # Return the lowest compatible major version possible
76        return possible_versions[0][1]
77
78    def restart(self):
79        python = self.find_desired_python()
80        sys.stderr.write('NOTE: %s switching to %s\n' %
81                         (os.path.basename(sys.argv[0]), python))
82        sys.argv.insert(0, '-u')
83        sys.argv.insert(0, python)
84        os.execv(sys.argv[0], sys.argv)
85