-
Notifications
You must be signed in to change notification settings - Fork 1
CI job to publish to PyPI #19
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
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e0484df
update publish job
ejfine df1044f
switch to test pypi
ejfine 626e2e5
remove ca
ejfine 44c3879
git tag
ejfine 5f5a500
test staging
ejfine 799ca6e
syntax
ejfine 8835e68
whitespace
ejfine 3b6b7f1
github
ejfine 4f7a6b3
echo
ejfine 48ebebb
actualy echo
ejfine bd5a320
ordering
ejfine f84b305
sleep
ejfine dd36c64
debug
ejfine 7dcb2db
get values
ejfine b8c685e
remove debug
ejfine a82888e
wip
ejfine 7754948
copier
ejfine 9a98010
snake
ejfine d0ae779
tag
ejfine 17c0a8a
readme
ejfine 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,95 @@ | ||
| import argparse | ||
| import subprocess | ||
| import tomllib | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def extract_version(toml_path: Path | str) -> str: | ||
| """Load toml_path and return the version string. | ||
|
|
||
| Checks [project].version (PEP 621) first, then [tool.poetry].version. Raises KeyError if no version field is found. | ||
| """ | ||
| path = Path(toml_path) | ||
| with path.open("rb") as f: | ||
| data = tomllib.load(f) | ||
|
|
||
| project = data.get("project", {}) | ||
| if version := project.get("version"): | ||
| return version | ||
|
|
||
| tool = data.get("tool", {}) | ||
| poetry = tool.get("poetry", {}) | ||
| if version := poetry.get("version"): | ||
| return version | ||
|
|
||
| raise KeyError(f"No version field found in {path!r}") # noqa: TRY003 # not worth a custom exception | ||
|
|
||
|
|
||
| def ensure_tag_not_present(tag: str, remote: str) -> None: | ||
| try: | ||
| _ = subprocess.run( # noqa: S603 # this is trusted input, it's our own arguments being passed in | ||
| ["git", "ls-remote", "--exit-code", "--tags", remote, f"refs/tags/{tag}"], # noqa: S607 # if `git` isn't in PATH already, then there are bigger problems to solve | ||
| check=True, | ||
| stdout=subprocess.DEVNULL, | ||
| stderr=subprocess.DEVNULL, | ||
| ) | ||
| raise Exception(f"Error: tag '{tag}' exists on remote '{remote}'") # noqa: TRY002,TRY003 # not worth a custom exception | ||
| except subprocess.CalledProcessError: | ||
| # tag not present, continue | ||
| return | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description=( | ||
| "Extract the version from a pyproject.toml file, " | ||
| "confirm that git tag v<version> is not present, or " | ||
| "create and push the tag to a remote." | ||
| ) | ||
| ) | ||
| _ = parser.add_argument( | ||
| "file", | ||
| nargs="?", | ||
| default="pyproject.toml", | ||
| help="Path to pyproject.toml (default: pyproject.toml)", | ||
| ) | ||
| _ = parser.add_argument( | ||
| "--confirm-tag-not-present", | ||
| action="store_true", | ||
| help=("Check that git tag v<version> is NOT present on the remote. If the tag exists, exit with an error."), | ||
| ) | ||
| _ = parser.add_argument( | ||
| "--push-tag-to-remote", | ||
| action="store_true", | ||
| help=( | ||
| "Create git tag v<version> locally and push it to the remote. " | ||
| "Internally confirms the tag is not already present." | ||
| ), | ||
| ) | ||
| _ = parser.add_argument( | ||
| "--remote", | ||
| default="origin", | ||
| help="Name of git remote to query/push (default: origin)", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| ver = extract_version(args.file) | ||
|
|
||
| tag = f"v{ver}" | ||
|
|
||
| if args.push_tag_to_remote: | ||
| ensure_tag_not_present(tag, args.remote) | ||
| _ = subprocess.run(["git", "tag", tag], check=True) # noqa: S603,S607 # this is trusted input, it's our own pyproject.toml file. and if `git` isn't in PATH, then there are larger problems anyway | ||
| _ = subprocess.run(["git", "push", args.remote, tag], check=True) # noqa: S603,S607 # this is trusted input, it's our own pyproject.toml file. and if `git` isn't in PATH, then there are larger problems anyway | ||
| return | ||
|
|
||
| if args.confirm_tag_not_present: | ||
| ensure_tag_not_present(tag, args.remote) | ||
| return | ||
|
|
||
| # Default behavior: just print the version | ||
| print(ver) # noqa: T201 # specifically printing this out so CI pipelines can read the value from stdout | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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 @@ | ||
| ../../../src/git_tag.py |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using a custom exception for tag collision errors instead of a generic Exception to improve error clarity and allow for more specific handling.