1
1
"""
2
- _version.py v1.2
2
+ _version.py v1.4
3
3
4
4
Simple version string management, using a hard-coded version string
5
5
for simplicity and compatibility, while adding git info at runtime.
14
14
* On a new release, you just update the __version__.
15
15
"""
16
16
17
+ # ruff: noqa: RUF100, S310, PLR2004, D212, D400, D415, S603, BLE001, COM812
18
+
17
19
import logging
18
20
import subprocess
19
21
from pathlib import Path
20
22
21
-
22
23
# This is the base version number, to be bumped before each release.
23
24
# The build system detects this definition when building a distribution.
24
- __version__ = "2.2.0 "
25
+ __version__ = "2.2.1 "
25
26
26
27
# Set this to your library name
27
28
project_name = "rendercanvas"
30
31
logger = logging .getLogger (project_name )
31
32
32
33
# 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.
33
35
repo_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
35
37
36
38
37
- def get_version ():
39
+ def get_version () -> str :
38
40
"""Get the version string."""
39
41
if repo_dir :
40
42
return get_extended_version ()
41
- else :
42
- return __version__
43
+ return __version__
43
44
44
45
45
- def get_extended_version ():
46
+ def get_extended_version () -> str :
46
47
"""Get an extended version string with information from git."""
47
-
48
48
release , post , labels = get_version_info_from_git ()
49
49
50
50
# Sample first 3 parts of __version__
@@ -54,9 +54,9 @@ def get_extended_version():
54
54
if not release :
55
55
release = base_release
56
56
elif release != base_release :
57
- logger . warning (
57
+ warning (
58
58
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."
60
60
)
61
61
62
62
# Build the total version
@@ -71,14 +71,14 @@ def get_extended_version():
71
71
return version
72
72
73
73
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.
76
77
77
78
With `release` the version number from the latest tag, `post` the
78
79
number of commits since that tag, and `labels` a tuple with the
79
80
git-hash and optionally a dirty flag.
80
81
"""
81
-
82
82
# Call out to Git
83
83
command = [
84
84
"git" ,
@@ -90,9 +90,9 @@ def get_version_info_from_git():
90
90
"--first-parent" ,
91
91
]
92
92
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 )
94
94
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 } " )
96
96
p = None
97
97
98
98
# Parse the result into parts
@@ -102,7 +102,7 @@ def get_version_info_from_git():
102
102
output = p .stdout .decode (errors = "ignore" )
103
103
if p .returncode :
104
104
stderr = p .stderr .decode (errors = "ignore" )
105
- logger . warning (
105
+ warning (
106
106
f"Could not get { project_name } version.\n \n stdout: "
107
107
+ output
108
108
+ "\n \n stderr: "
@@ -120,16 +120,23 @@ def get_version_info_from_git():
120
120
return release , post , labels
121
121
122
122
123
- def _to_tuple (v ):
124
- """Convert __version__ to version_info tuple."""
123
+ def version_to_tuple (v : str ) -> tuple :
125
124
v = __version__ .split ("+" )[0 ] # remove hash
126
125
return tuple (int (i ) if i .isnumeric () else i for i in v .split ("." ))
127
126
128
127
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
+
129
136
# Apply the versioning
130
137
base_version = __version__
131
138
__version__ = get_version ()
132
- version_info = _to_tuple (__version__ )
139
+ version_info = version_to_tuple (__version__ )
133
140
134
141
135
142
# The CLI part
@@ -149,41 +156,39 @@ def _to_tuple(v):
149
156
import urllib .request
150
157
151
158
_ , * args = sys .argv
159
+ this_file = Path (__file__ )
152
160
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__ } " )
158
163
159
164
elif args [0 ] == "bump" :
160
165
if len (args ) != 2 :
161
166
sys .exit ("Expected a version number to bump to." )
162
167
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 :
164
169
sys .exit ("Expected two dots in new version string." )
165
170
if not all (s .isnumeric () for s in new_version .split ("." )):
166
171
sys .exit ("Expected only numbers in new version string." )
167
- with open (__file__ , "rb" ) as f :
172
+ with this_file . open ("rb" ) as f :
168
173
text = ref_text = f .read ().decode ()
169
174
text = text .replace (base_version , new_version , 1 )
170
- with open (__file__ , "wb" ) as f :
175
+ with this_file . open ("wb" ) as f :
171
176
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 } '." )
173
178
174
179
elif args [0 ] == "update" :
175
180
u = "https://raw.githubusercontent.com/pygfx/_version/main/_version.py"
176
181
with urllib .request .urlopen (u ) as f :
177
182
text = ref_text = f .read ().decode ()
178
183
text = text .replace ("0.0.0" , base_version , 1 )
179
184
text = text .replace ("PROJECT_NAME" , project_name , 1 )
180
- with open (__file__ , "wb" ) as f :
185
+ with this_file . open ("wb" ) as f :
181
186
f .write (text .encode ())
182
- print ("Updated to the latest _version.py." )
187
+ prnt ("Updated to the latest _version.py." )
183
188
184
189
elif args [0 ].lstrip ("-" ) in ["h" , "help" ]:
185
- print (CLI_USAGE )
190
+ prnt (CLI_USAGE )
186
191
187
192
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