Skip to content

Commit 67e413a

Browse files
committed
Fixes setup compatibility issue on Windows
Fixes Bug #1052161 "python setup.py build" fails on Windows due to a hardcoded shell path: /bin/sh setup.py updated using openstack-common/update.py Change-Id: I33d38e0f96b6d124248c4a31959952d61cf1eb16
1 parent fd85c7e commit 67e413a

1 file changed

Lines changed: 63 additions & 39 deletions

File tree

  • openstackclient/openstack/common

openstackclient/openstack/common/setup.py

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
def parse_mailmap(mailmap='.mailmap'):
3232
mapping = {}
3333
if os.path.exists(mailmap):
34-
fp = open(mailmap, 'r')
35-
for l in fp:
36-
l = l.strip()
37-
if not l.startswith('#') and ' ' in l:
38-
canonical_email, alias = l.split(' ')
39-
mapping[alias] = canonical_email
34+
with open(mailmap, 'r') as fp:
35+
for l in fp:
36+
l = l.strip()
37+
if not l.startswith('#') and ' ' in l:
38+
canonical_email, alias = [x for x in l.split(' ')
39+
if x.startswith('<')]
40+
mapping[alias] = canonical_email
4041
return mapping
4142

4243

@@ -51,10 +52,10 @@ def canonicalize_emails(changelog, mapping):
5152

5253
# Get requirements from the first file that exists
5354
def get_reqs_from_files(requirements_files):
54-
reqs_in = []
5555
for requirements_file in requirements_files:
5656
if os.path.exists(requirements_file):
57-
return open(requirements_file, 'r').read().split('\n')
57+
with open(requirements_file, 'r') as fil:
58+
return fil.read().split('\n')
5859
return []
5960

6061

@@ -116,8 +117,12 @@ def write_requirements():
116117

117118

118119
def _run_shell_command(cmd):
119-
output = subprocess.Popen(["/bin/sh", "-c", cmd],
120-
stdout=subprocess.PIPE)
120+
if os.name == 'nt':
121+
output = subprocess.Popen(["cmd.exe", "/C", cmd],
122+
stdout=subprocess.PIPE)
123+
else:
124+
output = subprocess.Popen(["/bin/sh", "-c", cmd],
125+
stdout=subprocess.PIPE)
121126
out = output.communicate()
122127
if len(out) == 0:
123128
return None
@@ -135,11 +140,19 @@ def _get_git_next_version_suffix(branch_name):
135140
_run_shell_command("git fetch origin +refs/meta/*:refs/remotes/meta/*")
136141
milestone_cmd = "git show meta/openstack/release:%s" % branch_name
137142
milestonever = _run_shell_command(milestone_cmd)
138-
if not milestonever:
139-
milestonever = ""
143+
if milestonever:
144+
first_half = "%s~%s" % (milestonever, datestamp)
145+
else:
146+
first_half = datestamp
147+
140148
post_version = _get_git_post_version()
141-
revno = post_version.split(".")[-1]
142-
return "%s~%s.%s%s" % (milestonever, datestamp, revno_prefix, revno)
149+
# post version should look like:
150+
# 0.1.1.4.gcc9e28a
151+
# where the bit after the last . is the short sha, and the bit between
152+
# the last and second to last is the revno count
153+
(revno, sha) = post_version.split(".")[-2:]
154+
second_half = "%s%s.%s" % (revno_prefix, revno, sha)
155+
return ".".join((first_half, second_half))
143156

144157

145158
def _get_git_current_tag():
@@ -161,39 +174,48 @@ def _get_git_post_version():
161174
cmd = "git --no-pager log --oneline"
162175
out = _run_shell_command(cmd)
163176
revno = len(out.split("\n"))
177+
sha = _run_shell_command("git describe --always")
164178
else:
165179
tag_infos = tag_info.split("-")
166180
base_version = "-".join(tag_infos[:-2])
167-
revno = tag_infos[-2]
168-
return "%s.%s" % (base_version, revno)
181+
(revno, sha) = tag_infos[-2:]
182+
return "%s.%s.%s" % (base_version, revno, sha)
169183

170184

