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
@@ -58,28 +57,32 @@ class NotThisMethod(Exception):
5857
5958def register_vcs_handler (vcs , method ): # decorator
6059 """Create decorator to mark a method as the handler of a VCS."""
60+
6161 def decorate (f ):
6262 """Store f in HANDLERS[vcs][method]."""
6363 if vcs not in HANDLERS :
6464 HANDLERS [vcs ] = {}
6565 HANDLERS [vcs ][method ] = f
6666 return f
67+
6768 return decorate
6869
6970
70- def run_command (commands , args , cwd = None , verbose = False , hide_stderr = False ,
71- env = None ):
71+ def run_command (commands , args , cwd = None , verbose = False , hide_stderr = False , env = None ):
7272 """Call the given command(s)."""
7373 assert isinstance (commands , list )
7474 p = None
7575 for c in commands :
7676 try :
7777 dispcmd = str ([c ] + args )
7878 # remember shell=False, so use git.cmd on windows, not just git
79- p = subprocess .Popen ([c ] + args , cwd = cwd , env = env ,
80- stdout = subprocess .PIPE ,
81- stderr = (subprocess .PIPE if hide_stderr
82- else None ))
79+ p = subprocess .Popen (
80+ [c ] + args ,
81+ cwd = cwd ,
82+ env = env ,
83+ stdout = subprocess .PIPE ,
84+ stderr = (subprocess .PIPE if hide_stderr else None ),
85+ )
8386 break
8487 except EnvironmentError :
8588 e = sys .exc_info ()[1 ]
@@ -114,16 +117,22 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
114117 for i in range (3 ):
115118 dirname = os .path .basename (root )
116119 if dirname .startswith (parentdir_prefix ):
117- return {"version" : dirname [len (parentdir_prefix ):],
118- "full-revisionid" : None ,
119- "dirty" : False , "error" : None , "date" : None }
120+ return {
121+ "version" : dirname [len (parentdir_prefix ) :],
122+ "full-revisionid" : None ,
123+ "dirty" : False ,
124+ "error" : None ,
125+ "date" : None ,
126+ }
120127 else :
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
@@ -183,7 +192,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
183192 # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
184193 # just "foo-1.0". If we see a "tag: " prefix, prefer those.
185194 TAG = "tag: "
186- tags = set ([r [len (TAG ):] for r in refs if r .startswith (TAG )])
195+ tags = set ([r [len (TAG ) :] for r in refs if r .startswith (TAG )])
187196 if not tags :
188197 # Either we're using git < 1.8.3, or there really are no tags. We use
189198 # a heuristic: assume all version tags have a digit. The old git %d
@@ -192,27 +201,34 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
192201 # between branches and tags. By ignoring refnames without digits, we
193202 # filter out many common branch names like "release" and
194203 # "stabilization", as well as "HEAD" and "master".
195- tags = set ([r for r in refs if re .search (r'\d' , r )])
204+ tags = set ([r for r in refs if re .search (r"\d" , r )])
196205 if verbose :
197206 print ("discarding '%s', no digits" % "," .join (refs - tags ))
198207 if verbose :
199208 print ("likely tags: %s" % "," .join (sorted (tags )))
200209 for ref in sorted (tags ):
201210 # sorting will prefer e.g. "2.0" over "2.0rc1"
202211 if ref .startswith (tag_prefix ):
203- r = ref [len (tag_prefix ):]
212+ r = ref [len (tag_prefix ) :]
204213 if verbose :
205214 print ("picking %s" % r )
206- return {"version" : r ,
207- "full-revisionid" : keywords ["full" ].strip (),
208- "dirty" : False , "error" : None ,
209- "date" : date }
215+ return {
216+ "version" : r ,
217+ "full-revisionid" : keywords ["full" ].strip (),
218+ "dirty" : False ,
219+ "error" : None ,
220+ "date" : date ,
221+ }
210222 # no suitable tags, so version is "0+unknown", but full hex is still there
211223 if verbose :
212224 print ("no suitable tags, using unknown + full revision id" )
213- return {"version" : "0+unknown" ,
214- "full-revisionid" : keywords ["full" ].strip (),
215- "dirty" : False , "error" : "no suitable tags" , "date" : None }
225+ return {
226+ "version" : "0+unknown" ,
227+ "full-revisionid" : keywords ["full" ].strip (),
228+ "dirty" : False ,
229+ "error" : "no suitable tags" ,
230+ "date" : None ,
231+ }
216232
217233
218234@register_vcs_handler ("git" , "pieces_from_vcs" )
@@ -227,19 +243,27 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
227243 if sys .platform == "win32" :
228244 GITS = ["git.cmd" , "git.exe" ]
229245
230- out , rc = run_command (GITS , ["rev-parse" , "--git-dir" ], cwd = root ,
231- hide_stderr = True )
246+ out , rc = run_command (GITS , ["rev-parse" , "--git-dir" ], cwd = root , hide_stderr = True )
232247 if rc != 0 :
233248 if verbose :
234249 print ("Directory %s not under git control" % root )
235250 raise NotThisMethod ("'git rev-parse --git-dir' returned error" )
236251
237252 # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
238253 # if there isn't one, this yields HEX[-dirty] (no NUM)
239- describe_out , rc = run_command (GITS , ["describe" , "--tags" , "--dirty" ,
240- "--always" , "--long" ,
241- "--match" , "%s*" % tag_prefix ],
242- cwd = root )
254+ describe_out , rc = run_command (
255+ GITS ,
256+ [
257+ "describe" ,
258+ "--tags" ,
259+ "--dirty" ,
260+ "--always" ,
261+ "--long" ,
262+ "--match" ,
263+ "%s*" % tag_prefix ,
264+ ],
265+ cwd = root ,
266+ )
243267 # --long was added in git-1.5.5
244268 if describe_out is None :
245269 raise NotThisMethod ("'git describe' failed" )
@@ -262,17 +286,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
262286 dirty = git_describe .endswith ("-dirty" )
263287 pieces ["dirty" ] = dirty
264288 if dirty :
265- git_describe = git_describe [:git_describe .rindex ("-dirty" )]
289+ git_describe = git_describe [: git_describe .rindex ("-dirty" )]
266290
267291 # now we have TAG-NUM-gHEX or HEX
268292
269293 if "-" in git_describe :
270294 # TAG-NUM-gHEX
271- mo = re .search (r' ^(.+)-(\d+)-g([0-9a-f]+)$' , git_describe )
295+ mo = re .search (r" ^(.+)-(\d+)-g([0-9a-f]+)$" , git_describe )
272296 if not mo :
273297 # unparseable. Maybe git-describe is misbehaving?
274- pieces ["error" ] = ("unable to parse git-describe output: '%s'"
275- % describe_out )
298+ pieces ["error" ] = "unable to parse git-describe output: '%s'" % describe_out
276299 return pieces
277300
278301 # tag
@@ -281,10 +304,12 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
281304 if verbose :
282305 fmt = "tag '%s' doesn't start with prefix '%s'"
283306 print (fmt % (full_tag , tag_prefix ))
284- pieces ["error" ] = ("tag '%s' doesn't start with prefix '%s'"
285- % (full_tag , tag_prefix ))
307+ pieces ["error" ] = "tag '%s' doesn't start with prefix '%s'" % (
308+ full_tag ,
309+ tag_prefix ,
310+ )
286311 return pieces
287- pieces ["closest-tag" ] = full_tag [len (tag_prefix ):]
312+ pieces ["closest-tag" ] = full_tag [len (tag_prefix ) :]
288313
289314 # distance: number of commits since tag
290315 pieces ["distance" ] = int (mo .group (2 ))
@@ -295,13 +320,13 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
295320 else :
296321 # HEX: no tags
297322 pieces ["closest-tag" ] = None
298- count_out , rc = run_command (GITS , ["rev-list" , "HEAD" , "--count" ],
299- cwd = root )
323+ count_out , rc = run_command (GITS , ["rev-list" , "HEAD" , "--count" ], cwd = root )
300324 pieces ["distance" ] = int (count_out ) # total number of commits
301325
302326 # commit date: see ISO-8601 comment in git_versions_from_keywords()
303- date = run_command (GITS , ["show" , "-s" , "--format=%ci" , "HEAD" ],
304- cwd = root )[0 ].strip ()
327+ date = run_command (GITS , ["show" , "-s" , "--format=%ci" , "HEAD" ], cwd = root )[
328+ 0
329+ ].strip ()
305330 # Use only the last line. Previous lines may contain GPG signature
306331 # information.
307332 date = date .splitlines ()[- 1 ]
@@ -335,8 +360,7 @@ def render_pep440(pieces):
335360 rendered += ".dirty"
336361 else :
337362 # exception #1
338- rendered = "0+untagged.%d.g%s" % (pieces ["distance" ],
339- pieces ["short" ])
363+ rendered = "0+untagged.%d.g%s" % (pieces ["distance" ], pieces ["short" ])
340364 if pieces ["dirty" ]:
341365 rendered += ".dirty"
342366 return rendered
@@ -450,11 +474,13 @@ def render_git_describe_long(pieces):
450474def render (pieces , style ):
451475 """Render the given version pieces into the requested style."""
452476 if pieces ["error" ]:
453- return {"version" : "unknown" ,
454- "full-revisionid" : pieces .get ("long" ),
455- "dirty" : None ,
456- "error" : pieces ["error" ],
457- "date" : None }
477+ return {
478+ "version" : "unknown" ,
479+ "full-revisionid" : pieces .get ("long" ),
480+ "dirty" : None ,
481+ "error" : pieces ["error" ],
482+ "date" : None ,
483+ }
458484
459485 if not style or style == "default" :
460486 style = "pep440" # the default
@@ -474,9 +500,13 @@ def render(pieces, style):
474500 else :
475501 raise ValueError ("unknown style '%s'" % style )
476502
477- return {"version" : rendered , "full-revisionid" : pieces ["long" ],
478- "dirty" : pieces ["dirty" ], "error" : None ,
479- "date" : pieces .get ("date" )}
503+ return {
504+ "version" : rendered ,
505+ "full-revisionid" : pieces ["long" ],
506+ "dirty" : pieces ["dirty" ],
507+ "error" : None ,
508+ "date" : pieces .get ("date" ),
509+ }
480510
481511
482512def get_versions ():
@@ -490,8 +520,7 @@ def get_versions():
490520 verbose = cfg .verbose
491521
492522 try :
493- return git_versions_from_keywords (get_keywords (), cfg .tag_prefix ,
494- verbose )
523+ return git_versions_from_keywords (get_keywords (), cfg .tag_prefix , verbose )
495524 except NotThisMethod :
496525 pass
497526
@@ -500,13 +529,16 @@ def get_versions():
500529 # versionfile_source is the relative path from the top of the source
501530 # tree (where the .git directory might live) to this file. Invert
502531 # this to find the root from __file__.
503- for i in cfg .versionfile_source .split ('/' ):
532+ for i in cfg .versionfile_source .split ("/" ):
504533 root = os .path .dirname (root )
505534 except NameError :
506- return {"version" : "0+unknown" , "full-revisionid" : None ,
507- "dirty" : None ,
508- "error" : "unable to find root of source tree" ,
509- "date" : None }
535+ return {
536+ "version" : "0+unknown" ,
537+ "full-revisionid" : None ,
538+ "dirty" : None ,
539+ "error" : "unable to find root of source tree" ,
540+ "date" : None ,
541+ }
510542
511543 try :
512544 pieces = git_pieces_from_vcs (cfg .tag_prefix , root , verbose )
@@ -520,6 +552,10 @@ def get_versions():
520552 except NotThisMethod :
521553 pass
522554
523- return {"version" : "0+unknown" , "full-revisionid" : None ,
524- "dirty" : None ,
525- "error" : "unable to compute version" , "date" : None }
555+ return {
556+ "version" : "0+unknown" ,
557+ "full-revisionid" : None ,
558+ "dirty" : None ,
559+ "error" : "unable to compute version" ,
560+ "date" : None ,
561+ }
0 commit comments