|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | import argparse |
| 4 | +import json |
4 | 5 | import re |
5 | 6 | from urllib import request |
6 | 7 |
|
|
15 | 16 | } |
16 | 17 |
|
17 | 18 |
|
18 | | -def get_min_python_version(): |
| 19 | +def get_all_python_versions(): |
19 | 20 | """ |
20 | | - Find the minimum version of Python supported (i.e., not end-of-life) |
| 21 | + Retrieve all supported Python versions (i.e., not end-of-life) |
21 | 22 | """ |
22 | | - min_python = ( |
| 23 | + python_versions = ( |
23 | 24 | pd.read_html( |
24 | 25 | "https://devguide.python.org/versions/", |
25 | 26 | storage_options=HEADERS, |
26 | 27 | )[0] |
27 | | - .iloc[-1] |
28 | | - .Branch |
| 28 | + .query('Branch != "main"') |
| 29 | + .Branch.to_list() |
29 | 30 | ) |
| 31 | + return python_versions |
| 32 | + |
| 33 | + |
| 34 | +def get_all_python_versions_in_range(start, stop): |
| 35 | + """ |
| 36 | + Find all Python minor versions within some start/stop range (inclusive) |
| 37 | + """ |
| 38 | + python_versions = get_all_python_versions()[::-1] # Reverse order |
| 39 | + start_idx = python_versions.index(start) |
| 40 | + stop_idx = python_versions.index(stop) |
| 41 | + print(json.dumps(python_versions[start_idx : stop_idx + 1])) |
| 42 | + |
| 43 | + |
| 44 | +def get_min_python_version(): |
| 45 | + """ |
| 46 | + Find the minimum version of Python supported (i.e., not end-of-life) |
| 47 | + """ |
| 48 | + min_python = get_all_python_versions()[-1] |
30 | 49 | return min_python |
31 | 50 |
|
32 | 51 |
|
@@ -425,24 +444,32 @@ def get_all_min_versions(MIN_PYTHON): |
425 | 444 | if __name__ == "__main__": |
426 | 445 | parser = argparse.ArgumentParser() |
427 | 446 | parser.add_argument( |
428 | | - "-mode", type=str, default="min", help='Options: ["min", "max"]' |
| 447 | + "-mode", type=str, default="min", help='Options: ["min", "max", "range"]' |
429 | 448 | ) |
430 | | - parser.add_argument("python_version", nargs="?", default=None) |
| 449 | + parser.add_argument("python_version", nargs="*", default=None) |
431 | 450 | args = parser.parse_args() |
432 | 451 | # Example |
433 | 452 | # ./versions.py |
434 | 453 | # ./versions.py 3.11 |
435 | 454 | # ./versions.py -mode max |
436 | | - |
437 | | - print(f"mode: {args.mode}") |
| 455 | + # /versions.py -mode range 3.10 3.14 |
438 | 456 |
|
439 | 457 | if args.mode == "min": |
| 458 | + if len(args.python_version) == 0: |
| 459 | + args.python_version = None |
| 460 | + elif len(args.python_version) == 1: |
| 461 | + args.python_version = args.python_version[0] |
| 462 | + |
440 | 463 | if args.python_version is not None: |
441 | 464 | MIN_PYTHON = str(args.python_version) |
442 | 465 | else: |
443 | 466 | MIN_PYTHON = get_min_python_version() |
444 | 467 | get_all_min_versions(MIN_PYTHON) |
445 | 468 | elif args.mode == "max": |
446 | 469 | get_all_max_versions() |
| 470 | + elif args.mode == "range": |
| 471 | + start = args.python_version[0] |
| 472 | + stop = args.python_version[1] |
| 473 | + get_all_python_versions_in_range(start, stop) |
447 | 474 | else: |
448 | 475 | raise ValueError(f'Unrecognized mode: "{args.mode}"') |
0 commit comments