Skip to content

Commit

Permalink
feat(projector): get-versioneer-version
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Dec 13, 2024
1 parent dee50a6 commit aa4eedd
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6edde570198679e9302e2e78c037215131a507061766c547d19c5559ab8a7db5 requirements.in
3179ddf821eb9b1640acbfd5a43416596bf1ca019de276139e1760fbda7dd997 requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/aarch64-apple-darwin/requirements.txt --python-platform aarch64-apple-darwin --python-version 3.13
# uv pip compile requirements.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/aarch64-apple-darwin/requirements.txt --python-platform aarch64-apple-darwin --python-version 3.11
click==8.1.7
# via typer
markdown-it-py==3.0.0
Expand All @@ -24,3 +24,5 @@ typer==0.12.5
# via -r requirements.in
typing-extensions==4.12.2
# via typer
versioneer==0.29
# via -r requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/aarch64-apple-darwin/requirements_dev.txt --python-platform aarch64-apple-darwin --python-version 3.13
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/aarch64-apple-darwin/requirements_dev.txt --python-platform aarch64-apple-darwin --python-version 3.11
babel==2.15.0
# via mkdocs-material
cairocffi==1.7.1
Expand Down Expand Up @@ -120,6 +120,7 @@ numpy==2.0.0
# via
# contourpy
# matplotlib
# pandas-stubs
# types-seaborn
packaging==24.1
# via
Expand Down Expand Up @@ -198,6 +199,8 @@ tinycss2==1.3.0
# via
# cairosvg
# cssselect2
tomli==2.2.1
# via coverage
tqdm==4.66.4
# via -r requirements.in
typer==0.12.5
Expand All @@ -212,6 +215,8 @@ typing-extensions==4.12.2
# via typer
urllib3==2.2.2
# via requests
versioneer==0.29
# via -r requirements.in
verspec==0.1.0
# via mike
watchdog==4.0.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6edde570198679e9302e2e78c037215131a507061766c547d19c5559ab8a7db5 requirements.in
3179ddf821eb9b1640acbfd5a43416596bf1ca019de276139e1760fbda7dd997 requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-apple-darwin/requirements.txt --python-platform x86_64-apple-darwin --python-version 3.13
# uv pip compile requirements.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-apple-darwin/requirements.txt --python-platform x86_64-apple-darwin --python-version 3.11
click==8.1.7
# via typer
markdown-it-py==3.0.0
Expand All @@ -24,3 +24,5 @@ typer==0.12.5
# via -r requirements.in
typing-extensions==4.12.2
# via typer
versioneer==0.29
# via -r requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-apple-darwin/requirements_dev.txt --python-platform x86_64-apple-darwin --python-version 3.13
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-apple-darwin/requirements_dev.txt --python-platform x86_64-apple-darwin --python-version 3.11
babel==2.15.0
# via mkdocs-material
cairocffi==1.7.1
Expand Down Expand Up @@ -120,6 +120,7 @@ numpy==2.0.0
# via
# contourpy
# matplotlib
# pandas-stubs
# types-seaborn
packaging==24.1
# via
Expand Down Expand Up @@ -198,6 +199,8 @@ tinycss2==1.3.0
# via
# cairosvg
# cssselect2
tomli==2.2.1
# via coverage
tqdm==4.66.4
# via -r requirements.in
typer==0.12.5
Expand All @@ -212,6 +215,8 @@ typing-extensions==4.12.2
# via typer
urllib3==2.2.2
# via requests
versioneer==0.29
# via -r requirements.in
verspec==0.1.0
# via mike
watchdog==4.0.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6edde570198679e9302e2e78c037215131a507061766c547d19c5559ab8a7db5 requirements.in
3179ddf821eb9b1640acbfd5a43416596bf1ca019de276139e1760fbda7dd997 requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-manylinux_2_28/requirements.txt --python-platform x86_64-manylinux_2_28 --python-version 3.13
# uv pip compile requirements.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-manylinux_2_28/requirements.txt --python-platform x86_64-manylinux_2_28 --python-version 3.11
click==8.1.7
# via typer
markdown-it-py==3.0.0
Expand All @@ -24,3 +24,5 @@ typer==0.12.5
# via -r requirements.in
typing-extensions==4.12.2
# via typer
versioneer==0.29
# via -r requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-manylinux_2_28/requirements_dev.txt --python-platform x86_64-manylinux_2_28 --python-version 3.13
# uv pip compile requirements_dev.in -o /Users/kiyoon/project/workflows/python-projector/deps/lock/x86_64-manylinux_2_28/requirements_dev.txt --python-platform x86_64-manylinux_2_28 --python-version 3.11
babel==2.15.0
# via mkdocs-material
cairocffi==1.7.1
Expand Down Expand Up @@ -120,6 +120,7 @@ numpy==2.0.0
# via
# contourpy
# matplotlib
# pandas-stubs
# types-seaborn
packaging==24.1
# via
Expand Down Expand Up @@ -198,6 +199,8 @@ tinycss2==1.3.0
# via
# cairosvg
# cssselect2
tomli==2.2.1
# via coverage
tqdm==4.66.4
# via -r requirements.in
typer==0.12.5
Expand All @@ -212,6 +215,8 @@ typing-extensions==4.12.2
# via typer
urllib3==2.2.2
# via requests
versioneer==0.29
# via -r requirements.in
verspec==0.1.0
# via mike
watchdog==4.0.1
Expand Down
1 change: 1 addition & 0 deletions python-projector/deps/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tqdm>=4.0.0
typer>=0.12.4 # python 3.11 `SomeType | None` support
platformdirs
packaging
versioneer>=0.29
51 changes: 51 additions & 0 deletions python-projector/src/python_projector/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,57 @@ def run_doctest(project_dir: Annotated[Path | None, typer.Argument()] = None):
sys.exit(1)


