Skip to content

Commit 51d4a58

Browse files
committed
Updated check version script to check on pypi test also
1 parent 3a0313b commit 51d4a58

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

check_version.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11
import os
2+
import sys
23

34
import requests
4-
from spam_detector_ai import __version__, __package_name__, __test_version__
55

6+
from spam_detector_ai import __package_name__, __test_version__, __version__
67

7-
def check_package_version(package_name, current_version):
8-
version_exists = False
98

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.
1511
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.
2117
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
2343
if "GITHUB_OUTPUT" in os.environ:
2444
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
2545
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")
2648

2749

2850
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

Comments
 (0)