11"""
2- _version.py v1.3
2+ _version.py v1.4
33
44Simple version string management, using a hard-coded version string
55for simplicity and compatibility, while adding git info at runtime.
1414* On a new release, you just update the __version__.
1515"""
1616
17+ # ruff: noqa: RUF100, S310, PLR2004, D400, D415, S603, BLE001
18+
1719import logging
1820import subprocess
1921from pathlib import Path
2022
21-
2223# This is the base version number, to be bumped before each release.
2324# The build system detects this definition when building a distribution.
2425__version__ = "0.0.0"
3536repo_dir = repo_dir if repo_dir .joinpath (".git" ).exists () else None
3637
3738
38- def get_version ():
39+ def get_version () -> str :
3940 """Get the version string."""
4041 if repo_dir :
4142 return get_extended_version ()
42- else :
43- return __version__
43+ return __version__
4444
4545
46- def get_extended_version ():
46+ def get_extended_version () -> str :
4747 """Get an extended version string with information from git."""
48-
4948 release , post , labels = get_version_info_from_git ()
5049
5150 # Sample first 3 parts of __version__
@@ -55,9 +54,9 @@ def get_extended_version():
5554 if not release :
5655 release = base_release
5756 elif release != base_release :
58- logger . warning (
57+ warning (
5958 f"{ project_name } version from git ({ release } )"
60- + f" and __version__ ({ base_release } ) don't match."
59+ f" and __version__ ({ base_release } ) don't match."
6160 )
6261
6362 # Build the total version
@@ -72,14 +71,14 @@ def get_extended_version():
7271 return version
7372
7473
75- def get_version_info_from_git ():
76- """Get (release, post, labels) from Git.
74+ def get_version_info_from_git () -> str :
75+ """
76+ Get (release, post, labels) from Git.
7777
7878 With `release` the version number from the latest tag, `post` the
7979 number of commits since that tag, and `labels` a tuple with the
8080 git-hash and optionally a dirty flag.
8181 """
82-
8382 # Call out to Git
8483 command = [
8584 "git" ,
@@ -91,9 +90,11 @@ def get_version_info_from_git():
9190 "--first-parent" ,
9291 ]
9392 try :
94- p = subprocess .run (command , cwd = repo_dir , capture_output = True )
93+ p = subprocess .run (
94+ command , check = False , cwd = repo_dir , capture_output = True
95+ )
9596 except Exception as e :
96- logger . warning (f"Could not get { project_name } version: { e } " )
97+ warning (f"Could not get { project_name } version: { e } " )
9798 p = None
9899
99100 # Parse the result into parts
@@ -103,7 +104,7 @@ def get_version_info_from_git():
103104 output = p .stdout .decode (errors = "ignore" )
104105 if p .returncode :
105106 stderr = p .stderr .decode (errors = "ignore" )
106- logger . warning (
107+ warning (
107108 f"Could not get { project_name } version.\n \n stdout: "
108109 + output
109110 + "\n \n stderr: "
@@ -121,16 +122,23 @@ def get_version_info_from_git():
121122 return release , post , labels
122123
123124
124- def _to_tuple (v ):
125- """Convert __version__ to version_info tuple."""
125+ def version_to_tuple (v : str ) -> tuple :
126126 v = __version__ .split ("+" )[0 ] # remove hash
127127 return tuple (int (i ) if i .isnumeric () else i for i in v .split ("." ))
128128
129129
130+ def prnt (m : str ) -> None :
131+ sys .stdout .write (m + "\n " )
132+
133+
134+ def warning (m : str ) -> None :
135+ logger .warning (m )
136+
137+
130138# Apply the versioning
131139base_version = __version__
132140__version__ = get_version ()
133- version_info = _to_tuple (__version__ )
141+ version_info = version_to_tuple (__version__ )
134142
135143
136144# The CLI part
@@ -150,41 +158,39 @@ def _to_tuple(v):
150158 import urllib .request
151159
152160 _ , * args = sys .argv
161+ this_file = Path (__file__ )
153162
154- if not args :
155- print (f"{ project_name } v{ __version__ } " )
156-
157- elif args [0 ] == "version" :
158- print (f"{ project_name } v{ __version__ } " )
163+ if not args or args [0 ] == "version" :
164+ prnt (f"{ project_name } v{ __version__ } " )
159165
160166 elif args [0 ] == "bump" :
161167 if len (args ) != 2 :
162168 sys .exit ("Expected a version number to bump to." )
163169 new_version = args [1 ].lstrip ("v" ) # allow '1.2.3' and 'v1.2.3'
164- if not new_version .count ("." ) = = 2 :
170+ if new_version .count ("." ) ! = 2 :
165171 sys .exit ("Expected two dots in new version string." )
166172 if not all (s .isnumeric () for s in new_version .split ("." )):
167173 sys .exit ("Expected only numbers in new version string." )
168- with open (__file__ , "rb" ) as f :
174+ with this_file . open ("rb" ) as f :
169175 text = ref_text = f .read ().decode ()
170176 text = text .replace (base_version , new_version , 1 )
171- with open (__file__ , "wb" ) as f :
177+ with this_file . open ("wb" ) as f :
172178 f .write (text .encode ())
173- print (f"Bumped version from '{ base_version } ' to '{ new_version } '." )
179+ prnt (f"Bumped version from '{ base_version } ' to '{ new_version } '." )
174180
175181 elif args [0 ] == "update" :
176182 u = "https://raw.githubusercontent.com/pygfx/_version/main/_version.py"
177183 with urllib .request .urlopen (u ) as f :
178184 text = ref_text = f .read ().decode ()
179185 text = text .replace ("0.0.0" , base_version , 1 )
180186 text = text .replace ("PROJECT_NAME" , project_name , 1 )
181- with open (__file__ , "wb" ) as f :
187+ with this_file . open ("wb" ) as f :
182188 f .write (text .encode ())
183- print ("Updated to the latest _version.py." )
189+ prnt ("Updated to the latest _version.py." )
184190
185191 elif args [0 ].lstrip ("-" ) in ["h" , "help" ]:
186- print (CLI_USAGE )
192+ prnt (CLI_USAGE )
187193
188194 else :
189- print (f"Unknown command for _version.py: { args [0 ]!r} " )
190- print ("Use ``python _version.py help`` to see a list of options." )
195+ prnt (f"Unknown command for _version.py: { args [0 ]!r} " )
196+ prnt ("Use ``python _version.py help`` to see a list of options." )
0 commit comments