check_version.py revision 9f3660fabcee422e88c0cd5afe8ab8c5f6ed1e47
1# This file must use Python 1.5 syntax. 2import sys, string, os, glob, re 3 4 5def extract_version(path): 6 match = re.search(r'/python(\d+)\.(\d+)$', path) 7 if match: 8 return (int(match.group(1)), int(match.group(2))) 9 else: 10 return None 11 12 13def find_desired_python(): 14 """Returns the path of the desired python interpreter.""" 15 pythons = [] 16 pythons.extend(glob.glob('/usr/bin/python2*')) 17 pythons.extend(glob.glob('/usr/local/bin/python2*')) 18 19 possible_versions = [] 20 best_python = (0, 0), '' 21 for python in pythons: 22 version = extract_version(python) 23 if version >= (2, 4): 24 possible_versions.append((version, python)) 25 26 possible_versions.sort() 27 28 if not possible_versions: 29 raise ValueError('Python 2.x version 2.4 or better is required') 30 # Return the lowest possible version so that we use 2.4 if available 31 # rather than more recent versions. This helps make sure all code is 32 # compatible with 2.4 when developed on more recent systems with 2.5 or 33 # 2.6 installed. 34 return possible_versions[0][1] 35 36 37def restart(): 38 python = find_desired_python() 39 sys.argv.insert(0, '-u') 40 sys.argv.insert(0, python) 41 os.execv(sys.argv[0], sys.argv) 42 43 44def check_python_version(): 45 version = None 46 try: 47 version = sys.version_info[0:2] 48 except AttributeError: 49 pass # pre 2.0, no neat way to get the exact number 50 if not version or version != (2, 4): 51 restart() 52