8
8
9
9
from setuptools .command .sdist import sdist as sdist_orig
10
10
11
- Version = namedtuple (' Version' , (' release' , ' dev' , ' labels' ))
11
+ Version = namedtuple (" Version" , (" release" , " dev" , " labels" ))
12
12
13
13
# No public API
14
14
__all__ = []
17
17
package_name = os .path .basename (package_root )
18
18
distr_root = os .path .dirname (package_root )
19
19
20
- STATIC_VERSION_FILE = ' _static_version.py'
20
+ STATIC_VERSION_FILE = " _static_version.py"
21
21
22
22
23
23
def get_version (version_file = STATIC_VERSION_FILE ):
24
24
version_info = get_static_version_info (version_file )
25
- version = version_info [' version' ]
25
+ version = version_info [" version" ]
26
26
if version == "__use_git__" :
27
27
version = get_version_from_git ()
28
28
if not version :
@@ -36,43 +36,45 @@ def get_version(version_file=STATIC_VERSION_FILE):
36
36
37
37
def get_static_version_info (version_file = STATIC_VERSION_FILE ):
38
38
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 :
40
40
exec (f .read (), {}, version_info )
41
41
return version_info
42
42
43
43
44
44
def 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__"
46
46
47
47
48
48
def pep440_format (version_info ):
49
49
release , dev , labels = version_info
50
50
51
51
version_parts = [release ]
52
52
if dev :
53
- if release .endswith (' -dev' ) or release .endswith (' .dev' ):
53
+ if release .endswith (" -dev" ) or release .endswith (" .dev" ):
54
54
version_parts .append (dev )
55
55
else : # prefer PEP440 over strict adhesion to semver
56
- version_parts .append (f' .dev{ dev } ' )
56
+ version_parts .append (f" .dev{ dev } " )
57
57
58
58
if labels :
59
- version_parts .append ('+' )
59
+ version_parts .append ("+" )
60
60
version_parts .append ("." .join (labels ))
61
61
62
62
return "" .join (version_parts )
63
63
64
64
65
65
def get_version_from_git ():
66
66
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
+ )
70
73
except OSError :
71
74
return
72
75
if p .wait () != 0 :
73
76
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 ):
76
78
# The top-level directory of the current Git repository is not the same
77
79
# as the root directory of the distribution: do not extract the
78
80
# version from Git.
@@ -81,12 +83,14 @@ def get_version_from_git():
81
83
# git describe --first-parent does not take into account tags from branches
82
84
# that were merged-in. The '--long' flag gets us the 'dev' version and
83
85
# 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" ], []]:
85
87
try :
86
88
p = subprocess .Popen (
87
- [' git' , ' describe' , ' --long' , ' --always' ] + opts ,
89
+ [" git" , " describe" , " --long" , " --always" ] + opts ,
88
90
cwd = distr_root ,
89
- stdout = subprocess .PIPE , stderr = subprocess .PIPE )
91
+ stdout = subprocess .PIPE ,
92
+ stderr = subprocess .PIPE ,
93
+ )
90
94
except OSError :
91
95
return
92
96
if p .wait () == 0 :
@@ -97,17 +101,17 @@ def get_version_from_git():
97
101
description = (
98
102
p .communicate ()[0 ]
99
103
.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
103
107
)
104
108
105
109
try :
106
110
release , dev , git = description
107
111
except ValueError : # No tags, only the git hash
108
112
# 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"
111
115
dev = None
112
116
113
117
labels = []
@@ -117,12 +121,12 @@ def get_version_from_git():
117
121
labels .append (git )
118
122
119
123
try :
120
- p = subprocess .Popen ([' git' , ' diff' , ' --quiet' ], cwd = distr_root )
124
+ p = subprocess .Popen ([" git" , " diff" , " --quiet" ], cwd = distr_root )
121
125
except OSError :
122
- labels .append (' confused' ) # This should never happen.
126
+ labels .append (" confused" ) # This should never happen.
123
127
else :
124
128
if p .wait () == 1 :
125
- labels .append (' dirty' )
129
+ labels .append (" dirty" )
126
130
127
131
return Version (release , dev , labels )
128
132
@@ -134,25 +138,25 @@ def get_version_from_git():
134
138
# if it is not tagged.
135
139
def get_version_from_git_archive (version_info ):
136
140
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" ]
139
143
except KeyError :
140
144
# These fields are not present if we are running from an sdist.
141
145
# Execution should never reach here, though
142
146
return None
143
147
144
- if git_hash .startswith (' $Format' ) or refnames .startswith (' $Format' ):
148
+ if git_hash .startswith (" $Format" ) or refnames .startswith (" $Format" ):
145
149
# variables not expanded during 'git archive'
146
150
return None
147
151
148
- VTAG = ' tag: v'
152
+ VTAG = " tag: v"
149
153
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 )}
151
155
if version_tags :
152
156
release , * _ = sorted (version_tags ) # prefer e.g. "2.0" over "2.0rc1"
153
157
return Version (release , dev = None , labels = None )
154
158
else :
155
- return Version (' unknown' , dev = None , labels = [f' g{ git_hash } ' ])
159
+ return Version (" unknown" , dev = None , labels = [f" g{ git_hash } " ])
156
160
157
161
158
162
__version__ = get_version ()
@@ -162,30 +166,31 @@ def get_version_from_git_archive(version_info):
162
166
# which can be used from setup.py. The 'package_name' and
163
167
# '__version__' module globals are used (but not modified).
164
168
169
+
165
170
def _write_version (fname ):
166
171
# This could be a hard link, so try to delete it first. Is there any way
167
172
# to do this atomically together with opening?
168
173
try :
169
174
os .remove (fname )
170
175
except OSError :
171
176
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
+ )
175
182
176
183
177
184
class _build_py (build_py_orig ):
178
185
def run (self ):
179
186
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 ))
182
188
183
189
184
190
class _sdist (sdist_orig ):
185
191
def make_release_tree (self , base_dir , files ):
186
192
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 ))
189
194
190
195
191
196
cmdclass = dict (sdist = _sdist , build_py = _build_py )
0 commit comments