44
55"""Stamp temporary Python package metadata for dev wheel artifact builds."""
66
7- from __future__ import annotations
8-
97import argparse
10- import dataclasses
118import re
129import sys
1310from pathlib import Path
1411
1512DEV_VERSION_RE = re .compile (r"^(?P<release>\d+\.\d+\.\d+)\.dev(?P<number>\d*)$" )
16- PACKAGE_NAME_RE = re .compile (r'^(name\s*=\s*")([^"]+)(".*)$' )
1713PACKAGE_VERSION_RE = re .compile (r'^(version\s*=\s*")([^"]+)(".*)$' )
18- PYTHON_VERSION_RE = re .compile (r'^(__version__\s*=\s*")([^"]+)(".*)$' , re .MULTILINE )
19-
20-
21- @dataclasses .dataclass (frozen = True )
22- class DevWheelVersion :
23- """Normalized metadata used for a short-lived dev wheel artifact build."""
2414
25- version : str
2615
27-
28- def parse_dev_wheel_version (version : str ) -> DevWheelVersion :
16+ def parse_dev_wheel_version (version : str ) -> str :
2917 """Return the normalized PEP 440 `.dev` version or raise `ValueError`."""
3018
3119 match = DEV_VERSION_RE .fullmatch (version )
3220 if match is None :
3321 raise ValueError ("dev wheel versions must look like 0.0.1.dev0" )
3422
3523 number = match .group ("number" ) or "0"
36- return DevWheelVersion ( version = f"{ match .group ('release' )} .dev{ number } " )
24+ return f"{ match .group ('release' )} .dev{ number } "
3725
3826
39- def update_pyproject (path : Path , * , package_name : str , version : str ) -> bool :
40- """Set `[project]` name and version in `pyproject.toml`."""
27+ def update_pyproject (path : Path , version : str ) -> bool :
28+ """Set `[project]. version` in `pyproject.toml`."""
4129
4230 lines = path .read_text ().splitlines (keepends = True )
4331 in_project = False
4432 changed = False
45- found_name = False
4633 found_version = False
4734 output : list [str ] = []
4835
@@ -53,74 +40,37 @@ def update_pyproject(path: Path, *, package_name: str, version: str) -> bool:
5340
5441 updated = line
5542 if in_project :
56- updated , count = PACKAGE_NAME_RE .subn (rf"\g<1>{ package_name } \g<3>" , updated , count = 1 )
57- if count :
58- found_name = True
5943 updated , count = PACKAGE_VERSION_RE .subn (rf"\g<1>{ version } \g<3>" , updated , count = 1 )
6044 if count :
6145 found_version = True
6246
6347 changed = changed or updated != line
6448 output .append (updated )
6549
66- if not found_name :
67- raise ValueError (f"{ path } : missing [project] name" )
6850 if not found_version :
6951 raise ValueError (f"{ path } : missing [project] version" )
7052 if changed :
7153 path .write_text ("" .join (output ))
7254 return changed
7355
7456
75- def update_python_init (path : Path , version : str ) -> bool :
76- """Set `switchyard.__version__` for the dev wheel artifact."""
77-
78- text = path .read_text ()
79- updated , count = PYTHON_VERSION_RE .subn (rf"\g<1>{ version } \g<3>" , text , count = 1 )
80- if count != 1 :
81- raise ValueError (f"{ path } : missing __version__" )
82- if updated != text :
83- path .write_text (updated )
84- return True
85- return False
57+ def apply_version (version : str ) -> None :
58+ """Set the wheel version in `pyproject.toml`."""
8659
87-
88- def apply_version (version : DevWheelVersion , * , package_name : str ) -> None :
89- """Update package metadata files used by maturin wheel builds."""
90-
91- changes = [
92- (
93- "pyproject.toml" ,
94- update_pyproject (
95- Path ("pyproject.toml" ),
96- package_name = package_name ,
97- version = version .version ,
98- ),
99- ),
100- ("switchyard/__init__.py" , update_python_init (Path ("switchyard/__init__.py" ), version .version )),
101- ]
102-
103- changed = [path for path , did_change in changes if did_change ]
60+ changed = update_pyproject (Path ("pyproject.toml" ), version )
10461 if changed :
10562 print ("Set dev wheel metadata:" )
106- print (f" Package: { package_name } " )
107- print (f" Version: { version .version } " )
108- for path in changed :
109- print (f" updated { path } " )
63+ print (f" Version: { version } " )
64+ print (" updated pyproject.toml" )
11065 else :
111- print (f"dev wheel metadata already set for { package_name } { version . version } " )
66+ print (f"dev wheel metadata already set to { version } " )
11267
11368
11469def main (argv : list [str ] | None = None ) -> int :
11570 """CLI entry point."""
11671
11772 parser = argparse .ArgumentParser (description = __doc__ )
11873 parser .add_argument ("version" , help = "PEP 440 .dev version, such as 0.0.1.dev0" )
119- parser .add_argument (
120- "--package-name" ,
121- default = "nemo-switchyard" ,
122- help = "Distribution name to stamp into wheel metadata" ,
123- )
12474 parser .add_argument (
12575 "--print-version" ,
12676 action = "store_true" ,
@@ -131,9 +81,9 @@ def main(argv: list[str] | None = None) -> int:
13181 try :
13282 version = parse_dev_wheel_version (args .version )
13383 if args .print_version :
134- print (version . version )
84+ print (version )
13585 return 0
136- apply_version (version , package_name = args . package_name )
86+ apply_version (version )
13787 except ValueError as exc :
13888 print (f"error: { exc } " , file = sys .stderr )
13989 return 2
0 commit comments