Returns pythons ordered by version from lowest to highest.
(search_arch)
| 250 | |
| 251 | |
| 252 | def search_for_pythons(search_arch): |
| 253 | """Returns pythons ordered by version from lowest to highest.""" |
| 254 | pythons_found = list() |
| 255 | for pattern in PYTHON_SEARCH_PATHS[SYSTEM]: |
| 256 | # Replace env variable in path |
| 257 | match = re.search(r"%(\w+)%", pattern) |
| 258 | if match: |
| 259 | env_key = match.group(1) |
| 260 | if env_key in os.environ: |
| 261 | pattern = pattern.replace(match.group(0), os.environ[env_key]) |
| 262 | else: |
| 263 | print("ERROR: Env variable not found: {env_key}" |
| 264 | .format(env_key=env_key)) |
| 265 | sys.exit(1) |
| 266 | results = glob.glob(pattern) |
| 267 | for path in results: |
| 268 | if os.path.isdir(path): |
| 269 | python = os.path.join(path, |
| 270 | "python{ext}".format(ext=EXECUTABLE_EXT)) |
| 271 | version_code = ("import sys;" |
| 272 | "print(str(sys.version_info[:3]));") |
| 273 | if not os.path.isfile(python): |
| 274 | print("ERROR: Python executable not found: {executable}" |
| 275 | .format(executable=python)) |
| 276 | sys.exit(1) |
| 277 | version_str = subprocess.check_output([python, "-c", |
| 278 | version_code]) |
| 279 | version_str = version_str.strip() |
| 280 | if sys.version_info >= (3, 0): |
| 281 | version_str = version_str.decode("utf-8") |
| 282 | match = re.search("^\((\d+), (\d+), (\d+)\)$", version_str) |
| 283 | assert match, version_str |
| 284 | major = match.group(1) |
| 285 | minor = match.group(2) |
| 286 | micro = match.group(3) |
| 287 | version_tuple2 = (int(major), int(minor)) |
| 288 | version_tuple3 = (int(major), int(minor), int(micro)) |
| 289 | arch_code = ("import platform;" |
| 290 | "print(str(platform.architecture()[0]));") |
| 291 | arch = subprocess.check_output([python, "-c", arch_code]) |
| 292 | arch = arch.strip() |
| 293 | if sys.version_info >= (3, 0): |
| 294 | arch = arch.decode("utf-8") |
| 295 | if version_tuple2 in SUPPORTED_PYTHON_VERSIONS \ |
| 296 | and arch == search_arch: |
| 297 | name = ("Python {major}.{minor}.{micro} {arch}" |
| 298 | .format(major=major, minor=minor, micro=micro, |
| 299 | arch=arch)) |
| 300 | pythons_found.append(dict( |
| 301 | version2=version_tuple2, |
| 302 | version3=version_tuple3, |
| 303 | arch=arch, |
| 304 | executable=python, |
| 305 | name=name)) |
| 306 | ret_pythons = list() |
| 307 | for version_tuple in SUPPORTED_PYTHON_VERSIONS: |
| 308 | supported_python = None |
| 309 | for python in pythons_found: |