Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bug 704519: add "cfx --version", get version from git-describe or tar…

…ball

This uses a tool I wrote named "versioneer", v0.4, from
https://github.com/warner/python-versioneer
  • Loading branch information...
commit 33fa054fd9e1650fd88005cb82d0ee98420bdfff 1 parent d32b895
@warner warner authored
View
1  .gitattributes
@@ -2,3 +2,4 @@
.hgignore export-ignore
.hgtags export-ignore
.gitattributes export-ignore
+python-lib/cuddlefish/_version.py export-subst
View
13 python-lib/cuddlefish/__init__.py
@@ -8,7 +8,7 @@
import simplejson as json
from cuddlefish import packaging
from cuddlefish.bunch import Bunch
-from cuddlefish.version import get_version
+from cuddlefish._version import get_versions
MOZRUNNER_BIN_NOT_FOUND = 'Mozrunner could not locate your binary'
MOZRUNNER_BIN_NOT_FOUND_HELP = """
@@ -259,8 +259,10 @@ class CfxOption(optparse.Option):
TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER)
TYPE_CHECKER['json'] = check_json
-def parse_args(arguments, global_options, usage, parser_groups, defaults=None):
- parser = optparse.OptionParser(usage=usage.strip(), option_class=CfxOption)
+def parse_args(arguments, global_options, usage, version, parser_groups,
+ defaults=None):
+ parser = optparse.OptionParser(usage=usage.strip(), option_class=CfxOption,
+ version=version)
def name_cmp(a, b):
# a[0] = name sequence
@@ -475,10 +477,14 @@ def get_unique_prefix(jid):
def run(arguments=sys.argv[1:], target_cfg=None, pkg_cfg=None,
defaults=None, env_root=os.environ.get('CUDDLEFISH_ROOT'),
stdout=sys.stdout):
+ versions = get_versions()
+ sdk_version = versions["version"]
+ display_version = "Add-on SDK %s (%s)" % (sdk_version, versions["full"])
parser_kwargs = dict(arguments=arguments,
global_options=global_options,
parser_groups=parser_groups,
usage=usage,
+ version=display_version,
defaults=defaults)
(options, args) = parse_args(**parser_kwargs)
@@ -708,7 +714,6 @@ def run(arguments=sys.argv[1:], target_cfg=None, pkg_cfg=None,
harness_options['metadata'] = packaging.get_metadata(pkg_cfg, used_deps)
- sdk_version = get_version(env_root)
harness_options['sdkVersion'] = sdk_version
packaging.call_plugins(pkg_cfg, used_deps)
View
126 python-lib/cuddlefish/_version.py
@@ -0,0 +1,126 @@
+
+# This file helps to compute a version number in source trees obtained from
+# git-archive tarball (such as those provided by githubs download-from-tag
+# feature). Distribution tarballs (build by setup.py sdist) and build
+# directories (produced by setup.py build) will contain a much shorter file
+# that just contains the computed version number.
+
+# This file is released into the public domain. Generated by versioneer-0.4
+# (https://github.com/warner/python-versioneer)
+
+# these strings will be replaced by git during git-archive
+git_refnames = "$Format:%d$"
+git_full = "$Format:%H$"
+
+
+import subprocess
+
+def run_command(args, cwd=None, verbose=False):
+ try:
+ # remember shell=False, so use git.cmd on windows, not just git
+ p = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd)
+ except EnvironmentError, e:
+ if verbose:
+ print "unable to run %s" % args[0]
+ print e
+ return None
+ stdout = p.communicate()[0].strip()
+ if p.returncode != 0:
+ if verbose:
+ print "unable to run %s (error)" % args[0]
+ return None
+ return stdout
+
+
+import sys
+import re
+import os.path
+
+def get_expanded_variables(versionfile_source):
+ # the code embedded in _version.py can just fetch the value of these
+ # variables. When used from setup.py, we don't want to import
+ # _version.py, so we do it with a regexp instead. This function is not
+ # used from _version.py.
+ variables = {}
+ try:
+ for line in open(versionfile_source,"r").readlines():
+ if line.strip().startswith("git_refnames ="):
+ mo = re.search(r'=\s*"(.*)"', line)
+ if mo:
+ variables["refnames"] = mo.group(1)
+ if line.strip().startswith("git_full ="):
+ mo = re.search(r'=\s*"(.*)"', line)
+ if mo:
+ variables["full"] = mo.group(1)
+ except EnvironmentError:
+ pass
+ return variables
+
+def versions_from_expanded_variables(variables, tag_prefix):
+ refnames = variables["refnames"].strip()
+ if refnames.startswith("$Format"):
+ return {} # unexpanded, so not in an unpacked git-archive tarball
+ refs = set([r.strip() for r in refnames.strip("()").split(",")])
+ refs.discard("HEAD") ; refs.discard("master")
+ for ref in reversed(sorted(refs)):
+ if ref.startswith(tag_prefix):
+ r = ref[len(tag_prefix):]
+ if re.search(r'\d', r):
+ # git's %d expansion behaves like git log --decorate=short
+ # and strips out the refs/heads/ and refs/tags/ prefixes that
+ # would let us distinguish between branches and tags. By
+ # ignoring refnames without digits, we filter out many common
+ # branch names like "release" and "stabilization".
+ return { "version": r,
+ "full": variables["full"].strip() }
+ # no suitable tags, so we use the full revision id
+ return { "version": variables["full"].strip(),
+ "full": variables["full"].strip() }
+
+def versions_from_vcs(tag_prefix, verbose=False):
+ # this runs 'git' from the directory that contains this file. That either
+ # means someone ran a setup.py command (and this code is in
+ # versioneer.py, thus the containing directory is the root of the source
+ # tree), or someone ran a project-specific entry point (and this code is
+ # in _version.py, thus the containing directory is somewhere deeper in
+ # the source tree). This only gets called if the git-archive 'subst'
+ # variables were *not* expanded, and _version.py hasn't already been
+ # rewritten with a short version string, meaning we're inside a checked
+ # out source tree.
+
+ try:
+ source_dir = os.path.dirname(os.path.abspath(__file__))
+ except NameError:
+ # some py2exe/bbfreeze/non-CPython implementations don't do __file__
+ return {} # not always correct
+ GIT = "git"
+ if sys.platform == "win32":
+ GIT = "git.cmd"
+ stdout = run_command([GIT, "describe", "--tags", "--dirty", "--always"],
+ cwd=source_dir)
+ if stdout is None:
+ return {}
+ if not stdout.startswith(tag_prefix):
+ if verbose:
+ print "tag '%s' doesn't start with prefix '%s'" % (stdout, tag_prefix)
+ return {}
+ tag = stdout[len(tag_prefix):]
+ stdout = run_command([GIT, "rev-parse", "HEAD"], cwd=source_dir)
+ if stdout is None:
+ return {}
+ full = stdout.strip()
+ if tag.endswith("-dirty"):
+ full += "-dirty"
+ return {"version": tag, "full": full}
+
+
+tag_prefix = ""
+def get_versions():
+ variables = { "refnames": git_refnames, "full": git_full }
+ ver = versions_from_expanded_variables(variables, tag_prefix)
+ if not ver:
+ ver = versions_from_vcs(tag_prefix)
+ if not ver:
+ ver = {"version": "unknown", "full": ""}
+ return ver
+
View
12 python-lib/cuddlefish/tests/test_version.py
@@ -2,7 +2,7 @@
import unittest
import shutil
-from cuddlefish.version import get_version
+from cuddlefish._version import get_versions
class Version(unittest.TestCase):
def get_basedir(self):
@@ -19,14 +19,6 @@ def make_basedir(self):
def test_current_version(self):
# the SDK should be able to determine its own version. We don't care
# what it is, merely that it can be computed.
- env_root = os.environ.get('CUDDLEFISH_ROOT')
- version = get_version(env_root)
+ version = get_versions()["version"]
self.failUnless(isinstance(version, str), (version, type(version)))
self.failUnless(len(version) > 0, version)
- def test_read(self):
- basedir = self.make_basedir()
- f = open(os.path.join(basedir, ".version"), "w")
- f.write("versioniffic\n")
- f.close()
- sdk_version = get_version(basedir)
- self.failUnlessEqual(sdk_version, "versioniffic")
View
6 python-lib/cuddlefish/version.py
@@ -1,6 +0,0 @@
-import os
-
-def get_version(env_root):
- f = open(os.path.join(env_root, ".version"), "r")
- sdk_version = f.read().strip()
- return sdk_version
Please sign in to comment.
Something went wrong with that request. Please try again.