summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavanum Srinivas <dims@linux.vnet.ibm.com>2013-02-18 07:21:23 -0500
committerIsaku Yamahata <yamahata@valinux.co.jp>2013-02-25 16:16:55 +0900
commit8842a00c452dfa9d2ee70e73419e92daa477ea1a (patch)
tree084b2b17bcb92ccc7706b946ee8f5d1052d1dff0
parent2b418be864a5aa5ba135f7651e83051cf3bf9ce6 (diff)
downloadoslo-8842a00c452dfa9d2ee70e73419e92daa477ea1a.tar.gz
oslo-8842a00c452dfa9d2ee70e73419e92daa477ea1a.tar.xz
oslo-8842a00c452dfa9d2ee70e73419e92daa477ea1a.zip
openstack.common.setup: fails to get version from git
quantum run_tests.py fails because openstack.common.setup._get_version_from_git fails. It is because quantum unit tests run under quantum/tests/unit instead of git root dir. So the function should check parent dirs for .git. cinder folks seem to have hit this bug (1125416). ERROR: test_network_gateway_update (quantum.tests.unit.nicira.test_networkgw.NetworkGatewayExtensionTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "quantum/quantum/tests/unit/nicira/test_networkgw.py", line 70, in setUp config.parse(args=args) File "quantum/quantum/common/config.py", line 99, in parse version='%%prog %s' % quantum_version.release_string()) File "quantum/quantum/openstack/common/version.py", line 63, in release_string self.release = self._get_version_from_pkg_resources() File "quantum/quantum/openstack/common/version.py", line 56, in _get_version_from_pkg_resources return setup.get_version(self.package) File "quantum/quantum/openstack/common/setup.py", line 334, in get_version raise Exception("Versioning for this project requires either an sdist" Exception: Versioning for this project requires either an sdist tarball, or access to an upstream git repository. Change-Id: I2e24c00b5ba8f35381cac081ff72d86ea0d75d19 Fixes: bug #1131162 and bug #1125416 Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
-rw-r--r--openstack/common/setup.py52
-rw-r--r--tests/unit/test_setup.py45
2 files changed, 73 insertions, 24 deletions
diff --git a/openstack/common/setup.py b/openstack/common/setup.py
index 22f864d..80a0ece 100644
--- a/openstack/common/setup.py
+++ b/openstack/common/setup.py
@@ -43,6 +43,11 @@ def parse_mailmap(mailmap='.mailmap'):
return mapping
+def _parse_git_mailmap(git_dir, mailmap='.mailmap'):
+ mailmap = os.path.join(os.path.dirname(git_dir), mailmap)
+ return parse_mailmap(mailmap)
+
+
def canonicalize_emails(changelog, mapping):
"""Takes in a string and an email alias mapping and replaces all
instances of the aliases in the string with their real email.
@@ -127,14 +132,26 @@ def _run_shell_command(cmd, throw_on_error=False):
return out[0].strip()
+def _get_git_directory():
+ parent_dir = os.path.dirname(__file__)
+ while True:
+ git_dir = os.path.join(parent_dir, '.git')
+ if os.path.exists(git_dir):
+ return git_dir
+ parent_dir, child = os.path.split(parent_dir)
+ if not child: # reached to root dir
+ return None
+
+
def write_git_changelog():
"""Write a changelog based on the git changelog."""
new_changelog = 'ChangeLog'
+ git_dir = _get_git_directory()
if not os.getenv('SKIP_WRITE_GIT_CHANGELOG'):
- if os.path.exists('.git'):
- git_log_cmd = 'git log --stat'
+ if git_dir:
+ git_log_cmd = 'git --git-dir=%s log --stat' % git_dir
changelog = _run_shell_command(git_log_cmd)
- mailmap = parse_mailmap()
+ mailmap = _parse_git_mailmap(git_dir)
with open(new_changelog, "w") as changelog_file:
changelog_file.write(canonicalize_emails(changelog, mailmap))
else:
@@ -146,13 +163,15 @@ def generate_authors():
jenkins_email = 'jenkins@review.(openstack|stackforge).org'
old_authors = 'AUTHORS.in'
new_authors = 'AUTHORS'
+ git_dir = _get_git_directory()
if not os.getenv('SKIP_GENERATE_AUTHORS'):
- if os.path.exists('.git'):
+ if git_dir:
# don't include jenkins email address in AUTHORS file
- git_log_cmd = ("git log --format='%aN <%aE>' | sort -u | "
+ git_log_cmd = ("git --git-dir=" + git_dir +
+ " log --format='%aN <%aE>' | sort -u | "
"egrep -v '" + jenkins_email + "'")
changelog = _run_shell_command(git_log_cmd)
- mailmap = parse_mailmap()
+ mailmap = _parse_git_mailmap(git_dir)
with open(new_authors, 'w') as new_authors_fh:
new_authors_fh.write(canonicalize_emails(changelog, mailmap))
if os.path.exists(old_authors):
@@ -258,19 +277,21 @@ def get_cmdclass():
return cmdclass
-def _get_revno():
+def _get_revno(git_dir):
"""Return the number of commits since the most recent tag.
We use git-describe to find this out, but if there are no
tags then we fall back to counting commits since the beginning
of time.
"""
- describe = _run_shell_command("git describe --always")
+ describe = _run_shell_command(
+ "git --git-dir=%s describe --always" % git_dir)
if "-" in describe:
return describe.rsplit("-", 2)[-2]
# no tags found
- revlist = _run_shell_command("git rev-list --abbrev-commit HEAD")
+ revlist = _run_shell_command(
+ "git --git-dir=%s rev-list --abbrev-commit HEAD" % git_dir)
return len(revlist.splitlines())
@@ -279,18 +300,21 @@ def _get_version_from_git(pre_version):
revision if there is one, or tag plus number of additional revisions
if the current revision has no tag."""
- if os.path.exists('.git'):
+ git_dir = _get_git_directory()
+ if git_dir:
if pre_version:
try:
return _run_shell_command(
- "git describe --exact-match",
+ "git --git-dir=" + git_dir + " describe --exact-match",
throw_on_error=True).replace('-', '.')
except Exception:
- sha = _run_shell_command("git log -n1 --pretty=format:%h")
- return "%s.a%s.g%s" % (pre_version, _get_revno(), sha)
+ sha = _run_shell_command(
+ "git --git-dir=" + git_dir + " log -n1 --pretty=format:%h")
+ return "%s.a%s.g%s" % (pre_version, _get_revno(git_dir), sha)
else:
return _run_shell_command(
- "git describe --always").replace('-', '.')
+ "git --git-dir=" + git_dir + " describe --always").replace(
+ '-', '.')
return None
diff --git a/tests/unit/test_setup.py b/tests/unit/test_setup.py
index 60e2e6f..30a5eb6 100644
--- a/tests/unit/test_setup.py
+++ b/tests/unit/test_setup.py
@@ -15,6 +15,7 @@
# License for the specific language governing permissions and limitations
# under the License.
+import io
import os
import sys
import StringIO
@@ -84,32 +85,56 @@ class GitLogsTest(utils.BaseTestCase):
temp_path = self.useFixture(fixtures.TempDir()).path
self.useFixture(DiveDir(temp_path))
+ @staticmethod
+ def _root_dir():
+ # NOTE(yamahata): get root direcotry of repository
+ # __file__ = $ROOT/tests/unit/test_setup.py
+ # => $ROOT/tests/unit => $ROOT/tests => $ROOT
+ root_dir = os.path.dirname(__file__)
+ root_dir = os.path.dirname(root_dir)
+ root_dir = os.path.dirname(root_dir)
+ return root_dir
+
def test_write_git_changelog(self):
- exist_files = [".git", ".mailmap"]
- self.useFixture(fixtures.MonkeyPatch("os.path.exists",
- lambda path: path in exist_files))
+ root_dir = self._root_dir()
+ exist_files = [os.path.join(root_dir, f) for f in ".git", ".mailmap"]
+ self.useFixture(fixtures.MonkeyPatch(
+ "os.path.exists",
+ lambda path: os.path.abspath(path) in exist_files))
self.useFixture(fixtures.FakePopen(lambda _: {
"stdout": StringIO.StringIO("Author: Foo Bar <email@bar.com>\n")
}))
- with open(".mailmap", "w") as mm_fh:
- mm_fh.write("Foo Bar <email@foo.com> <email@bar.com>\n")
+
+ builtin_open = open
+
+ def _fake_open(name, mode):
+ if name.endswith('.mailmap'):
+ # StringIO.StringIO doesn't have __exit__ (at least python 2.6)
+ return io.BytesIO("Foo Bar <email@foo.com> <email@bar.com>\n")
+ return builtin_open(name, mode)
+ self.useFixture(fixtures.MonkeyPatch("__builtin__.open", _fake_open))
write_git_changelog()
- with open("ChangeLog", "r") as ch_fh:
+ with open(os.path.join(root_dir, "ChangeLog"), "r") as ch_fh:
self.assertTrue("email@foo.com" in ch_fh.read())
def test_generate_authors(self):
author_old = "Foo Foo <email@foo.com>"
author_new = "Bar Bar <email@bar.com>"
- exist_files = [".git", "AUTHORS.in"]
- self.useFixture(fixtures.MonkeyPatch("os.path.exists",
- lambda path: path in exist_files))
+ root_dir = self._root_dir()
+ exist_files = [os.path.join(root_dir, ".git"),
+ os.path.abspath("AUTHORS.in")]
+ self.useFixture(fixtures.MonkeyPatch(
+ "os.path.exists",
+ lambda path: os.path.abspath(path) in exist_files))
+
+ git_log_cmd = "git --git-dir=%s log" % os.path.join(root_dir, '.git')
self.useFixture(fixtures.FakePopen(lambda proc_args: {
"stdout": StringIO.StringIO(
author_new
- if proc_args["args"][2].startswith("git log")
+ if proc_args["args"][2].startswith(git_log_cmd)
else "")
}))