• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# This file must use Python 1.5 syntax.
2import glob
3import os
4import sys
5
6
7class check_python_version:
8
9    def __init__(self):
10        # The change to prefer 2.4 really messes up any systems which have both
11        # the new and old version of Python, but where the newer is default.
12        # This is because packages, libraries, etc are all installed into the
13        # new one by default. Some things (like running under mod_python) just
14        # plain don't handle python restarting properly. I know that I do some
15        # development under ipython and whenever I run (or do anything that
16        # runs) 'import common' it restarts my shell. Overall, the change was
17        # fairly annoying for me (and I can't get around having 2.4 and 2.5
18        # installed with 2.5 being default).
19        if sys.version_info.major >= 3:
20            try:
21                # We can't restart when running under mod_python.
22                from mod_python import apache
23            except ImportError:
24                self.restart()
25
26
27    PYTHON_BIN_GLOB_STRINGS = ['/usr/bin/python2*', '/usr/local/bin/python2*']
28
29
30    def find_desired_python(self):
31        """Returns the path of the desired python interpreter."""
32        # CrOS only ever has Python 2.7 available, so pick whatever matches.
33        pythons = []
34        for glob_str in self.PYTHON_BIN_GLOB_STRINGS:
35            pythons.extend(glob.glob(glob_str))
36        return pythons[0]
37
38
39    def restart(self):
40        python = self.find_desired_python()
41        sys.stderr.write('NOTE: %s switching to %s\n' %
42                         (os.path.basename(sys.argv[0]), python))
43        sys.argv.insert(0, '-u')
44        sys.argv.insert(0, python)
45        os.execv(sys.argv[0], sys.argv)
46