1-
21# This file helps to compute a version number in source trees obtained from
32# git-archive tarball (such as those provided by githubs download-from-tag
43# feature). Distribution tarballs (built by setup.py sdist) and build
@@ -59,28 +58,32 @@ class NotThisMethod(Exception):
5958
6059def register_vcs_handler (vcs , method ): # decorator
6160 """Create decorator to mark a method as the handler of a VCS."""
61+
6262 def decorate (f ):
6363 """Store f in HANDLERS[vcs][method]."""
6464 if vcs not in HANDLERS :
6565 HANDLERS [vcs ] = {}
6666 HANDLERS [vcs ][method ] = f
6767 return f
68+
6869 return decorate
6970
7071
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 ):
7373 """Call the given command(s)."""
7474 assert isinstance (commands , list )
7575 process = None
7676 for command in commands :
7777 try :
7878 dispcmd = str ([command ] + args )
7979 # 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+ )
8487 break
8588 except OSError :
8689 e = sys .exc_info ()[1 ]
@@ -115,15 +118,21 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
115118 for _ in range (3 ):
116119 dirname = os .path .basename (root )
117120 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+ }
121128 rootdirs .append (root )
122129 root = os .path .dirname (root ) # up a level
123130
124131 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+ )
127136 raise NotThisMethod ("rootdir doesn't start with parentdir_prefix" )
128137
129138
@@ -182,7 +191,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
182191 # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
183192 # just "foo-1.0". If we see a "tag: " prefix, prefer those.
184193 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 )}
186195 if not tags :
187196 # Either we're using git < 1.8.3, or there really are no tags. We use
188197 # 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):
191200 # between branches and tags. By ignoring refnames without digits, we
192201 # filter out many common branch names like "release" and
193202 # "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 )}
195204 if verbose :
196205 print ("discarding '%s', no digits" % "," .join (refs - tags ))
197206 if verbose :
198207 print ("likely tags: %s" % "," .join (sorted (tags )))
199208 for ref in sorted (tags ):
200209 # sorting will prefer e.g. "2.0" over "2.0rc1"
201210 if ref .startswith (tag_prefix ):
202- r = ref [len (tag_prefix ):]
211+ r = ref [len (tag_prefix ) :]
203212 # Filter out refs that exactly match prefix or that don't start
204213 # with a number once the prefix is stripped (mostly a concern
205214 # when prefix is '')
206- if not re .match (r'\d' , r ):
215+ if not re .match (r"\d" , r ):
207216 continue
208217 if verbose :
209218 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+ }
214226 # no suitable tags, so version is "0+unknown", but full hex is still there
215227 if verbose :
216228 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+ }
220236
221237
222238@register_vcs_handler ("git" , "pieces_from_vcs" )
@@ -233,20 +249,27 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
233249 GITS = ["git.cmd" , "git.exe" ]
234250 TAG_PREFIX_REGEX = r"\*"
235251
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 )
238253 if rc != 0 :
239254 if verbose :
240255 print ("Directory %s not under git control" % root )
241256 raise NotThisMethod ("'git rev-parse --git-dir' returned error" )
242257
243258 # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
244259 # 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+ )
250273 # --long was added in git-1.5.5
251274 if describe_out is None :
252275 raise NotThisMethod ("'git describe' failed" )
@@ -261,8 +284,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
261284 pieces ["short" ] = full_out [:7 ] # maybe improved later
262285 pieces ["error" ] = None
263286
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 )
266288 # --abbrev-ref was added in git-1.6.3
267289 if rc != 0 or branch_name is None :
268290 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):
302324 dirty = git_describe .endswith ("-dirty" )
303325 pieces ["dirty" ] = dirty
304326 if dirty :
305- git_describe = git_describe [:git_describe .rindex ("-dirty" )]
327+ git_describe = git_describe [: git_describe .rindex ("-dirty" )]
306328
307329 # now we have TAG-NUM-gHEX or HEX
308330
309331 if "-" in git_describe :
310332 # 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 )
312334 if not mo :
313335 # 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
316337 return pieces
317338
318339 # tag
@@ -321,10 +342,12 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command):
321342 if verbose :
322343 fmt = "tag '%s' doesn't start with prefix '%s'"
323344 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+ )
326349 return pieces
327- pieces ["closest-tag" ] = full_tag [len (tag_prefix ):]
350+ pieces ["closest-tag" ] = full_tag [len (tag_prefix ) :]
328351
329352 # distance: number of commits since tag
330353 pieces ["distance" ] = int (mo .group (2 ))
@@ -373,8 +396,7 @@ def render_pep440(pieces):
373396 rendered += ".dirty"
374397 else :
375398 # 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" ])
378400 if pieces ["dirty" ]:
379401 rendered += ".dirty"
380402 return rendered
@@ -403,8 +425,7 @@ def render_pep440_branch(pieces):
403425 rendered = "0"
404426 if pieces ["branch" ] != "master" :
405427 rendered += ".dev0"
406- rendered += "+untagged.%d.g%s" % (pieces ["distance" ],
407- pieces ["short" ])
428+ rendered += "+untagged.%d.g%s" % (pieces ["distance" ], pieces ["short" ])
408429 if pieces ["dirty" ]:
409430 rendered += ".dirty"
410431 return rendered
@@ -432,7 +453,7 @@ def render_pep440_pre(pieces):
432453 tag_version , post_version = pep440_split_post (pieces ["closest-tag" ])
433454 rendered = tag_version
434455 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" ])
436457 else :
437458 rendered += ".post0.dev%d" % (pieces ["distance" ])
438459 else :
@@ -565,11 +586,13 @@ def render_git_describe_long(pieces):
565586def render (pieces , style ):
566587 """Render the given version pieces into the requested style."""
567588 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+ }
573596
574597 if not style or style == "default" :
575598 style = "pep440" # the default
@@ -593,9 +616,13 @@ def render(pieces, style):
593616 else :
594617 raise ValueError ("unknown style '%s'" % style )
595618
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+ }
599626
600627
601628def get_versions ():
@@ -609,8 +636,7 @@ def get_versions():
609636 verbose = cfg .verbose
610637
611638 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 )
614640 except NotThisMethod :
615641 pass
616642
@@ -619,13 +645,16 @@ def get_versions():
619645 # versionfile_source is the relative path from the top of the source
620646 # tree (where the .git directory might live) to this file. Invert
621647 # this to find the root from __file__.
622- for _ in cfg .versionfile_source .split ('/' ):
648+ for _ in cfg .versionfile_source .split ("/" ):
623649 root = os .path .dirname (root )
624650 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+ }
629658
630659 try :
631660 pieces = git_pieces_from_vcs (cfg .tag_prefix , root , verbose )
@@ -639,6 +668,10 @@ def get_versions():
639668 except NotThisMethod :
640669 pass
641670
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