171185
def write_git_changelog():
172186
"""Write a changelog based on the git changelog."""
173-
if os.path.isdir('.git'):
174-
git_log_cmd = 'git log --stat'
175-
changelog = _run_shell_command(git_log_cmd)
176-
mailmap = parse_mailmap()
177-
with open("ChangeLog", "w") as changelog_file:
178-
changelog_file.write(canonicalize_emails(changelog, mailmap))
187+
new_changelog = 'ChangeLog'
188+
if not os.getenv('SKIP_WRITE_GIT_CHANGELOG'):
189+
if os.path.isdir('.git'):
190+
git_log_cmd = 'git log --stat'
191+
changelog = _run_shell_command(git_log_cmd)
192+
mailmap = parse_mailmap()
193+
with open(new_changelog, "w") as changelog_file:
194+
changelog_file.write(canonicalize_emails(changelog, mailmap))
195+
else:
196+
open(new_changelog, 'w').close()
179197

180198

181199
def generate_authors():
182200
"""Create AUTHORS file using git commits."""
183-
jenkins_email = 'jenkins@review.openstack.org'
201+
jenkins_email = 'jenkins@review.(openstack|stackforge).org'
184202
old_authors = 'AUTHORS.in'
185203
new_authors = 'AUTHORS'
186-
if os.path.isdir('.git'):
187-
# don't include jenkins email address in AUTHORS file
188-
git_log_cmd = ("git log --format='%aN <%aE>' | sort -u | "
189-
"grep -v " + jenkins_email)
190-
changelog = _run_shell_command(git_log_cmd)
191-
mailmap = parse_mailmap()
192-
with open(new_authors, 'w') as new_authors_fh:
193-
new_authors_fh.write(canonicalize_emails(changelog, mailmap))
194-
if os.path.exists(old_authors):
195-
with open(old_authors, "r") as old_authors_fh:
196-
new_authors_fh.write('\n' + old_authors_fh.read())
204+
if not os.getenv('SKIP_GENERATE_AUTHORS'):
205+
if os.path.isdir('.git'):
206+
# don't include jenkins email address in AUTHORS file
207+
git_log_cmd = ("git log --format='%aN <%aE>' | sort -u | "
208+
"egrep -v '" + jenkins_email + "'")
209+
changelog = _run_shell_command(git_log_cmd)
210+
mailmap = parse_mailmap()
211+
with open(new_authors, 'w') as new_authors_fh:
212+
new_authors_fh.write(canonicalize_emails(changelog, mailmap))
213+
if os.path.exists(old_authors):
214+
with open(old_authors, "r") as old_authors_fh:
215+
new_authors_fh.write('\n' + old_authors_fh.read())
216+
else:
217+
open(new_authors, 'w').close()
218+
197219

198220
_rst_template = """%(heading)s
199221
%(underline)s
@@ -207,7 +229,7 @@ def generate_authors():
207229

208230
def read_versioninfo(project):
209231
"""Read the versioninfo file. If it doesn't exist, we're in a github
210-
zipball, and there's really know way to know what version we really
232+
zipball, and there's really no way to know what version we really
211233
are, but that should be ok, because the utility of that should be
212234
just about nil if this code path is in use in the first place."""
213235
versioninfo_path = os.path.join(project, 'versioninfo')
@@ -221,7 +243,8 @@ def read_versioninfo(project):
221243

222244
def write_versioninfo(project, version):
223245
"""Write a simple file containing the version of the package."""
224-
open(os.path.join(project, 'versioninfo'), 'w').write("%s\n" % version)
246+
with open(os.path.join(project, 'versioninfo'), 'w') as fil:
247+
fil.write("%s\n" % version)
225248

226249

227250
def get_cmdclass():
@@ -312,7 +335,8 @@ def get_git_branchname():
312335

313336

314337
def get_pre_version(projectname, base_version):
315-
"""Return a version which is based"""
338+
"""Return a version which is leading up to a version that will
339+
be released in the future."""
316340
if os.path.isdir('.git'):
317341
current_tag = _get_git_current_tag()
318342
if current_tag is not None:
@@ -324,10 +348,10 @@ def get_pre_version(projectname, base_version):
324348
version_suffix = _get_git_next_version_suffix(branch_name)
325349
version = "%s~%s" % (base_version, version_suffix)
326350
write_versioninfo(projectname, version)
327-
return version.split('~')[0]
351+
return version
328352
else:
329353
version = read_versioninfo(projectname)
330-
return version.split('~')[0]
354+
return version
331355

332356

333357
def get_post_version(projectname):

0 commit comments

Comments
 (0)