88
99from setuptools .command .sdist import sdist as sdist_orig
1010
11- Version = namedtuple (' Version' , (' release' , ' dev' , ' labels' ))
11+ Version = namedtuple (" Version" , (" release" , " dev" , " labels" ))
1212
1313# No public API
1414__all__ = []
1717package_name = os .path .basename (package_root )
1818distr_root = os .path .dirname (package_root )
1919
20- STATIC_VERSION_FILE = ' _static_version.py'
20+ STATIC_VERSION_FILE = " _static_version.py"
2121
2222
2323def get_version (version_file = STATIC_VERSION_FILE ):
2424 version_info = get_static_version_info (version_file )
25- version = version_info [' version' ]
25+ version = version_info [" version" ]
2626 if version == "__use_git__" :
2727 version = get_version_from_git ()
2828 if not version :
@@ -36,43 +36,45 @@ def get_version(version_file=STATIC_VERSION_FILE):
3636
3737def get_static_version_info (version_file = STATIC_VERSION_FILE ):
3838 version_info = {}
39- with open (os .path .join (package_root , version_file ), 'rb' ) as f :
39+ with open (os .path .join (package_root , version_file ), "rb" ) as f :
4040 exec (f .read (), {}, version_info )
4141 return version_info
4242
4343
4444def version_is_from_git (version_file = STATIC_VERSION_FILE ):
45- return get_static_version_info (version_file )[' version' ] == ' __use_git__'
45+ return get_static_version_info (version_file )[" version" ] == " __use_git__"
4646
4747
4848def pep440_format (version_info ):
4949 release , dev , labels = version_info
5050
5151 version_parts = [release ]
5252 if dev :
53- if release .endswith (' -dev' ) or release .endswith (' .dev' ):
53+ if release .endswith (" -dev" ) or release .endswith (" .dev" ):
5454 version_parts .append (dev )
5555 else : # prefer PEP440 over strict adhesion to semver
56- version_parts .append (f' .dev{ dev } ' )
56+ version_parts .append (f" .dev{ dev } " )
5757
5858 if labels :
59- version_parts .append ('+' )
59+ version_parts .append ("+" )
6060 version_parts .append ("." .join (labels ))
6161
6262 return "" .join (version_parts )
6363
6464
6565def get_version_from_git ():
6666 try :
67- p = subprocess .Popen (['git' , 'rev-parse' , '--show-toplevel' ],
68- cwd = distr_root ,
69- stdout = subprocess .PIPE , stderr = subprocess .PIPE )
67+ p = subprocess .Popen (
68+ ["git" , "rev-parse" , "--show-toplevel" ],
69+ cwd = distr_root ,
70+ stdout = subprocess .PIPE ,
71+ stderr = subprocess .PIPE ,
72+ )
7073 except OSError :
7174 return
7275 if p .wait () != 0 :
7376 return
74- if not os .path .samefile (p .communicate ()[0 ].decode ().rstrip ('\n ' ),
75- distr_root ):
77+ if not os .path .samefile (p .communicate ()[0 ].decode ().rstrip ("\n " ), distr_root ):
7678 # The top-level directory of the current Git repository is not the same
7779 # as the root directory of the distribution: do not extract the
7880 # version from Git.
@@ -81,12 +83,14 @@ def get_version_from_git():
8183 # git describe --first-parent does not take into account tags from branches
8284 # that were merged-in. The '--long' flag gets us the 'dev' version and
8385 # git hash, '--always' returns the git hash even if there are no tags.
84- for opts in [[' --first-parent' ], []]:
86+ for opts in [[" --first-parent" ], []]:
8587 try :
8688 p = subprocess .Popen (
87- [' git' , ' describe' , ' --long' , ' --always' ] + opts ,
89+ [" git" , " describe" , " --long" , " --always" ] + opts ,
8890 cwd = distr_root ,
89- stdout = subprocess .PIPE , stderr = subprocess .PIPE )
91+ stdout = subprocess .PIPE ,
92+ stderr = subprocess .PIPE ,
93+ )
9094 except OSError :
9195 return
9296 if p .wait () == 0 :
@@ -97,17 +101,17 @@ def get_version_from_git():
97101 description = (
98102 p .communicate ()[0 ]
99103 .decode ()
100- .strip ('v' ) # Tags can have a leading 'v', but the version should not
101- .rstrip (' \n ' )
102- .rsplit ('-' , 2 ) # Split the latest tag, commits since tag, and hash
104+ .strip ("v" ) # Tags can have a leading 'v', but the version should not
105+ .rstrip (" \n " )
106+ .rsplit ("-" , 2 ) # Split the latest tag, commits since tag, and hash
103107 )
104108
105109 try :
106110 release , dev , git = description
107111 except ValueError : # No tags, only the git hash
108112 # prepend 'g' to match with format returned by 'git describe'
109- git = ' g{}' .format (* description )
110- release = ' unknown'
113+ git = " g{}" .format (* description )
114+ release = " unknown"
111115 dev = None
112116
113117 labels = []
@@ -117,12 +121,12 @@ def get_version_from_git():
117121 labels .append (git )
118122
119123 try :
120- p = subprocess .Popen ([' git' , ' diff' , ' --quiet' ], cwd = distr_root )
124+ p = subprocess .Popen ([" git" , " diff" , " --quiet" ], cwd = distr_root )
121125 except OSError :
122- labels .append (' confused' ) # This should never happen.
126+ labels .append (" confused" ) # This should never happen.
123127 else :
124128 if p .wait () == 1 :
125- labels .append (' dirty' )
129+ labels .append (" dirty" )
126130
127131 return Version (release , dev , labels )
128132
@@ -134,25 +138,25 @@ def get_version_from_git():
134138# if it is not tagged.
135139def get_version_from_git_archive (version_info ):
136140 try :
137- refnames = version_info [' refnames' ]
138- git_hash = version_info [' git_hash' ]
141+ refnames = version_info [" refnames" ]
142+ git_hash = version_info [" git_hash" ]
139143 except KeyError :
140144 # These fields are not present if we are running from an sdist.
141145 # Execution should never reach here, though
142146 return None
143147
144- if git_hash .startswith (' $Format' ) or refnames .startswith (' $Format' ):
148+ if git_hash .startswith (" $Format" ) or refnames .startswith (" $Format" ):
145149 # variables not expanded during 'git archive'
146150 return None
147151
148- VTAG = ' tag: v'
152+ VTAG = " tag: v"
149153 refs = {r .strip () for r in refnames .split ("," )}
150- version_tags = {r [len (VTAG ):] for r in refs if r .startswith (VTAG )}
154+ version_tags = {r [len (VTAG ) :] for r in refs if r .startswith (VTAG )}
151155 if version_tags :
152156 release , * _ = sorted (version_tags ) # prefer e.g. "2.0" over "2.0rc1"
153157 return Version (release , dev = None , labels = None )
154158 else :
155- return Version (' unknown' , dev = None , labels = [f' g{ git_hash } ' ])
159+ return Version (" unknown" , dev = None , labels = [f" g{ git_hash } " ])
156160
157161
158162__version__ = get_version ()
@@ -162,30 +166,31 @@ def get_version_from_git_archive(version_info):
162166# which can be used from setup.py. The 'package_name' and
163167# '__version__' module globals are used (but not modified).
164168
169+
165170def _write_version (fname ):
166171 # This could be a hard link, so try to delete it first. Is there any way
167172 # to do this atomically together with opening?
168173 try :
169174 os .remove (fname )
170175 except OSError :
171176 pass
172- with open (fname , 'w' ) as f :
173- f .write ("# This file has been created by setup.py.\n "
174- "version = '{}'\n " .format (__version__ ))
177+ with open (fname , "w" ) as f :
178+ f .write (
179+ "# This file has been created by setup.py.\n "
180+ "version = '{}'\n " .format (__version__ )
181+ )
175182
176183
177184class _build_py (build_py_orig ):
178185 def run (self ):
179186 super ().run ()
180- _write_version (os .path .join (self .build_lib , package_name ,
181- STATIC_VERSION_FILE ))
187+ _write_version (os .path .join (self .build_lib , package_name , STATIC_VERSION_FILE ))
182188
183189
184190class _sdist (sdist_orig ):
185191 def make_release_tree (self , base_dir , files ):
186192 super ().make_release_tree (base_dir , files )
187- _write_version (os .path .join (base_dir , package_name ,
188- STATIC_VERSION_FILE ))
193+ _write_version (os .path .join (base_dir , package_name , STATIC_VERSION_FILE ))
189194
190195
191196cmdclass = dict (sdist = _sdist , build_py = _build_py )
0 commit comments