@app.command()
def get_versioneer_version(
project_dir: Annotated[Path | None, typer.Argument()] = None,
*,
chrome_compatible: Annotated[
bool, typer.Option(help="Used for chrome extensions.")
] = False,
):
"""
Similar to `versioneer.get_version()`, but with more options.
"""
import versioneer

from python_projector.utils.files import find_pyproject_toml
from python_projector.utils.version import (
versioneer_render_chrome_ext_compat_version,
)

pyproject_toml = find_pyproject_toml(project_dir)
with pyproject_toml.open("rb") as f:
pyproject = tomllib.load(f)

if project_dir is None:
project_dir = pyproject_toml.parent

try:
tag_prefix = pyproject["tool"]["versioneer"]["tag_prefix"] # usually "v"
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.tag_prefix in {pyproject_toml}"
) from e

try:
style = pyproject["tool"]["versioneer"]["style"] # pep440
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.style in {pyproject_toml}"
) from e

pieces = versioneer.git_pieces_from_vcs(
tag_prefix=tag_prefix, root=project_dir, verbose=True
)

if chrome_compatible or style == "chrome-ext":
version = versioneer_render_chrome_ext_compat_version(pieces=pieces)
else:
version = versioneer.render(pieces=pieces, style=style)["version"]

print(version)


def main():
app()

Expand Down
29 changes: 29 additions & 0 deletions python-projector/src/python_projector/utils/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from packaging.version import Version


Expand Down Expand Up @@ -52,3 +54,30 @@ def min_version_requires_python(version_range: str):
raise InvalidVersionRangeError(version_range)

return str(min_version)


def versioneer_render_chrome_ext_compat_version(pieces: dict[str, Any]) -> str:
"""
Get versioneer version that is compatible for chrome extension.
Note:
- Chrome extension requires version string to be in the format of 1~4 numbers.
- Thus, we remove git hash and dirty flag from the version string. (If dirty, add 1 to distance)
- We also change the format of distance from +DISTANCE to .DISTANCE
"""
if pieces["error"]:
raise ValueError("Unable to render version")

if pieces["closest-tag"]:
closest_tag: str = pieces["closest-tag"]
else:
closest_tag = "0"

version = closest_tag
if pieces["distance"] or pieces["dirty"]:
if pieces["dirty"]:
version += f".{pieces['distance'] + 1}"
else:
version += f".{pieces['distance']}"

return version

0 comments on commit aa4eedd

Please sign in to comment.