Skip to content

Commit 53bba5d

Browse files
Merge pull request #167 from stuartcampbell/fix-versioning
Fix versioning
2 parents 3897106 + e62aed1 commit 53bba5d

File tree

6 files changed

+48
-25
lines changed

6 files changed

+48
-25
lines changed

pyproject.toml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["hatchling", "hatch-vcs", "hatch-requirements-txt"]
2+
requires = ["hatchling", "uv-dynamic-versioning"]
33
build-backend = "hatchling.build"
44

55
[project]
@@ -29,15 +29,13 @@ nsls2api = "nsls2api.cli.cli:app"
2929
[project.urls]
3030
Source = "https://github.com/nsls2/nsls2api/"
3131

32-
[tool.hatch]
33-
version.source = "vcs"
32+
[tool.hatch.version]
33+
source = "uv-dynamic-versioning"
3434

35-
[tool.hatch.metadata.hooks.requirements_txt]
36-
files = ["requirements.txt"]
37-
38-
[tool.hatch.build]
39-
packages = ["src/nsls2api"]
40-
hooks.vcs.version-file = "src/nsls2api/_version.py"
35+
[tool.uv-dynamic-versioning]
36+
vcs = "git"
37+
style = "pep440"
38+
bump = true
4139

4240
[tool.hatch.build.targets.sdist]
4341
exclude = [

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ slack-bolt
2323
textual
2424
typer
2525
uuid
26+
uv-dynamic-versioning
2627
uvicorn
2728
Werkzeug

requirements.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ decorator==5.2.1
3333
# via gssapi
3434
dnspython==2.7.0
3535
# via pymongo
36+
dunamai==1.24.1
37+
# via uv-dynamic-versioning
3638
faker==37.3.0
3739
# via -r requirements.in
3840
fastapi==0.115.12
@@ -45,6 +47,8 @@ h11==0.16.0
4547
# via
4648
# httpcore
4749
# uvicorn
50+
hatchling==1.27.0
51+
# via uv-dynamic-versioning
4852
httpcore==1.0.9
4953
# via
5054
# httpx
@@ -65,6 +69,7 @@ jinja2==3.1.6
6569
# via
6670
# -r requirements.in
6771
# jinja-partials
72+
# uv-dynamic-versioning
6873
lazy-model==0.2.0
6974
# via beanie
7075
ldap3==2.9.1
@@ -93,11 +98,17 @@ n2snusertools==0.3.10
9398
packaging==25.0
9499
# via
95100
# asgi-correlation-id
101+
# dunamai
96102
# gunicorn
103+
# hatchling
97104
passlib==1.7.4
98105
# via -r requirements.in
106+
pathspec==0.12.1
107+
# via hatchling
99108
platformdirs==4.3.8
100109
# via textual
110+
pluggy==1.6.0
111+
# via hatchling
101112
prettytable==3.16.0
102113
# via n2snusertools
103114
prometheus-client==0.22.0
@@ -115,6 +126,7 @@ pydantic==2.11.5
115126
# fastapi
116127
# lazy-model
117128
# pydantic-settings
129+
# uv-dynamic-versioning
118130
pydantic-core==2.33.2
119131
# via pydantic
120132
pydantic-settings==2.9.1
@@ -157,6 +169,10 @@ textual==3.2.0
157169
# via -r requirements.in
158170
toml==0.10.2
159171
# via beanie
172+
tomlkit==0.13.2
173+
# via uv-dynamic-versioning
174+
trove-classifiers==2025.5.9.12
175+
# via hatchling
160176
typer==0.16.0
161177
# via -r requirements.in
162178
typing-extensions==4.13.2
@@ -179,6 +195,8 @@ uc-micro-py==1.0.3
179195
# via linkify-it-py
180196
uuid==1.30
181197
# via -r requirements.in
198+
uv-dynamic-versioning==0.8.2
199+
# via -r requirements.in
182200
uvicorn==0.34.2
183201
# via -r requirements.in
184202
wcwidth==0.2.13

src/nsls2api/api/v1/stats_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fastapi
22

3-
from nsls2api._version import version as api_version
43
from nsls2api.api.models.facility_model import FacilityName
54
from nsls2api.api.models.stats_model import (
65
AboutModel,
@@ -13,6 +12,7 @@
1312
facility_service,
1413
proposal_service,
1514
)
15+
from nsls2api.version import get_version
1616

1717
router = fastapi.APIRouter()
1818

@@ -78,5 +78,7 @@ async def stats():
7878

7979
@router.get("/about", response_model=AboutModel)
8080
async def about():
81+
api_version = get_version()
82+
logger.info(f"API version: {api_version}")
8183
model = AboutModel(version=api_version, description="NSLS-II Facility API")
8284
return model

src/nsls2api/cli/cli.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
from rich.theme import Theme
1212

1313
from nsls2api.cli import admin, api, auth, beamline, environment, facility, proposal
14+
from nsls2api.version import get_version
1415

15-
app = typer.Typer(help="NSLS-II API Command Line Interface", no_args_is_help=True)
16+
# Remove no_args_is_help and add invoke_without_command to allow a version option without subcommand.
17+
app = typer.Typer(
18+
help="NSLS-II API Command Line Interface", invoke_without_command=True
19+
)
1620

1721
# Register sub-commands
1822
app.add_typer(admin.app, name="admin", help="Administrative commands")
@@ -58,16 +62,11 @@ def create_command_panel(command_name: str, commands: dict) -> Panel:
5862

5963
def show_welcome():
6064
"""Display welcome message and version information"""
61-
try:
62-
from nsls2api._version import version
63-
64-
version_str = version
65-
except ImportError:
66-
version_str = "unknown"
65+
version = get_version()
6766

6867
welcome_panel = Panel(
6968
Text("Welcome to the NSLS-II API Command Line Interface", style="info"),
70-
subtitle=f"Version: {version_str}",
69+
subtitle=f"Version: {version}",
7170
border_style="cyan",
7271
box=box.DOUBLE,
7372
)
@@ -131,15 +130,11 @@ def main(
131130
NSLS-II API Command Line Interface
132131
"""
133132
if version:
134-
try:
135-
from nsls2api._version import version as ver
136-
137-
console.print(f"[info]NSLS-II API CLI version: {ver}")
138-
except ImportError:
139-
console.print("[warning]Version information not available")
133+
version = get_version()
134+
console.print(f"[info]NSLS-II API CLI version: {version}")
140135
raise typer.Exit()
141136

142-
# Only show welcome message and commands if no subcommand is specified
137+
# Only show a welcome message and commands if no subcommand is specified
143138
if ctx.invoked_subcommand is None:
144139
show_welcome()
145140
console.print()

src/nsls2api/version.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import importlib.metadata
2+
3+
4+
def get_version() -> str:
5+
"""Get the version of the nsls2api package"""
6+
try:
7+
return importlib.metadata.version("nsls2api")
8+
except importlib.metadata.PackageNotFoundError:
9+
return "unknown"

0 commit comments

Comments
 (0)