michael@0: michael@0: # This file helps to compute a version number in source trees obtained from michael@0: # git-archive tarball (such as those provided by githubs download-from-tag michael@0: # feature). Distribution tarballs (build by setup.py sdist) and build michael@0: # directories (produced by setup.py build) will contain a much shorter file michael@0: # that just contains the computed version number. michael@0: michael@0: # This file is released into the public domain. Generated by versioneer-0.6 michael@0: # (https://github.com/warner/python-versioneer) michael@0: michael@0: # these strings will be replaced by git during git-archive michael@0: git_refnames = "$Format:%d$" michael@0: git_full = "$Format:%H$" michael@0: michael@0: michael@0: import subprocess michael@0: michael@0: def run_command(args, cwd=None, verbose=False): michael@0: try: michael@0: # remember shell=False, so use git.cmd on windows, not just git michael@0: p = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd) michael@0: except EnvironmentError, e: michael@0: if verbose: michael@0: print "unable to run %s" % args[0] michael@0: print e michael@0: return None michael@0: stdout = p.communicate()[0].strip() michael@0: if p.returncode != 0: michael@0: if verbose: michael@0: print "unable to run %s (error)" % args[0] michael@0: return None michael@0: return stdout michael@0: michael@0: michael@0: import sys michael@0: import re michael@0: import os.path michael@0: michael@0: def get_expanded_variables(versionfile_source): michael@0: # the code embedded in _version.py can just fetch the value of these michael@0: # variables. When used from setup.py, we don't want to import michael@0: # _version.py, so we do it with a regexp instead. This function is not michael@0: # used from _version.py. michael@0: variables = {} michael@0: try: michael@0: for line in open(versionfile_source,"r").readlines(): michael@0: if line.strip().startswith("git_refnames ="): michael@0: mo = re.search(r'=\s*"(.*)"', line) michael@0: if mo: michael@0: variables["refnames"] = mo.group(1) michael@0: if line.strip().startswith("git_full ="): michael@0: mo = re.search(r'=\s*"(.*)"', line) michael@0: if mo: michael@0: variables["full"] = mo.group(1) michael@0: except EnvironmentError: michael@0: pass michael@0: return variables michael@0: michael@0: def versions_from_expanded_variables(variables, tag_prefix): michael@0: refnames = variables["refnames"].strip() michael@0: if refnames.startswith("$Format"): michael@0: return {} # unexpanded, so not in an unpacked git-archive tarball michael@0: refs = set([r.strip() for r in refnames.strip("()").split(",")]) michael@0: for ref in list(refs): michael@0: if not re.search(r'\d', ref): michael@0: refs.discard(ref) michael@0: # Assume all version tags have a digit. git's %d expansion michael@0: # behaves like git log --decorate=short and strips out the michael@0: # refs/heads/ and refs/tags/ prefixes that would let us michael@0: # distinguish between branches and tags. By ignoring refnames michael@0: # without digits, we filter out many common branch names like michael@0: # "release" and "stabilization", as well as "HEAD" and "master". michael@0: for ref in sorted(refs): michael@0: # sorting will prefer e.g. "2.0" over "2.0rc1" michael@0: if ref.startswith(tag_prefix): michael@0: r = ref[len(tag_prefix):] michael@0: return { "version": r, michael@0: "full": variables["full"].strip() } michael@0: # no suitable tags, so we use the full revision id michael@0: return { "version": variables["full"].strip(), michael@0: "full": variables["full"].strip() } michael@0: michael@0: def versions_from_vcs(tag_prefix, versionfile_source, verbose=False): michael@0: # this runs 'git' from the root of the source tree. That either means michael@0: # someone ran a setup.py command (and this code is in versioneer.py, thus michael@0: # the containing directory is the root of the source tree), or someone michael@0: # ran a project-specific entry point (and this code is in _version.py, michael@0: # thus the containing directory is somewhere deeper in the source tree). michael@0: # This only gets called if the git-archive 'subst' variables were *not* michael@0: # expanded, and _version.py hasn't already been rewritten with a short michael@0: # version string, meaning we're inside a checked out source tree. michael@0: michael@0: try: michael@0: here = os.path.abspath(__file__) michael@0: except NameError: michael@0: # some py2exe/bbfreeze/non-CPython implementations don't do __file__ michael@0: return {} # not always correct michael@0: michael@0: # versionfile_source is the relative path from the top of the source tree michael@0: # (where the .git directory might live) to this file. Invert this to find michael@0: # the root from __file__. michael@0: root = here michael@0: for i in range(len(versionfile_source.split("/"))): michael@0: root = os.path.dirname(root) michael@0: if not os.path.exists(os.path.join(root, ".git")): michael@0: return {} michael@0: michael@0: GIT = "git" michael@0: if sys.platform == "win32": michael@0: GIT = "git.cmd" michael@0: stdout = run_command([GIT, "describe", "--tags", "--dirty", "--always"], michael@0: cwd=root) michael@0: if stdout is None: michael@0: return {} michael@0: if not stdout.startswith(tag_prefix): michael@0: if verbose: michael@0: print "tag '%s' doesn't start with prefix '%s'" % (stdout, tag_prefix) michael@0: return {} michael@0: tag = stdout[len(tag_prefix):] michael@0: stdout = run_command([GIT, "rev-parse", "HEAD"], cwd=root) michael@0: if stdout is None: michael@0: return {} michael@0: full = stdout.strip() michael@0: if tag.endswith("-dirty"): michael@0: full += "-dirty" michael@0: return {"version": tag, "full": full} michael@0: michael@0: michael@0: def versions_from_parentdir(parentdir_prefix, versionfile_source, verbose=False): michael@0: try: michael@0: here = os.path.abspath(__file__) michael@0: # versionfile_source is the relative path from the top of the source michael@0: # tree (where the .git directory might live) to _version.py, when michael@0: # this is used by the runtime. Invert this to find the root from michael@0: # __file__. michael@0: root = here michael@0: for i in range(len(versionfile_source.split("/"))): michael@0: root = os.path.dirname(root) michael@0: except NameError: michael@0: # try a couple different things to handle py2exe, bbfreeze, and michael@0: # non-CPython implementations which don't do __file__. This code michael@0: # either lives in versioneer.py (used by setup.py) or _version.py michael@0: # (used by the runtime). In the versioneer.py case, sys.argv[0] will michael@0: # be setup.py, in the root of the source tree. In the _version.py michael@0: # case, we have no idea what sys.argv[0] is (some michael@0: # application-specific runner). michael@0: root = os.path.dirname(os.path.abspath(sys.argv[0])) michael@0: # Source tarballs conventionally unpack into a directory that includes michael@0: # both the project name and a version string. michael@0: dirname = os.path.basename(root) michael@0: if not dirname.startswith(parentdir_prefix): michael@0: if verbose: michael@0: print "dirname '%s' doesn't start with prefix '%s'" % (dirname, parentdir_prefix) michael@0: return None michael@0: return {"version": dirname[len(parentdir_prefix):], "full": ""} michael@0: michael@0: tag_prefix = "" michael@0: parentdir_prefix = "addon-sdk-" michael@0: versionfile_source = "python-lib/cuddlefish/_version.py" michael@0: michael@0: def get_versions(): michael@0: variables = { "refnames": git_refnames, "full": git_full } michael@0: ver = versions_from_expanded_variables(variables, tag_prefix) michael@0: if not ver: michael@0: ver = versions_from_vcs(tag_prefix, versionfile_source) michael@0: if not ver: michael@0: ver = versions_from_parentdir(parentdir_prefix, versionfile_source) michael@0: if not ver: michael@0: ver = {"version": "unknown", "full": ""} michael@0: return ver michael@0: