addon-sdk/source/python-lib/cuddlefish/_version.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/python-lib/cuddlefish/_version.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,171 @@
     1.4 +
     1.5 +# This file helps to compute a version number in source trees obtained from
     1.6 +# git-archive tarball (such as those provided by githubs download-from-tag
     1.7 +# feature). Distribution tarballs (build by setup.py sdist) and build
     1.8 +# directories (produced by setup.py build) will contain a much shorter file
     1.9 +# that just contains the computed version number.
    1.10 +
    1.11 +# This file is released into the public domain. Generated by versioneer-0.6
    1.12 +# (https://github.com/warner/python-versioneer)
    1.13 +
    1.14 +# these strings will be replaced by git during git-archive
    1.15 +git_refnames = "$Format:%d$"
    1.16 +git_full = "$Format:%H$"
    1.17 +
    1.18 +
    1.19 +import subprocess
    1.20 +
    1.21 +def run_command(args, cwd=None, verbose=False):
    1.22 +    try:
    1.23 +        # remember shell=False, so use git.cmd on windows, not just git
    1.24 +        p = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd)
    1.25 +    except EnvironmentError, e:
    1.26 +        if verbose:
    1.27 +            print "unable to run %s" % args[0]
    1.28 +            print e
    1.29 +        return None
    1.30 +    stdout = p.communicate()[0].strip()
    1.31 +    if p.returncode != 0:
    1.32 +        if verbose:
    1.33 +            print "unable to run %s (error)" % args[0]
    1.34 +        return None
    1.35 +    return stdout
    1.36 +
    1.37 +
    1.38 +import sys
    1.39 +import re
    1.40 +import os.path
    1.41 +
    1.42 +def get_expanded_variables(versionfile_source):
    1.43 +    # the code embedded in _version.py can just fetch the value of these
    1.44 +    # variables. When used from setup.py, we don't want to import
    1.45 +    # _version.py, so we do it with a regexp instead. This function is not
    1.46 +    # used from _version.py.
    1.47 +    variables = {}
    1.48 +    try:
    1.49 +        for line in open(versionfile_source,"r").readlines():
    1.50 +            if line.strip().startswith("git_refnames ="):
    1.51 +                mo = re.search(r'=\s*"(.*)"', line)
    1.52 +                if mo:
    1.53 +                    variables["refnames"] = mo.group(1)
    1.54 +            if line.strip().startswith("git_full ="):
    1.55 +                mo = re.search(r'=\s*"(.*)"', line)
    1.56 +                if mo:
    1.57 +                    variables["full"] = mo.group(1)
    1.58 +    except EnvironmentError:
    1.59 +        pass
    1.60 +    return variables
    1.61 +
    1.62 +def versions_from_expanded_variables(variables, tag_prefix):
    1.63 +    refnames = variables["refnames"].strip()
    1.64 +    if refnames.startswith("$Format"):
    1.65 +        return {} # unexpanded, so not in an unpacked git-archive tarball
    1.66 +    refs = set([r.strip() for r in refnames.strip("()").split(",")])
    1.67 +    for ref in list(refs):
    1.68 +        if not re.search(r'\d', ref):
    1.69 +            refs.discard(ref)
    1.70 +            # Assume all version tags have a digit. git's %d expansion
    1.71 +            # behaves like git log --decorate=short and strips out the
    1.72 +            # refs/heads/ and refs/tags/ prefixes that would let us
    1.73 +            # distinguish between branches and tags. By ignoring refnames
    1.74 +            # without digits, we filter out many common branch names like
    1.75 +            # "release" and "stabilization", as well as "HEAD" and "master".
    1.76 +    for ref in sorted(refs):
    1.77 +        # sorting will prefer e.g. "2.0" over "2.0rc1"
    1.78 +        if ref.startswith(tag_prefix):
    1.79 +            r = ref[len(tag_prefix):]
    1.80 +            return { "version": r,
    1.81 +                     "full": variables["full"].strip() }
    1.82 +    # no suitable tags, so we use the full revision id
    1.83 +    return { "version": variables["full"].strip(),
    1.84 +             "full": variables["full"].strip() }
    1.85 +
    1.86 +def versions_from_vcs(tag_prefix, versionfile_source, verbose=False):
    1.87 +    # this runs 'git' from the root of the source tree. That either means
    1.88 +    # someone ran a setup.py command (and this code is in versioneer.py, thus
    1.89 +    # the containing directory is the root of the source tree), or someone
    1.90 +    # ran a project-specific entry point (and this code is in _version.py,
    1.91 +    # thus the containing directory is somewhere deeper in the source tree).
    1.92 +    # This only gets called if the git-archive 'subst' variables were *not*
    1.93 +    # expanded, and _version.py hasn't already been rewritten with a short
    1.94 +    # version string, meaning we're inside a checked out source tree.
    1.95 +
    1.96 +    try:
    1.97 +        here = os.path.abspath(__file__)
    1.98 +    except NameError:
    1.99 +        # some py2exe/bbfreeze/non-CPython implementations don't do __file__
   1.100 +        return {} # not always correct
   1.101 +
   1.102 +    # versionfile_source is the relative path from the top of the source tree
   1.103 +    # (where the .git directory might live) to this file. Invert this to find
   1.104 +    # the root from __file__.
   1.105 +    root = here
   1.106 +    for i in range(len(versionfile_source.split("/"))):
   1.107 +        root = os.path.dirname(root)
   1.108 +    if not os.path.exists(os.path.join(root, ".git")):
   1.109 +        return {}
   1.110 +
   1.111 +    GIT = "git"
   1.112 +    if sys.platform == "win32":
   1.113 +        GIT = "git.cmd"
   1.114 +    stdout = run_command([GIT, "describe", "--tags", "--dirty", "--always"],
   1.115 +                         cwd=root)
   1.116 +    if stdout is None:
   1.117 +        return {}
   1.118 +    if not stdout.startswith(tag_prefix):
   1.119 +        if verbose:
   1.120 +            print "tag '%s' doesn't start with prefix '%s'" % (stdout, tag_prefix)
   1.121 +        return {}
   1.122 +    tag = stdout[len(tag_prefix):]
   1.123 +    stdout = run_command([GIT, "rev-parse", "HEAD"], cwd=root)
   1.124 +    if stdout is None:
   1.125 +        return {}
   1.126 +    full = stdout.strip()
   1.127 +    if tag.endswith("-dirty"):
   1.128 +        full += "-dirty"
   1.129 +    return {"version": tag, "full": full}
   1.130 +
   1.131 +
   1.132 +def versions_from_parentdir(parentdir_prefix, versionfile_source, verbose=False):
   1.133 +    try:
   1.134 +        here = os.path.abspath(__file__)
   1.135 +        # versionfile_source is the relative path from the top of the source
   1.136 +        # tree (where the .git directory might live) to _version.py, when
   1.137 +        # this is used by the runtime. Invert this to find the root from
   1.138 +        # __file__.
   1.139 +        root = here
   1.140 +        for i in range(len(versionfile_source.split("/"))):
   1.141 +            root = os.path.dirname(root)
   1.142 +    except NameError:
   1.143 +        # try a couple different things to handle py2exe, bbfreeze, and
   1.144 +        # non-CPython implementations which don't do __file__. This code
   1.145 +        # either lives in versioneer.py (used by setup.py) or _version.py
   1.146 +        # (used by the runtime). In the versioneer.py case, sys.argv[0] will
   1.147 +        # be setup.py, in the root of the source tree. In the _version.py
   1.148 +        # case, we have no idea what sys.argv[0] is (some
   1.149 +        # application-specific runner).
   1.150 +        root = os.path.dirname(os.path.abspath(sys.argv[0]))
   1.151 +    # Source tarballs conventionally unpack into a directory that includes
   1.152 +    # both the project name and a version string.
   1.153 +    dirname = os.path.basename(root)
   1.154 +    if not dirname.startswith(parentdir_prefix):
   1.155 +        if verbose:
   1.156 +            print "dirname '%s' doesn't start with prefix '%s'" %                   (dirname, parentdir_prefix)
   1.157 +        return None
   1.158 +    return {"version": dirname[len(parentdir_prefix):], "full": ""}
   1.159 +
   1.160 +tag_prefix = ""
   1.161 +parentdir_prefix = "addon-sdk-"
   1.162 +versionfile_source = "python-lib/cuddlefish/_version.py"
   1.163 +
   1.164 +def get_versions():
   1.165 +    variables = { "refnames": git_refnames, "full": git_full }
   1.166 +    ver = versions_from_expanded_variables(variables, tag_prefix)
   1.167 +    if not ver:
   1.168 +        ver = versions_from_vcs(tag_prefix, versionfile_source)
   1.169 +    if not ver:
   1.170 +        ver = versions_from_parentdir(parentdir_prefix, versionfile_source)
   1.171 +    if not ver:
   1.172 +        ver = {"version": "unknown", "full": ""}
   1.173 +    return ver
   1.174 +

mercurial