|
3 | 3 | import argparse |
4 | 4 | import json |
5 | 5 | import ssl |
| 6 | +import sys |
6 | 7 | import urllib.request |
7 | 8 |
|
8 | 9 | import certifi |
9 | 10 | from packaging.specifiers import SpecifierSet |
10 | 11 | from packaging.version import Version |
11 | 12 |
|
12 | 13 |
|
| 14 | +def has_python_version(version, data): |
| 15 | + for d in data: |
| 16 | + try: |
| 17 | + if Version(d["python_version"][2:]) == version: |
| 18 | + return True |
| 19 | + except Exception: |
| 20 | + pass |
| 21 | + return False |
| 22 | + |
| 23 | + |
13 | 24 | def get_pandas_versions(pandas_range): |
14 | 25 | url = "https://pypi.org/pypi/pandas/json" |
15 | 26 | ssl_context = ssl.create_default_context(cafile=certifi.where()) |
16 | | - with urllib.request.urlopen(url, context=ssl_context) as response: |
| 27 | + python_version = Version( |
| 28 | + str(sys.version_info.major) + str(sys.version_info.minor) |
| 29 | + ) |
| 30 | + # Set a timeout for the request to avoid hanging |
| 31 | + timeout = 10 # seconds |
| 32 | + |
| 33 | + # Try to fetch pandas versions from PyPI |
| 34 | + with urllib.request.urlopen( |
| 35 | + url, timeout=timeout, context=ssl_context |
| 36 | + ) as response: |
17 | 37 | data = json.loads(response.read()) |
| 38 | + |
| 39 | + # Extract and filter versions |
18 | 40 | versions = [Version(v) for v in data["releases"]] |
| 41 | + |
19 | 42 | specifier = SpecifierSet(pandas_range.lstrip("pandas")) |
20 | | - matching_versions = [v for v in versions if v in specifier] |
| 43 | + matching_versions = [ |
| 44 | + v |
| 45 | + for v in versions |
| 46 | + if v in specifier |
| 47 | + and has_python_version(python_version, data["releases"][str(v)]) |
| 48 | + ] |
| 49 | + |
21 | 50 | matching_minors = sorted( |
22 | 51 | set(".".join((str(v.major), str(v.minor))) for v in matching_versions), |
23 | 52 | key=Version, |
24 | 53 | ) |
| 54 | + |
25 | 55 | return matching_minors |
26 | 56 |
|
27 | 57 |
|
|
0 commit comments