@@ -59,35 +59,50 @@ jobs:
59
59
exit(1)
60
60
EOF
61
61
62
- # Check PyPI version using API
62
+ # Check PyPI version using Python
63
63
- name : Check if version exists on PyPI
64
64
id : check_version
65
65
run : |
66
- PACKAGE_NAME="${{ steps.package_info.outputs.package_name }}"
67
- VERSION="${{ steps.package_info.outputs.version }}"
66
+ python - <<EOF >> "$GITHUB_OUTPUT"
67
+ import requests
68
+ import sys
69
+ import os
70
+ import json
71
+ from packaging import version as packaging_version
72
+
73
+ package_name = os.environ['PACKAGE_NAME']
74
+ current_version = os.environ['VERSION']
68
75
69
- echo "Checking version $VERSION for package $PACKAGE_NAME"
76
+ print(f "Checking version {current_version} for package {package_name}")
70
77
71
- # Use PyPI JSON API to check version
72
- HTTP_STATUS=$(curl -s -o response.json -w "%{http_code}" "https://pypi.org/pypi/$PACKAGE_NAME/json" )
78
+ url = f"https://pypi.org/pypi/{package_name}/json"
79
+ response = requests.get(url )
73
80
74
- if [ "$HTTP_STATUS" -eq 200 ]; then
75
- # Check if version exists in releases
76
- if cat response.json | python -c "import sys, json; data = json.load(sys.stdin); sys.exit(0 if '$VERSION' in data['releases'] else 1)"; then
77
- echo "Version $VERSION already exists on PyPI"
78
- echo "should_publish=false" >> "$GITHUB_OUTPUT"
79
- else
80
- echo "Version $VERSION is new"
81
- echo "should_publish=true" >> "$GITHUB_OUTPUT"
82
- fi
83
- elif [ "$HTTP_STATUS" -eq 404 ]; then
84
- # Package doesn't exist on PyPI yet
85
- echo "Package not found on PyPI - this is a new package"
86
- echo "should_publish=true" >> "$GITHUB_OUTPUT"
87
- else
88
- echo "Error checking PyPI API: HTTP status $HTTP_STATUS"
89
- exit 1
90
- fi
81
+ if response.status_code == 200:
82
+ data = response.json()
83
+ releases = data.get('releases', {})
84
+
85
+ # Normalize the version for comparison
86
+ current_ver = packaging_version.parse(current_version)
87
+
88
+ # Check if version exists
89
+ if str(current_ver) in releases:
90
+ print("Version already exists on PyPI")
91
+ print("should_publish=false")
92
+ else:
93
+ print("Version is new")
94
+ print("should_publish=true")
95
+
96
+ elif response.status_code == 404:
97
+ print("Package not found on PyPI - this is a new package")
98
+ print("should_publish=true")
99
+ else:
100
+ print(f"Error checking PyPI API: HTTP status {response.status_code}")
101
+ sys.exit(1)
102
+ EOF
103
+ env :
104
+ PACKAGE_NAME : ${{ steps.package_info.outputs.package_name }}
105
+ VERSION : ${{ steps.package_info.outputs.version }}
91
106
92
107
- name : Publish to PyPI
93
108
if : steps.check_version.outputs.should_publish == 'true'
0 commit comments