-
Notifications
You must be signed in to change notification settings - Fork 126
Support date-based PyPI versioning and dev packages #2806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Script to calculate the version for the HEIR Python package.""" | ||
|
|
||
| import argparse | ||
| import datetime | ||
| import os | ||
| import re | ||
| import sys | ||
| import requests | ||
|
|
||
|
|
||
| def get_pypi_versions(package_name): | ||
| """Fetch all published versions for a package from PyPI.""" | ||
| url = f"https://pypi.org/pypi/{package_name}/json" | ||
| try: | ||
| response = requests.get(url, timeout=10) | ||
| if response.status_code == 200: | ||
| data = response.json() | ||
| return list(data.get("releases", {}).keys()) | ||
| except Exception as e: | ||
| print(f"Error fetching from PyPI: {e}", file=sys.stderr) | ||
| return [] | ||
|
|
||
|
|
||
| def get_next_dev_version(package_name): | ||
| """Calculate the next .devN version for today's date.""" | ||
| today = datetime.datetime.now(datetime.timezone.utc).strftime("%Y.%m.%d") | ||
| versions = get_pypi_versions(package_name) | ||
|
|
||
| # Find versions matching today's date and the .dev suffix | ||
| pattern = re.compile(rf"^{re.escape(today)}\.dev(\d+)$") | ||
| max_dev = -1 | ||
|
|
||
| for v in versions: | ||
| match = pattern.match(v) | ||
| if match: | ||
| max_dev = max(max_dev, int(match.group(1))) | ||
|
|
||
| next_dev = max_dev + 1 | ||
| return f"{today}.dev{next_dev}" | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Calculate HEIR package version." | ||
| ) | ||
| parser.add_argument( | ||
| "--event", | ||
| default="workflow_dispatch", | ||
| help="GitHub event name (e.g., release, workflow_dispatch)", | ||
| ) | ||
| parser.add_argument( | ||
| "--ref", | ||
| default="refs/heads/main", | ||
| help="GitHub ref (e.g., refs/heads/main)", | ||
| ) | ||
| parser.add_argument("--tag", help="Release tag name") | ||
| parser.add_argument("--package", default="heir_py", help="PyPI package name") | ||
| parser.add_argument( | ||
| "--gha", action="store_true", help="Output for GitHub Actions" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| version = "" | ||
| should_publish = "false" | ||
|
|
||
| if args.event == "release": | ||
| if args.tag: | ||
| version = args.tag.lstrip("v") | ||
| should_publish = "true" | ||
| elif args.event == "workflow_dispatch" and args.ref == "refs/heads/main": | ||
| version = get_next_dev_version(args.package) | ||
| should_publish = "true" | ||
| else: | ||
| # For PRs or other events, we let setuptools-scm handle it automatically | ||
| # or we don't publish. | ||
| should_publish = "false" | ||
|
|
||
| if args.gha: | ||
| # Writing to GITHUB_OUTPUT if available | ||
| output_file = os.environ.get("GITHUB_OUTPUT") | ||
| if output_file: | ||
| with open(output_file, "a") as f: | ||
| f.write(f"version={version}\n") | ||
| f.write(f"should_publish={should_publish}\n") | ||
|
|
||
| print(f"version={version}") | ||
| print(f"should_publish={should_publish}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.