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
@@ -59,28 +58,32 @@ class NotThisMethod(Exception):
59
58
60
59
def register_vcs_handler (vcs , method ): # decorator
61
60
"""Create decorator to mark a method as the handler of a VCS."""
61
+
62
62
def decorate (f ):
63
63
"""Store f in HANDLERS[vcs][method]."""
64
64
if vcs not in HANDLERS :
65
65
HANDLERS [vcs ] = {}
66
66
HANDLERS [vcs ][method ] = f
67
67
return f
68
+
68
69
return decorate
69
70
70
71
71
- def run_command (commands , args , cwd = None , verbose = False , hide_stderr = False ,
72
- env = None ):
72
+ def run_command (commands , args , cwd = None , verbose = False , hide_stderr = False , env = None ):
73
73
"""Call the given command(s)."""
74
74
assert isinstance (commands , list )
75
75
process = None
76
76
for command in commands :
77
77
try :
78
78
dispcmd = str ([command ] + args )
79
79
# remember shell=False, so use git.cmd on windows, not just git
80
- process = subprocess .Popen ([command ] + args , cwd = cwd , env = env ,
81
- stdout = subprocess .PIPE ,
82
- stderr = (subprocess .PIPE if hide_stderr
83
- else None ))
80
+ process = subprocess .Popen (
81
+ [command ] + args ,
82
+ cwd = cwd ,
83
+ env = env ,
84
+ stdout = subprocess .PIPE ,
85
+ stderr = (subprocess .PIPE if hide_stderr else None ),
86
+ )
84
87
break
85
88
except OSError :
86
89
e = sys .exc_info ()[1 ]
@@ -115,15 +118,21 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
115
118
for _ in range (3 ):
116
119
dirname = os .path .basename (root )
117
120
if dirname .startswith (parentdir_prefix ):
118
- return {"version" : dirname [len (parentdir_prefix ):],
119
- "full-revisionid" : None ,
120
- "dirty" : False , "error" : None , "date" : None }
121
+ return {
122
+ "version" : dirname [len (parentdir_prefix ) :],
123
+ "full-revisionid" : None ,
124
+ "dirty" : False ,
125
+ "error" : None ,
126
+ "date" : None ,
127
+ }
121
128
rootdirs .append (root )
122
129
root = os .path .dirname (root ) # up a level
123
130
124
131
if verbose :
125
- print ("Tried directories %s but none started with prefix %s" %
126
- (str (rootdirs ), parentdir_prefix ))
132
+ print (
133
+ "Tried directories %s but none started with prefix %s"
134
+ % (str (rootdirs ), parentdir_prefix )
135
+ )
127
136
raise NotThisMethod ("rootdir doesn't start with parentdir_prefix" )
128
137
129
138
@@ -182,7 +191,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
182
191
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
183
192
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
184
193
TAG = "tag: "
185
- tags = {r [len (TAG ):] for r in refs if r .startswith (TAG )}
194
+ tags = {r [len (TAG ) :] for r in refs if r .startswith (TAG )}
186
195
if not tags :
187
196
# Either we're using git < 1.8.3, or there really are no tags. We use
188
197
# a heuristic: assume all version tags have a digit. The old git %d
@@ -191,32 +200,39 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
191
200
# between branches and tags. By ignoring refnames without digits, we
192
201
# filter out many common branch names like "release" and
193
202
# "stabilization", as well as "HEAD" and "master".
194
- tags = {r for r in refs if re .search (r'\d' , r )}
203
+ tags = {r for r in refs if re .search (r"\d" , r )}
195
204
if verbose :
196
205
print ("discarding '%s', no digits" % "," .join (refs - tags ))
197
206
if verbose :
198
207
print ("likely tags: %s" % "," .join (sorted (tags )))
199
208
for ref in sorted (tags ):
200
209
# sorting will prefer e.g. "2.0" over "2.0rc1"
201
210
if ref .startswith (tag_prefix ):
202
- r = ref [len (tag_prefix ):]
211
+ r = ref [len (tag_prefix ) :]
203
212
# Filter out refs that exactly match prefix or that don't start
204
213
# with a number once the prefix is stripped (mostly a concern
205
214
# when prefix is '')
206
- if not re .match (r'\d' , r ):
215
+ if not re .match (r"\d" , r ):
207
216
continue
208
217
if verbose :
209
218
print ("picking %s" % r )
210
- return {"version" : r ,
211
- "full-revisionid" : keywords ["full" ].strip (),
212
- "dirty" : False , "error" : None ,
213
- "date" : date }
219
+ return {
220
+ "version" : r ,
221
+ "full-revisionid" : keywords ["full" ].strip (),
222
+ "dirty" : False ,
223
+ "error" : None ,
224
+ "date" : date ,
225
+ }
214
226
# no suitable tags, so version is "0+unknown", but full hex is still there
215
227
if verbose :
216
228
print ("no suitable tags, using unknown + full revision id" )
217
- return {"version" : "0+unknown" ,
218
- "full-revisionid" : keywords ["full" ].strip (),
219
- "dirty" : False , "error" : "no suitable tags" , "date" : None }
229
+ return {
230
+ "version" : "0+unknown" ,
231
+ "full-revisionid" : keywords ["full" ].strip (),
232
+ "dirty" : False ,
233
+ "error" : "no suitable tags" ,
234
+ "date" : None ,
235
+ }
220
236
221
237
222
238
@register_vcs_handler ("git" , "pieces_from_vcs" )
@@ -233,20 +249,27 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
233
249
GITS = ["git.cmd" , "git.exe" ]
234
250
TAG_PREFIX_REGEX = r"\*"
235
251
236
- _ , rc = runner (GITS , ["rev-parse" , "--git-dir" ], cwd = root ,
237
- hide_stderr = True )
252
+ _ , rc = runner (GITS , ["rev-parse" , "--git-dir" ], cwd = root , hide_stderr = True )
238
253
if rc != 0 :
239
254
if verbose :
240
255
print ("Directory %s not under git control" % root )
241
256
raise NotThisMethod ("'git rev-parse --git-dir' returned error" )
242
257
243
258
# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
244
259
# if there isn't one, this yields HEX[-dirty] (no NUM)
245
- describe_out , rc = runner (GITS , ["describe" , "--tags" , "--dirty" ,
246
- "--always" , "--long" ,
247
- "--match" ,
248
- "%s%s" % (tag_prefix , TAG_PREFIX_REGEX )],
249
- cwd = root )
260
+ describe_out , rc = runner (
261
+ GITS ,
262
+ [
263
+ "describe" ,
264
+ "--tags" ,
265
+ "--dirty" ,
266
+ "--always" ,
267
+ "--long" ,
268
+ "--match" ,
269
+ "%s%s" % (tag_prefix , TAG_PREFIX_REGEX ),
270
+ ],
271
+ cwd = root ,
272
+ )
250
273
# --long was added in git-1.5.5
251
274
if describe_out is None :
252
275
raise NotThisMethod ("'git describe' failed" )
@@ -261,8 +284,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
261
284
pieces ["short" ] = full_out [:7 ] # maybe improved later
262
285
pieces ["error" ] = None
263
286
264
- branch_name , rc = runner (GITS , ["rev-parse" , "--abbrev-ref" , "HEAD" ],
265
- cwd = root )
287
+ branch_name , rc = runner (GITS , ["rev-parse" , "--abbrev-ref" , "HEAD" ], cwd = root )
266
288
# --abbrev-ref was added in git-1.6.3
267
289
if rc != 0 or branch_name is None :
268
290
raise NotThisMethod ("'git rev-parse --abbrev-ref' returned error" )
@@ -302,17 +324,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
302
324
dirty = git_describe .endswith ("-dirty" )
303
325
pieces ["dirty" ] = dirty
304
326
if dirty :
305
- git_describe = git_describe [:git_describe .rindex ("-dirty" )]
327
+ git_describe = git_describe [: git_describe .rindex ("-dirty" )]
306
328
307
329
# now we have TAG-NUM-gHEX or HEX
308
330
309
331
if "-" in git_describe :
310
332
# TAG-NUM-gHEX
311
- mo = re .search (r' ^(.+)-(\d+)-g([0-9a-f]+)$' , git_describe )
333
+ mo = re .search (r" ^(.+)-(\d+)-g([0-9a-f]+)$" , git_describe )
312
334
if not mo :
313
335
# unparsable. Maybe git-describe is misbehaving?
314
- pieces ["error" ] = ("unable to parse git-describe output: '%s'"
315
- % describe_out )
336
+ pieces ["error" ] = "unable to parse git-describe output: '%s'" % describe_out
316
337
return pieces
317
338
318
339
# tag
@@ -321,10 +342,12 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
321
342
if verbose :
322
343
fmt = "tag '%s' doesn't start with prefix '%s'"
323
344
print (fmt % (full_tag , tag_prefix ))
324
- pieces ["error" ] = ("tag '%s' doesn't start with prefix '%s'"
325
- % (full_tag , tag_prefix ))
345
+ pieces ["error" ] = "tag '%s' doesn't start with prefix '%s'" % (
346
+ full_tag ,
347
+ tag_prefix ,
348
+ )
326
349
return pieces
327
- pieces ["closest-tag" ] = full_tag [len (tag_prefix ):]
350
+ pieces ["closest-tag" ] = full_tag [len (tag_prefix ) :]
328
351
329
352
# distance: number of commits since tag
330
353
pieces ["distance" ] = int (mo .group (2 ))
@@ -373,8 +396,7 @@ def render_pep440(pieces):
373
396
rendered += ".dirty"
374
397
else :
375
398
# exception #1
376
- rendered = "0+untagged.%d.g%s" % (pieces ["distance" ],
377
- pieces ["short" ])
399
+ rendered = "0+untagged.%d.g%s" % (pieces ["distance" ], pieces ["short" ])
378
400
if pieces ["dirty" ]:
379
401
rendered += ".dirty"
380
402
return rendered
@@ -403,8 +425,7 @@ def render_pep440_branch(pieces):
403
425
rendered = "0"
404
426
if pieces ["branch" ] != "master" :
405
427
rendered += ".dev0"
406
- rendered += "+untagged.%d.g%s" % (pieces ["distance" ],
407
- pieces ["short" ])
428
+ rendered += "+untagged.%d.g%s" % (pieces ["distance" ], pieces ["short" ])
408
429
if pieces ["dirty" ]:
409
430
rendered += ".dirty"
410
431
return rendered
@@ -432,7 +453,7 @@ def render_pep440_pre(pieces):
432
453
tag_version , post_version = pep440_split_post (pieces ["closest-tag" ])
433
454
rendered = tag_version
434
455
if post_version is not None :
435
- rendered += ".post%d.dev%d" % (post_version + 1 , pieces ["distance" ])
456
+ rendered += ".post%d.dev%d" % (post_version + 1 , pieces ["distance" ])
436
457
else :
437
458
rendered += ".post0.dev%d" % (pieces ["distance" ])
438
459
else :
@@ -565,11 +586,13 @@ def render_git_describe_long(pieces):
565
586
def render (pieces , style ):
566
587
"""Render the given version pieces into the requested style."""
567
588
if pieces ["error" ]:
568
- return {"version" : "unknown" ,
569
- "full-revisionid" : pieces .get ("long" ),
570
- "dirty" : None ,
571
- "error" : pieces ["error" ],
572
- "date" : None }
589
+ return {
590
+ "version" : "unknown" ,
591
+ "full-revisionid" : pieces .get ("long" ),
592
+ "dirty" : None ,
593
+ "error" : pieces ["error" ],
594
+ "date" : None ,
595
+ }
573
596
574
597
if not style or style == "default" :
575
598
style = "pep440" # the default
@@ -593,9 +616,13 @@ def render(pieces, style):
593
616
else :
594
617
raise ValueError ("unknown style '%s'" % style )
595
618
596
- return {"version" : rendered , "full-revisionid" : pieces ["long" ],
597
- "dirty" : pieces ["dirty" ], "error" : None ,
598
- "date" : pieces .get ("date" )}
619
+ return {
620
+ "version" : rendered ,
621
+ "full-revisionid" : pieces ["long" ],
622
+ "dirty" : pieces ["dirty" ],
623
+ "error" : None ,
624
+ "date" : pieces .get ("date" ),
625
+ }
599
626
600
627
601
628
def get_versions ():
@@ -609,8 +636,7 @@ def get_versions():
609
636
verbose = cfg .verbose
610
637
611
638
try :
612
- return git_versions_from_keywords (get_keywords (), cfg .tag_prefix ,
613
- verbose )
639
+ return git_versions_from_keywords (get_keywords (), cfg .tag_prefix , verbose )
614
640
except NotThisMethod :
615
641
pass
616
642
@@ -619,13 +645,16 @@ def get_versions():
619
645
# versionfile_source is the relative path from the top of the source
620
646
# tree (where the .git directory might live) to this file. Invert
621
647
# this to find the root from __file__.
622
- for _ in cfg .versionfile_source .split ('/' ):
648
+ for _ in cfg .versionfile_source .split ("/" ):
623
649
root = os .path .dirname (root )
624
650
except NameError :
625
- return {"version" : "0+unknown" , "full-revisionid" : None ,
626
- "dirty" : None ,
627
- "error" : "unable to find root of source tree" ,
628
- "date" : None }
651
+ return {
652
+ "version" : "0+unknown" ,
653
+ "full-revisionid" : None ,
654
+ "dirty" : None ,
655
+ "error" : "unable to find root of source tree" ,
656
+ "date" : None ,
657
+ }
629
658
630
659
try :
631
660
pieces = git_pieces_from_vcs (cfg .tag_prefix , root , verbose )
@@ -639,6 +668,10 @@ def get_versions():
639
668
except NotThisMethod :
640
669
pass
641
670
642
- return {"version" : "0+unknown" , "full-revisionid" : None ,
643
- "dirty" : None ,
644
- "error" : "unable to compute version" , "date" : None }
671
+ return {
672
+ "version" : "0+unknown" ,
673
+ "full-revisionid" : None ,
674
+ "dirty" : None ,
675
+ "error" : "unable to compute version" ,
676
+ "date" : None ,
677
+ }
0 commit comments