1
-
2
1
# This file helps to compute a version number in source trees obtained from
3
2
# git-archive tarball (such as those provided by githubs download-from-tag
4
3
# feature). Distribution tarballs (built by setup.py sdist) and build
@@ -68,12 +67,14 @@ class NotThisMethod(Exception):
68
67
69
68
def register_vcs_handler (vcs : str , method : str ) -> Callable : # decorator
70
69
"""Create decorator to mark a method as the handler of a VCS."""
70
+
71
71
def decorate (f : Callable ) -> Callable :
72
72
"""Store f in HANDLERS[vcs][method]."""
73
73
if vcs not in HANDLERS :
74
74
HANDLERS [vcs ] = {}
75
75
HANDLERS [vcs ][method ] = f
76
76
return f
77
+
77
78
return decorate
78
79
79
80
@@ -100,10 +101,14 @@ def run_command(
100
101
try :
101
102
dispcmd = str ([command ] + args )
102
103
# remember shell=False, so use git.cmd on windows, not just git
103
- process = subprocess .Popen ([command ] + args , cwd = cwd , env = env ,
104
- stdout = subprocess .PIPE ,
105
- stderr = (subprocess .PIPE if hide_stderr
106
- else None ), ** popen_kwargs )
104
+ process = subprocess .Popen (
105
+ [command ] + args ,
106
+ cwd = cwd ,
107
+ env = env ,
108
+ stdout = subprocess .PIPE ,
109
+ stderr = (subprocess .PIPE if hide_stderr else None ),
110
+ ** popen_kwargs ,
111
+ )
107
112
break
108
113
except OSError as e :
109
114
if e .errno == errno .ENOENT :
@@ -141,15 +146,21 @@ def versions_from_parentdir(
141
146
for _ in range (3 ):
142
147
dirname = os .path .basename (root )
143
148
if dirname .startswith (parentdir_prefix ):
144
- return {"version" : dirname [len (parentdir_prefix ):],
145
- "full-revisionid" : None ,
146
- "dirty" : False , "error" : None , "date" : None }
149
+ return {
150
+ "version" : dirname [len (parentdir_prefix ) :],
151
+ "full-revisionid" : None ,
152
+ "dirty" : False ,
153
+ "error" : None ,
154
+ "date" : None ,
155
+ }
147
156
rootdirs .append (root )
148
157
root = os .path .dirname (root ) # up a level
149
158
150
159
if verbose :
151
- print ("Tried directories %s but none started with prefix %s" %
152
- (str (rootdirs ), parentdir_prefix ))
160
+ print (
161
+ "Tried directories %s but none started with prefix %s"
162
+ % (str (rootdirs ), parentdir_prefix )
163
+ )
153
164
raise NotThisMethod ("rootdir doesn't start with parentdir_prefix" )
154
165
155
166
@@ -212,7 +223,7 @@ def git_versions_from_keywords(
212
223
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
213
224
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
214
225
TAG = "tag: "
215
- tags = {r [len (TAG ):] for r in refs if r .startswith (TAG )}
226
+ tags = {r [len (TAG ) :] for r in refs if r .startswith (TAG )}
216
227
if not tags :
217
228
# Either we're using git < 1.8.3, or there really are no tags. We use
218
229
# a heuristic: assume all version tags have a digit. The old git %d
@@ -221,40 +232,44 @@ def git_versions_from_keywords(
221
232
# between branches and tags. By ignoring refnames without digits, we
222
233
# filter out many common branch names like "release" and
223
234
# "stabilization", as well as "HEAD" and "master".
224
- tags = {r for r in refs if re .search (r'\d' , r )}
235
+ tags = {r for r in refs if re .search (r"\d" , r )}
225
236
if verbose :
226
237
print ("discarding '%s', no digits" % "," .join (refs - tags ))
227
238
if verbose :
228
239
print ("likely tags: %s" % "," .join (sorted (tags )))
229
240
for ref in sorted (tags ):
230
241
# sorting will prefer e.g. "2.0" over "2.0rc1"
231
242
if ref .startswith (tag_prefix ):
232
- r = ref [len (tag_prefix ):]
243
+ r = ref [len (tag_prefix ) :]
233
244
# Filter out refs that exactly match prefix or that don't start
234
245
# with a number once the prefix is stripped (mostly a concern
235
246
# when prefix is '')
236
- if not re .match (r'\d' , r ):
247
+ if not re .match (r"\d" , r ):
237
248
continue
238
249
if verbose :
239
250
print ("picking %s" % r )
240
- return {"version" : r ,
241
- "full-revisionid" : keywords ["full" ].strip (),
242
- "dirty" : False , "error" : None ,
243
- "date" : date }
251
+ return {
252
+ "version" : r ,
253
+ "full-revisionid" : keywords ["full" ].strip (),
254
+ "dirty" : False ,
255
+ "error" : None ,
256
+ "date" : date ,
257
+ }
244
258
# no suitable tags, so version is "0+unknown", but full hex is still there
245
259
if verbose :
246
260
print ("no suitable tags, using unknown + full revision id" )
247
- return {"version" : "0+unknown" ,
248
- "full-revisionid" : keywords ["full" ].strip (),
249
- "dirty" : False , "error" : "no suitable tags" , "date" : None }
261
+ return {
262
+ "version" : "0+unknown" ,
263
+ "full-revisionid" : keywords ["full" ].strip (),
264
+ "dirty" : False ,
265
+ "error" : "no suitable tags" ,
266
+ "date" : None ,
267
+ }
250
268
251
269
252
270
@register_vcs_handler ("git" , "pieces_from_vcs" )
253
271
def git_pieces_from_vcs (
254
- tag_prefix : str ,
255
- root : str ,
256
- verbose : bool ,
257
- runner : Callable = run_command
272
+ tag_prefix : str , root : str , verbose : bool , runner : Callable = run_command
258
273
) -> Dict [str , Any ]:
259
274
"""Get version from 'git describe' in the root of the source tree.
260
275
@@ -273,19 +288,27 @@ def git_pieces_from_vcs(
273
288
env .pop ("GIT_DIR" , None )
274
289
runner = functools .partial (runner , env = env )
275
290
276
- _ , rc = runner (GITS , ["rev-parse" , "--git-dir" ], cwd = root ,
277
- hide_stderr = not verbose )
291
+ _ , rc = runner (GITS , ["rev-parse" , "--git-dir" ], cwd = root , hide_stderr = not verbose )
278
292
if rc != 0 :
279
293
if verbose :
280
294
print ("Directory %s not under git control" % root )
281
295
raise NotThisMethod ("'git rev-parse --git-dir' returned error" )
282
296
283
297
# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
284
298
# if there isn't one, this yields HEX[-dirty] (no NUM)
285
- describe_out , rc = runner (GITS , [
286
- "describe" , "--tags" , "--dirty" , "--always" , "--long" ,
287
- "--match" , f"{ tag_prefix } [[:digit:]]*"
288
- ], cwd = root )
299
+ describe_out , rc = runner (
300
+ GITS ,
301
+ [
302
+ "describe" ,
303
+ "--tags" ,
304
+ "--dirty" ,
305
+ "--always" ,
306
+ "--long" ,
307
+ "--match" ,
308
+ f"{ tag_prefix } [[:digit:]]*" ,
309
+ ],
310
+ cwd = root ,
311
+ )
289
312
# --long was added in git-1.5.5
290
313
if describe_out is None :
291
314
raise NotThisMethod ("'git describe' failed" )
@@ -300,8 +323,7 @@ def git_pieces_from_vcs(
300
323
pieces ["short" ] = full_out [:7 ] # maybe improved later
301
324
pieces ["error" ] = None
302
325
303
- branch_name , rc = runner (GITS , ["rev-parse" , "--abbrev-ref" , "HEAD" ],
304
- cwd = root )
326
+ branch_name , rc = runner (GITS , ["rev-parse" , "--abbrev-ref" , "HEAD" ], cwd = root )
305
327
# --abbrev-ref was added in git-1.6.3
306
328
if rc != 0 or branch_name is None :
307
329
raise NotThisMethod ("'git rev-parse --abbrev-ref' returned error" )
@@ -341,17 +363,16 @@ def git_pieces_from_vcs(
341
363
dirty = git_describe .endswith ("-dirty" )
342
364
pieces ["dirty" ] = dirty
343
365
if dirty :
344
- git_describe = git_describe [:git_describe .rindex ("-dirty" )]
366
+ git_describe = git_describe [: git_describe .rindex ("-dirty" )]
345
367
346
368
# now we have TAG-NUM-gHEX or HEX
347
369
348
370
if "-" in git_describe :
349
371
# TAG-NUM-gHEX
350
- mo = re .search (r' ^(.+)-(\d+)-g([0-9a-f]+)$' , git_describe )
372
+ mo = re .search (r" ^(.+)-(\d+)-g([0-9a-f]+)$" , git_describe )
351
373
if not mo :
352
374
# unparsable. Maybe git-describe is misbehaving?
353
- pieces ["error" ] = ("unable to parse git-describe output: '%s'"
354
- % describe_out )
375
+ pieces ["error" ] = "unable to parse git-describe output: '%s'" % describe_out
355
376
return pieces
356
377
357
378
# tag
@@ -360,10 +381,12 @@ def git_pieces_from_vcs(
360
381
if verbose :
361
382
fmt = "tag '%s' doesn't start with prefix '%s'"
362
383
print (fmt % (full_tag , tag_prefix ))
363
- pieces ["error" ] = ("tag '%s' doesn't start with prefix '%s'"
364
- % (full_tag , tag_prefix ))
384
+ pieces ["error" ] = "tag '%s' doesn't start with prefix '%s'" % (
385
+ full_tag ,
386
+ tag_prefix ,
387
+ )
365
388
return pieces
366
- pieces ["closest-tag" ] = full_tag [len (tag_prefix ):]
389
+ pieces ["closest-tag" ] = full_tag [len (tag_prefix ) :]
367
390
368
391
# distance: number of commits since tag
369
392
pieces ["distance" ] = int (mo .group (2 ))
@@ -412,8 +435,7 @@ def render_pep440(pieces: Dict[str, Any]) -> str:
412
435
rendered += ".dirty"
413
436
else :
414
437
# exception #1
415
- rendered = "0+untagged.%d.g%s" % (pieces ["distance" ],
416
- pieces ["short" ])
438
+ rendered = "0+untagged.%d.g%s" % (pieces ["distance" ], pieces ["short" ])
417
439
if pieces ["dirty" ]:
418
440
rendered += ".dirty"
419
441
return rendered
@@ -442,8 +464,7 @@ def render_pep440_branch(pieces: Dict[str, Any]) -> str:
442
464
rendered = "0"
443
465
if pieces ["branch" ] != "master" :
444
466
rendered += ".dev0"
445
- rendered += "+untagged.%d.g%s" % (pieces ["distance" ],
446
- pieces ["short" ])
467
+ rendered += "+untagged.%d.g%s" % (pieces ["distance" ], pieces ["short" ])
447
468
if pieces ["dirty" ]:
448
469
rendered += ".dirty"
449
470
return rendered
@@ -604,11 +625,13 @@ def render_git_describe_long(pieces: Dict[str, Any]) -> str:
604
625
def render (pieces : Dict [str , Any ], style : str ) -> Dict [str , Any ]:
605
626
"""Render the given version pieces into the requested style."""
606
627
if pieces ["error" ]:
607
- return {"version" : "unknown" ,
608
- "full-revisionid" : pieces .get ("long" ),
609
- "dirty" : None ,
610
- "error" : pieces ["error" ],
611
- "date" : None }
628
+ return {
629
+ "version" : "unknown" ,
630
+ "full-revisionid" : pieces .get ("long" ),
631
+ "dirty" : None ,
632
+ "error" : pieces ["error" ],
633
+ "date" : None ,
634
+ }
612
635
613
636
if not style or style == "default" :
614
637
style = "pep440" # the default
@@ -632,9 +655,13 @@ def render(pieces: Dict[str, Any], style: str) -> Dict[str, Any]:
632
655
else :
633
656
raise ValueError ("unknown style '%s'" % style )
634
657
635
- return {"version" : rendered , "full-revisionid" : pieces ["long" ],
636
- "dirty" : pieces ["dirty" ], "error" : None ,
637
- "date" : pieces .get ("date" )}
658
+ return {
659
+ "version" : rendered ,
660
+ "full-revisionid" : pieces ["long" ],
661
+ "dirty" : pieces ["dirty" ],
662
+ "error" : None ,
663
+ "date" : pieces .get ("date" ),
664
+ }
638
665
639
666
640
667
def get_versions () -> Dict [str , Any ]:
@@ -648,8 +675,7 @@ def get_versions() -> Dict[str, Any]:
648
675
verbose = cfg .verbose
649
676
650
677
try :
651
- return git_versions_from_keywords (get_keywords (), cfg .tag_prefix ,
652
- verbose )
678
+ return git_versions_from_keywords (get_keywords (), cfg .tag_prefix , verbose )
653
679
except NotThisMethod :
654
680
pass
655
681
@@ -658,13 +684,16 @@ def get_versions() -> Dict[str, Any]:
658
684
# versionfile_source is the relative path from the top of the source
659
685
# tree (where the .git directory might live) to this file. Invert
660
686
# this to find the root from __file__.
661
- for _ in cfg .versionfile_source .split ('/' ):
687
+ for _ in cfg .versionfile_source .split ("/" ):
662
688
root = os .path .dirname (root )
663
689
except NameError :
664
- return {"version" : "0+unknown" , "full-revisionid" : None ,
665
- "dirty" : None ,
666
- "error" : "unable to find root of source tree" ,
667
- "date" : None }
690
+ return {
691
+ "version" : "0+unknown" ,
692
+ "full-revisionid" : None ,
693
+ "dirty" : None ,
694
+ "error" : "unable to find root of source tree" ,
695
+ "date" : None ,
696
+ }
668
697
669
698
try :
670
699
pieces = git_pieces_from_vcs (cfg .tag_prefix , root , verbose )
@@ -678,6 +707,10 @@ def get_versions() -> Dict[str, Any]:
678
707
except NotThisMethod :
679
708
pass
680
709
681
- return {"version" : "0+unknown" , "full-revisionid" : None ,
682
- "dirty" : None ,
683
- "error" : "unable to compute version" , "date" : None }
710
+ return {
711
+ "version" : "0+unknown" ,
712
+ "full-revisionid" : None ,
713
+ "dirty" : None ,
714
+ "error" : "unable to compute version" ,
715
+ "date" : None ,
716
+ }
0 commit comments