|
1 | 1 | import os
|
| 2 | +import sys |
2 | 3 |
|
3 | 4 | import requests
|
4 |
| -from spam_detector_ai import __version__, __package_name__, __test_version__ |
5 | 5 |
|
| 6 | +from spam_detector_ai import __package_name__, __test_version__, __version__ |
6 | 7 |
|
7 |
| -def check_package_version(package_name, current_version): |
8 |
| - version_exists = False |
9 | 8 |
|
10 |
| - if __test_version__: |
11 |
| - print("Test version, skipping PyPI check.") |
12 |
| - version_exists = True |
13 |
| - else: |
14 |
| - response = requests.get(f"https://pypi.org/pypi/{package_name}/json") |
| 9 | +def check_package_version(package_name, current_version, github_ref_=None): |
| 10 | + """Check if the given package version already exists on PyPI or TestPyPI. |
15 | 11 |
|
16 |
| - if response.status_code == 200: |
17 |
| - released_versions = response.json()["releases"].keys() |
18 |
| - if current_version in released_versions: |
19 |
| - print(f"Version {current_version} already exists on PyPI!") |
20 |
| - version_exists = True |
| 12 | + :param: package_name (str): The name of the package to check. |
| 13 | + :param: current_version (str): The current version of the package. |
| 14 | + :param: github_ref_ (str, optional): The GitHub reference (branch or tag name). |
| 15 | + If it contains 'test', checks against TestPyPI, otherwise PyPI. |
| 16 | + Defaults to None, in which case the check is against PyPI. |
21 | 17 |
|
22 |
| - # Output for GitHub Actions using environment files |
| 18 | + Sets environment variables for GitHub Actions: |
| 19 | + - version_exists: True if the version already exists, False otherwise. |
| 20 | + - publish_to_pypi: True if eligible for publishing to PyPI, False otherwise. |
| 21 | + - publish_to_testpypi: True if eligible for publishing to TestPyPI, False otherwise. |
| 22 | + """ |
| 23 | + version_exists = False |
| 24 | + publish_to_pypi = False |
| 25 | + publish_to_testpypi = False |
| 26 | + |
| 27 | + # Determine if this is a test version based on the GitHub ref or __test_version__ |
| 28 | + is_test_version = "test" in github_ref_ if github_ref_ else __test_version__ |
| 29 | + pypi_url = "https://test.pypi.org/pypi/" if is_test_version else "https://pypi.org/pypi/" |
| 30 | + |
| 31 | + # Check if the current version exists on the respective PyPI repository |
| 32 | + response = requests.get(f"{pypi_url}{package_name}/json") |
| 33 | + if response.status_code == 200: |
| 34 | + released_versions = response.json()["releases"].keys() |
| 35 | + if current_version in released_versions: |
| 36 | + print(f"Version {current_version} already exists on {'TestPyPI' if is_test_version else 'PyPI'}!") |
| 37 | + version_exists = True |
| 38 | + else: |
| 39 | + publish_to_pypi = not is_test_version |
| 40 | + publish_to_testpypi = is_test_version |
| 41 | + |
| 42 | + # Set environment variables for GitHub Actions |
23 | 43 | if "GITHUB_OUTPUT" in os.environ:
|
24 | 44 | with open(os.environ["GITHUB_OUTPUT"], "a") as f:
|
25 | 45 | f.write(f"version_exists={version_exists}\n")
|
| 46 | + f.write(f"publish_to_pypi={publish_to_pypi}\n") |
| 47 | + f.write(f"publish_to_testpypi={publish_to_testpypi}\n") |
26 | 48 |
|
27 | 49 |
|
28 | 50 | if __name__ == "__main__":
|
29 |
| - check_package_version(__package_name__, __version__) |
| 51 | + # Get the GitHub ref from the command line argument, if provided |
| 52 | + github_ref = sys.argv[1] if len(sys.argv) > 1 else None |
| 53 | + check_package_version(__package_name__, __version__, github_ref) |
0 commit comments