99from pathlib import Path
1010import re
1111import tomllib
12+ from datetime import datetime , timezone
1213
1314from e3 .main import Main
1415from e3 .os .process import Run
@@ -46,7 +47,22 @@ def main() -> None:
4647 default = "pyproject.toml" ,
4748 help = "Path to a Python project or pyproject.toml file" ,
4849 )
49- parser .add_argument ("--dry-run" , action = "store_true" , help = "Do not build the wheel" )
50+ parser .add_argument (
51+ "--template" , default = "{major}.{minor}.{patch}" , help = "Version number template"
52+ )
53+ parser .add_argument (
54+ "--dry-run" ,
55+ action = "store_true" ,
56+ help = "Do not change the version file nor build the wheel" ,
57+ )
58+ parser .add_argument (
59+ "--no-build" , action = "store_true" , help = "Do not build the wheel"
60+ )
61+ parser .add_argument (
62+ "--no-restore" ,
63+ action = "store_true" ,
64+ help = "Keep the modified version file with the computed build version" ,
65+ )
5066
5167 main .parse_args ()
5268 assert main .args
@@ -131,7 +147,17 @@ def main() -> None:
131147 ["git" , "rev-list" , f"{ previous_commit_sha } ..HEAD" , "--count" ],
132148 cwd = root_dir ,
133149 )
134- build_version = f"{ version_major } .{ version_minor } .{ output .strip ()} "
150+
151+ # Get the build version from custom version template
152+ date = datetime .now (tz = timezone .utc )
153+ build_version = main .args .template .format (
154+ major = version_major ,
155+ minor = version_minor ,
156+ patch = output .strip (),
157+ year = f"{ date .year :04} " ,
158+ month = f"{ date .month :02} " ,
159+ day = f"{ date .day :02} " ,
160+ )
135161 logger .info (f"{ version_major } .{ version_minor } -> { build_version } " )
136162
137163 if not main .args .dry_run :
@@ -141,24 +167,26 @@ def main() -> None:
141167
142168 try :
143169 # Build the wheel
144- run (
145- [
146- sys .executable ,
147- "-m" ,
148- "pip" ,
149- "wheel" ,
150- "." ,
151- "-q" ,
152- "--no-deps" ,
153- "-C--python-tag=py3" ,
154- "-w" ,
155- "build" ,
156- ],
157- cwd = root_dir ,
158- )
170+ if not main .args .no_build :
171+ run (
172+ [
173+ sys .executable ,
174+ "-m" ,
175+ "pip" ,
176+ "wheel" ,
177+ "." ,
178+ "-q" ,
179+ "--no-deps" ,
180+ "-C--python-tag=py3" ,
181+ "-w" ,
182+ "build" ,
183+ ],
184+ cwd = root_dir ,
185+ )
159186 finally :
160187 # Revert change to version file
161- run (["git" , "restore" , version_path ], cwd = root_dir , fail_ok = True )
188+ if not main .args .no_restore :
189+ run (["git" , "restore" , version_path ], cwd = root_dir , fail_ok = True )
162190
163191
164192if __name__ == "__main__" :
0 commit comments