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: If0ae835aeada8769e46dddf4f3c2f2edfdfbc5fe
This commit is contained in:
Alessandro Pilotti
2012-11-05 18:19:13 +02:00
parent a8e88aa340
commit 16aafa728e
2 changed files with 61 additions and 47 deletions

View File

@@ -31,13 +31,13 @@ from setuptools.command import sdist
def parse_mailmap(mailmap='.mailmap'): def parse_mailmap(mailmap='.mailmap'):
mapping = {} mapping = {}
if os.path.exists(mailmap): if os.path.exists(mailmap):
fp = open(mailmap, 'r') with open(mailmap, 'r') as fp:
for l in fp: for l in fp:
l = l.strip() l = l.strip()
if not l.startswith('#') and ' ' in l: if not l.startswith('#') and ' ' in l:
canonical_email, alias = [x for x in l.split(' ') canonical_email, alias = [x for x in l.split(' ')
if x.startswith('<')] if x.startswith('<')]
mapping[alias] = canonical_email mapping[alias] = canonical_email
return mapping return mapping
@@ -52,10 +52,10 @@ def canonicalize_emails(changelog, mapping):
# Get requirements from the first file that exists # Get requirements from the first file that exists
def get_reqs_from_files(requirements_files): def get_reqs_from_files(requirements_files):
reqs_in = []
for requirements_file in requirements_files: for requirements_file in requirements_files:
if os.path.exists(requirements_file): if os.path.exists(requirements_file):
return open(requirements_file, 'r').read().split('\n') with open(requirements_file, 'r') as fil:
return fil.read().split('\n')
return [] return []
@@ -117,8 +117,12 @@ def write_requirements():
def _run_shell_command(cmd): def _run_shell_command(cmd):
output = subprocess.Popen(["/bin/sh", "-c", cmd], if os.name == 'nt':
stdout=subprocess.PIPE) output = subprocess.Popen(["cmd.exe", "/C", cmd],
stdout=subprocess.PIPE)
else:
output = subprocess.Popen(["/bin/sh", "-c", cmd],
stdout=subprocess.PIPE)
out = output.communicate() out = output.communicate()
if len(out) == 0: if len(out) == 0:
return None return None
@@ -127,13 +131,6 @@ def _run_shell_command(cmd):
return out[0].strip() return out[0].strip()
def _get_git_branch_name():
branch_ref = _run_shell_command("git symbolic-ref -q HEAD")
if branch_ref is None:
return "HEAD"
return branch_ref[len("refs/heads/"):]
def _get_git_next_version_suffix(branch_name): def _get_git_next_version_suffix(branch_name):
datestamp = datetime.datetime.now().strftime('%Y%m%d') datestamp = datetime.datetime.now().strftime('%Y%m%d')
if branch_name == 'milestone-proposed': if branch_name == 'milestone-proposed':
@@ -143,16 +140,18 @@ def _get_git_next_version_suffix(branch_name):
_run_shell_command("git fetch origin +refs/meta/*:refs/remotes/meta/*") _run_shell_command("git fetch origin +refs/meta/*:refs/remotes/meta/*")
milestone_cmd = "git show meta/openstack/release:%s" % branch_name milestone_cmd = "git show meta/openstack/release:%s" % branch_name
milestonever = _run_shell_command(milestone_cmd) milestonever = _run_shell_command(milestone_cmd)
if not milestonever: if milestonever:
milestonever = branch_name first_half = "%s~%s" % (milestonever, datestamp)
else:
first_half = datestamp
post_version = _get_git_post_version() post_version = _get_git_post_version()
# post version should look like: # post version should look like:
# 0.1.1.4.cc9e28a # 0.1.1.4.gcc9e28a
# where the bit after the last . is the short sha, and the bit between # where the bit after the last . is the short sha, and the bit between
# the last and second to last is the revno count # the last and second to last is the revno count
(revno, sha) = post_version.split(".")[-2:] (revno, sha) = post_version.split(".")[-2:]
first_half = "%(milestonever)s~%(datestamp)s" % locals() second_half = "%s%s.%s" % (revno_prefix, revno, sha)
second_half = "%(revno_prefix)s%(revno)s.%(sha)s" % locals()
return ".".join((first_half, second_half)) return ".".join((first_half, second_half))
@@ -180,37 +179,43 @@ def _get_git_post_version():
tag_infos = tag_info.split("-") tag_infos = tag_info.split("-")
base_version = "-".join(tag_infos[:-2]) base_version = "-".join(tag_infos[:-2])
(revno, sha) = tag_infos[-2:] (revno, sha) = tag_infos[-2:]
# git describe prefixes the sha with a g
sha = sha[1:]
return "%s.%s.%s" % (base_version, revno, sha) return "%s.%s.%s" % (base_version, revno, sha)
def write_git_changelog(): def write_git_changelog():
"""Write a changelog based on the git changelog.""" """Write a changelog based on the git changelog."""
if os.path.isdir('.git'): new_changelog = 'ChangeLog'
git_log_cmd = 'git log --stat' if not os.getenv('SKIP_WRITE_GIT_CHANGELOG'):
changelog = _run_shell_command(git_log_cmd) if os.path.isdir('.git'):
mailmap = parse_mailmap() git_log_cmd = 'git log --stat'
with open("ChangeLog", "w") as changelog_file: changelog = _run_shell_command(git_log_cmd)
changelog_file.write(canonicalize_emails(changelog, mailmap)) mailmap = parse_mailmap()
with open(new_changelog, "w") as changelog_file:
changelog_file.write(canonicalize_emails(changelog, mailmap))
else:
open(new_changelog, 'w').close()
def generate_authors(): def generate_authors():
"""Create AUTHORS file using git commits.""" """Create AUTHORS file using git commits."""
jenkins_email = 'jenkins@review.openstack.org' jenkins_email = 'jenkins@review.(openstack|stackforge).org'
old_authors = 'AUTHORS.in' old_authors = 'AUTHORS.in'
new_authors = 'AUTHORS' new_authors = 'AUTHORS'
if os.path.isdir('.git'): if not os.getenv('SKIP_GENERATE_AUTHORS'):
# don't include jenkins email address in AUTHORS file if os.path.isdir('.git'):
git_log_cmd = ("git log --format='%aN <%aE>' | sort -u | " # don't include jenkins email address in AUTHORS file
"grep -v " + jenkins_email) git_log_cmd = ("git log --format='%aN <%aE>' | sort -u | "
changelog = _run_shell_command(git_log_cmd) "egrep -v '" + jenkins_email + "'")
mailmap = parse_mailmap() changelog = _run_shell_command(git_log_cmd)
with open(new_authors, 'w') as new_authors_fh: mailmap = parse_mailmap()
new_authors_fh.write(canonicalize_emails(changelog, mailmap)) with open(new_authors, 'w') as new_authors_fh:
if os.path.exists(old_authors): new_authors_fh.write(canonicalize_emails(changelog, mailmap))
with open(old_authors, "r") as old_authors_fh: if os.path.exists(old_authors):
new_authors_fh.write('\n' + old_authors_fh.read()) with open(old_authors, "r") as old_authors_fh:
new_authors_fh.write('\n' + old_authors_fh.read())
else:
open(new_authors, 'w').close()
_rst_template = """%(heading)s _rst_template = """%(heading)s
%(underline)s %(underline)s
@@ -238,7 +243,8 @@ def read_versioninfo(project):
def write_versioninfo(project, version): def write_versioninfo(project, version):
"""Write a simple file containing the version of the package.""" """Write a simple file containing the version of the package."""
open(os.path.join(project, 'versioninfo'), 'w').write("%s\n" % version) with open(os.path.join(project, 'versioninfo'), 'w') as fil:
fil.write("%s\n" % version)
def get_cmdclass(): def get_cmdclass():
@@ -319,6 +325,15 @@ def get_cmdclass():
return cmdclass return cmdclass
def get_git_branchname():
for branch in _run_shell_command("git branch --color=never").split("\n"):
if branch.startswith('*'):
_branch_name = branch.split()[1].strip()
if _branch_name == "(no":
_branch_name = "no-branch"
return _branch_name
def get_pre_version(projectname, base_version): def get_pre_version(projectname, base_version):
"""Return a version which is leading up to a version that will """Return a version which is leading up to a version that will
be released in the future.""" be released in the future."""
@@ -329,7 +344,7 @@ def get_pre_version(projectname, base_version):
else: else:
branch_name = os.getenv('BRANCHNAME', branch_name = os.getenv('BRANCHNAME',
os.getenv('GERRIT_REFNAME', os.getenv('GERRIT_REFNAME',
_get_git_branch_name())) get_git_branchname()))
version_suffix = _get_git_next_version_suffix(branch_name) version_suffix = _get_git_next_version_suffix(branch_name)
version = "%s~%s" % (base_version, version_suffix) version = "%s~%s" % (base_version, version_suffix)
write_versioninfo(projectname, version) write_versioninfo(projectname, version)

View File

@@ -20,7 +20,6 @@ Utilities for consuming the auto-generated versioninfo files.
import datetime import datetime
import pkg_resources import pkg_resources
import os
import setup import setup
@@ -107,7 +106,7 @@ class VersionInfo(object):
versioninfo = "%s/versioninfo" % self.package versioninfo = "%s/versioninfo" % self.package
try: try:
raw_version = pkg_resources.resource_string(requirement, raw_version = pkg_resources.resource_string(requirement,
versioninfo) versioninfo)
self.version = self._newer_version(raw_version.strip()) self.version = self._newer_version(raw_version.strip())
except (IOError, pkg_resources.DistributionNotFound): except (IOError, pkg_resources.DistributionNotFound):
self.version = self._generate_version() self.version = self._generate_version()