11"""
2- _version.py v1.2
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, D212, D400, D415, S603, BLE001, COM812
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.
24- __version__ = "2.2.0 "
25+ __version__ = "2.2.1 "
2526
2627# Set this to your library name
2728project_name = "rendercanvas"
3031logger = logging .getLogger (project_name )
3132
3233# Get whether this is a repo. If so, repo_dir is the path, otherwise None.
34+ # .git is a dir in a normal repo and a file when in a submodule.
3335repo_dir = Path (__file__ ).parents [1 ]
34- repo_dir = repo_dir if repo_dir .joinpath (".git" ).is_dir () else None
36+ repo_dir = repo_dir if repo_dir .joinpath (".git" ).exists () else None
3537
3638
37- def get_version ():
39+ def get_version () -> str :
3840 """Get the version string."""
3941 if repo_dir :
4042 return get_extended_version ()
41- else :
42- return __version__
43+ return __version__
4344
4445
45- def get_extended_version ():
46+ def get_extended_version () -> str :
4647 """Get an extended version string with information from git."""
47-
4848 release , post , labels = get_version_info_from_git ()
4949
5050 # Sample first 3 parts of __version__
@@ -54,9 +54,9 @@ def get_extended_version():
5454 if not release :
5555 release = base_release
5656 elif release != base_release :
57- logger . warning (
57+ warning (
5858 f"{ project_name } version from git ({ release } )"
59- + f" and __version__ ({ base_release } ) don't match."
59+ f" and __version__ ({ base_release } ) don't match."
6060 )
6161
6262 # Build the total version
@@ -71,14 +71,14 @@ def get_extended_version():
7171 return version
7272
7373
74- def get_version_info_from_git ():
75- """Get (release, post, labels) from Git.
74+ def get_version_info_from_git () -> str :
75+ """
76+ Get (release, post, labels) from Git.
7677
7778 With `release` the version number from the latest tag, `post` the
7879 number of commits since that tag, and `labels` a tuple with the
7980 git-hash and optionally a dirty flag.
8081 """
81-
8282 # Call out to Git
8383 command = [
8484 "git" ,
@@ -90,9 +90,9 @@ def get_version_info_from_git():
9090 "--first-parent" ,
9191 ]
9292 try :
93- p = subprocess .run (command , cwd = repo_dir , capture_output = True )
93+ p = subprocess .run (command , check = False , cwd = repo_dir , capture_output = True )
9494 except Exception as e :
95- logger . warning (f"Could not get { project_name } version: { e } " )
95+ warning (f"Could not get { project_name } version: { e } " )
9696 p = None
9797
9898 # Parse the result into parts
@@ -102,7 +102,7 @@ def get_version_info_from_git():
102102 output = p .stdout .decode (errors = "ignore" )
103103 if p .returncode :
104104 stderr = p .stderr .decode (errors = "ignore" )
105- logger . warning (
105+ warning (
106106 f"Could not get { project_name } version.\n \n stdout: "
107107 + output
108108 + "\n \n stderr: "
@@ -120,16 +120,23 @@ def get_version_info_from_git():
120120 return release , post , labels
121121
122122
123- def _to_tuple (v ):
124- """Convert __version__ to version_info tuple."""
123+ def version_to_tuple (v : str ) -> tuple :
125124 v = __version__ .split ("+" )[0 ] # remove hash
126125 return tuple (int (i ) if i .isnumeric () else i for i in v .split ("." ))
127126
128127
128+ def prnt (m : str ) -> None :
129+ sys .stdout .write (m + "\n " )
130+
131+
132+ def warning (m : str ) -> None :
133+ logger .warning (m )
134+
135+
129136# Apply the versioning
130137base_version = __version__
131138__version__ = get_version ()
132- version_info = _to_tuple (__version__ )
139+ version_info = version_to_tuple (__version__ )
133140
134141
135142# The CLI part
@@ -149,41 +156,39 @@ def _to_tuple(v):
149156 import urllib .request
150157
151158 _ , * args = sys .argv
159+ this_file = Path (__file__ )
152160
153- if not args :
154- print (f"{ project_name } v{ __version__ } " )
155-
156- elif args [0 ] == "version" :
157- print (f"{ project_name } v{ __version__ } " )
161+ if not args or args [0 ] == "version" :
162+ prnt (f"{ project_name } v{ __version__ } " )
158163
159164 elif args [0 ] == "bump" :
160165 if len (args ) != 2 :
161166 sys .exit ("Expected a version number to bump to." )
162167 new_version = args [1 ].lstrip ("v" ) # allow '1.2.3' and 'v1.2.3'
163- if not new_version .count ("." ) = = 2 :
168+ if new_version .count ("." ) ! = 2 :
164169 sys .exit ("Expected two dots in new version string." )
165170 if not all (s .isnumeric () for s in new_version .split ("." )):
166171 sys .exit ("Expected only numbers in new version string." )
167- with open (__file__ , "rb" ) as f :
172+ with this_file . open ("rb" ) as f :
168173 text = ref_text = f .read ().decode ()
169174 text = text .replace (base_version , new_version , 1 )
170- with open (__file__ , "wb" ) as f :
175+ with this_file . open ("wb" ) as f :
171176 f .write (text .encode ())
172- print (f"Bumped version from '{ base_version } ' to '{ new_version } '." )
177+ prnt (f"Bumped version from '{ base_version } ' to '{ new_version } '." )
173178
174179 elif args [0 ] == "update" :
175180 u = "https://raw.githubusercontent.com/pygfx/_version/main/_version.py"
176181 with urllib .request .urlopen (u ) as f :
177182 text = ref_text = f .read ().decode ()
178183 text = text .replace ("0.0.0" , base_version , 1 )
179184 text = text .replace ("PROJECT_NAME" , project_name , 1 )
180- with open (__file__ , "wb" ) as f :
185+ with this_file . open ("wb" ) as f :
181186 f .write (text .encode ())
182- print ("Updated to the latest _version.py." )
187+ prnt ("Updated to the latest _version.py." )
183188
184189 elif args [0 ].lstrip ("-" ) in ["h" , "help" ]:
185- print (CLI_USAGE )
190+ prnt (CLI_USAGE )
186191
187192 else :
188- print (f"Unknown command for _version.py: { args [0 ]!r} " )
189- print ("Use ``python _version.py help`` to see a list of options." )
193+ prnt (f"Unknown command for _version.py: { args [0 ]!r} " )
194+ prnt ("Use ``python _version.py help`` to see a list of options." )
0 commit comments