blob: 1a6aa6c39db2a4019c13e11caa2b4fe0575cf791 [file] [log] [blame]
mblighe4e958f2009-08-24 22:26:11 +00001# This file must use Python 1.5 syntax.
Allen Lifc6326f2017-02-06 13:27:31 -08002import glob
3import os
Allen Lifc6326f2017-02-06 13:27:31 -08004import sys
mblighe4e958f2009-08-24 22:26:11 +00005
6
Allen Lifc6326f2017-02-06 13:27:31 -08007class check_python_version:
8
9 def __init__(self):
Allen Lifc6326f2017-02-06 13:27:31 -080010 # 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).
Mike Frysinger09f87282019-11-19 18:02:07 -050019 if sys.version_info.major >= 3:
Allen Lifc6326f2017-02-06 13:27:31 -080020 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
Allen Lifc6326f2017-02-06 13:27:31 -080027 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."""
Mike Frysinger09f87282019-11-19 18:02:07 -050032 # CrOS only ever has Python 2.7 available, so pick whatever matches.
Allen Lifc6326f2017-02-06 13:27:31 -080033 pythons = []
34 for glob_str in self.PYTHON_BIN_GLOB_STRINGS:
35 pythons.extend(glob.glob(glob_str))
Mike Frysinger09f87282019-11-19 18:02:07 -050036 return pythons[0]
Allen Lifc6326f2017-02-06 13:27:31 -080037
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)