mbligh | e4e958f | 2009-08-24 22:26:11 +0000 | [diff] [blame] | 1 | # This file must use Python 1.5 syntax. |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 2 | import glob |
| 3 | import os |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 4 | import sys |
mbligh | e4e958f | 2009-08-24 22:26:11 +0000 | [diff] [blame] | 5 | |
| 6 | |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 7 | class check_python_version: |
| 8 | |
| 9 | def __init__(self): |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 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). |
Mike Frysinger | 09f8728 | 2019-11-19 18:02:07 -0500 | [diff] [blame] | 19 | if sys.version_info.major >= 3: |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 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 | |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 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.""" |
Mike Frysinger | 09f8728 | 2019-11-19 18:02:07 -0500 | [diff] [blame] | 32 | # CrOS only ever has Python 2.7 available, so pick whatever matches. |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 33 | pythons = [] |
| 34 | for glob_str in self.PYTHON_BIN_GLOB_STRINGS: |
| 35 | pythons.extend(glob.glob(glob_str)) |
Mike Frysinger | 09f8728 | 2019-11-19 18:02:07 -0500 | [diff] [blame] | 36 | return pythons[0] |
Allen Li | fc6326f | 2017-02-06 13:27:31 -0800 | [diff] [blame] | 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) |