|
| 1 | + |
| 2 | +IN_LONG_VERSION_PY = True |
| 3 | +# This file helps to compute a version number in source trees obtained from |
| 4 | +# git-archive tarball (such as those provided by githubs download-from-tag |
| 5 | +# feature). Distribution tarballs (build by setup.py sdist) and build |
| 6 | +# directories (produced by setup.py build) will contain a much shorter file |
| 7 | +# that just contains the computed version number. |
| 8 | + |
| 9 | +# This file is released into the public domain. Generated by |
| 10 | +# versioneer-0.7+ (https://github.com/warner/python-versioneer) |
| 11 | + |
| 12 | +# these strings will be replaced by git during git-archive |
| 13 | +git_refnames = "$Format:%d$" |
| 14 | +git_full = "$Format:%H$" |
| 15 | + |
| 16 | + |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | + |
| 20 | +def run_command(args, cwd=None, verbose=False): |
| 21 | + try: |
| 22 | + # remember shell=False, so use git.cmd on windows, not just git |
| 23 | + p = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd) |
| 24 | + except EnvironmentError: |
| 25 | + e = sys.exc_info()[1] |
| 26 | + if verbose: |
| 27 | + print("unable to run %s" % args[0]) |
| 28 | + print(e) |
| 29 | + return None |
| 30 | + stdout = p.communicate()[0].strip() |
| 31 | + if sys.version >= '3': |
| 32 | + stdout = stdout.decode() |
| 33 | + if p.returncode != 0: |
| 34 | + if verbose: |
| 35 | + print("unable to run %s (error)" % args[0]) |
| 36 | + return None |
| 37 | + return stdout |
| 38 | + |
| 39 | + |
| 40 | +import sys |
| 41 | +import re |
| 42 | +import os.path |
| 43 | + |
| 44 | +def get_expanded_variables(versionfile_source): |
| 45 | + # the code embedded in _version.py can just fetch the value of these |
| 46 | + # variables. When used from setup.py, we don't want to import |
| 47 | + # _version.py, so we do it with a regexp instead. This function is not |
| 48 | + # used from _version.py. |
| 49 | + variables = {} |
| 50 | + try: |
| 51 | + for line in open(versionfile_source,"r").readlines(): |
| 52 | + if line.strip().startswith("git_refnames ="): |
| 53 | + mo = re.search(r'=\s*"(.*)"', line) |
| 54 | + if mo: |
| 55 | + variables["refnames"] = mo.group(1) |
| 56 | + if line.strip().startswith("git_full ="): |
| 57 | + mo = re.search(r'=\s*"(.*)"', line) |
| 58 | + if mo: |
| 59 | + variables["full"] = mo.group(1) |
| 60 | + except EnvironmentError: |
| 61 | + pass |
| 62 | + return variables |
| 63 | + |
| 64 | +def versions_from_expanded_variables(variables, tag_prefix, verbose=False): |
| 65 | + refnames = variables["refnames"].strip() |
| 66 | + if refnames.startswith("$Format"): |
| 67 | + if verbose: |
| 68 | + print("variables are unexpanded, not using") |
| 69 | + return {} # unexpanded, so not in an unpacked git-archive tarball |
| 70 | + refs = set([r.strip() for r in refnames.strip("()").split(",")]) |
| 71 | + for ref in list(refs): |
| 72 | + if not re.search(r'\d', ref): |
| 73 | + if verbose: |
| 74 | + print("discarding '%s', no digits" % ref) |
| 75 | + refs.discard(ref) |
| 76 | + # Assume all version tags have a digit. git's %d expansion |
| 77 | + # behaves like git log --decorate=short and strips out the |
| 78 | + # refs/heads/ and refs/tags/ prefixes that would let us |
| 79 | + # distinguish between branches and tags. By ignoring refnames |
| 80 | + # without digits, we filter out many common branch names like |
| 81 | + # "release" and "stabilization", as well as "HEAD" and "master". |
| 82 | + if verbose: |
| 83 | + print("remaining refs: %s" % ",".join(sorted(refs))) |
| 84 | + for ref in sorted(refs): |
| 85 | + # sorting will prefer e.g. "2.0" over "2.0rc1" |
| 86 | + if ref.startswith(tag_prefix): |
| 87 | + r = ref[len(tag_prefix):] |
| 88 | + if verbose: |
| 89 | + print("picking %s" % r) |
| 90 | + return { "version": r, |
| 91 | + "full": variables["full"].strip() } |
| 92 | + # no suitable tags, so we use the full revision id |
| 93 | + if verbose: |
| 94 | + print("no suitable tags, using full revision id") |
| 95 | + return { "version": variables["full"].strip(), |
| 96 | + "full": variables["full"].strip() } |
| 97 | + |
| 98 | +def versions_from_vcs(tag_prefix, versionfile_source, verbose=False): |
| 99 | + # this runs 'git' from the root of the source tree. That either means |
| 100 | + # someone ran a setup.py command (and this code is in versioneer.py, so |
| 101 | + # IN_LONG_VERSION_PY=False, thus the containing directory is the root of |
| 102 | + # the source tree), or someone ran a project-specific entry point (and |
| 103 | + # this code is in _version.py, so IN_LONG_VERSION_PY=True, thus the |
| 104 | + # containing directory is somewhere deeper in the source tree). This only |
| 105 | + # gets called if the git-archive 'subst' variables were *not* expanded, |
| 106 | + # and _version.py hasn't already been rewritten with a short version |
| 107 | + # string, meaning we're inside a checked out source tree. |
| 108 | + |
| 109 | + try: |
| 110 | + here = os.path.abspath(__file__) |
| 111 | + except NameError: |
| 112 | + # some py2exe/bbfreeze/non-CPython implementations don't do __file__ |
| 113 | + return {} # not always correct |
| 114 | + |
| 115 | + # versionfile_source is the relative path from the top of the source tree |
| 116 | + # (where the .git directory might live) to this file. Invert this to find |
| 117 | + # the root from __file__. |
| 118 | + root = here |
| 119 | + if IN_LONG_VERSION_PY: |
| 120 | + for i in range(len(versionfile_source.split("/"))): |
| 121 | + root = os.path.dirname(root) |
| 122 | + else: |
| 123 | + root = os.path.dirname(here) |
| 124 | + if not os.path.exists(os.path.join(root, ".git")): |
| 125 | + if verbose: |
| 126 | + print("no .git in %s" % root) |
| 127 | + return {} |
| 128 | + |
| 129 | + GIT = "git" |
| 130 | + if sys.platform == "win32": |
| 131 | + GIT = "git.cmd" |
| 132 | + stdout = run_command([GIT, "describe", "--tags", "--dirty", "--always"], |
| 133 | + cwd=root) |
| 134 | + if stdout is None: |
| 135 | + return {} |
| 136 | + if not stdout.startswith(tag_prefix): |
| 137 | + if verbose: |
| 138 | + print("tag '%s' doesn't start with prefix '%s'" % (stdout, tag_prefix)) |
| 139 | + return {} |
| 140 | + tag = stdout[len(tag_prefix):] |
| 141 | + stdout = run_command([GIT, "rev-parse", "HEAD"], cwd=root) |
| 142 | + if stdout is None: |
| 143 | + return {} |
| 144 | + full = stdout.strip() |
| 145 | + if tag.endswith("-dirty"): |
| 146 | + full += "-dirty" |
| 147 | + return {"version": tag, "full": full} |
| 148 | + |
| 149 | + |
| 150 | +def versions_from_parentdir(parentdir_prefix, versionfile_source, verbose=False): |
| 151 | + if IN_LONG_VERSION_PY: |
| 152 | + # We're running from _version.py. If it's from a source tree |
| 153 | + # (execute-in-place), we can work upwards to find the root of the |
| 154 | + # tree, and then check the parent directory for a version string. If |
| 155 | + # it's in an installed application, there's no hope. |
| 156 | + try: |
| 157 | + here = os.path.abspath(__file__) |
| 158 | + except NameError: |
| 159 | + # py2exe/bbfreeze/non-CPython don't have __file__ |
| 160 | + return {} # without __file__, we have no hope |
| 161 | + # versionfile_source is the relative path from the top of the source |
| 162 | + # tree to _version.py. Invert this to find the root from __file__. |
| 163 | + root = here |
| 164 | + for i in range(len(versionfile_source.split("/"))): |
| 165 | + root = os.path.dirname(root) |
| 166 | + else: |
| 167 | + # we're running from versioneer.py, which means we're running from |
| 168 | + # the setup.py in a source tree. sys.argv[0] is setup.py in the root. |
| 169 | + here = os.path.abspath(sys.argv[0]) |
| 170 | + root = os.path.dirname(here) |
| 171 | + |
| 172 | + # Source tarballs conventionally unpack into a directory that includes |
| 173 | + # both the project name and a version string. |
| 174 | + dirname = os.path.basename(root) |
| 175 | + if not dirname.startswith(parentdir_prefix): |
| 176 | + if verbose: |
| 177 | + print("guessing rootdir is '%s', but '%s' doesn't start with prefix '%s'" % |
| 178 | + (root, dirname, parentdir_prefix)) |
| 179 | + return None |
| 180 | + return {"version": dirname[len(parentdir_prefix):], "full": ""} |
| 181 | + |
| 182 | +tag_prefix = "" |
| 183 | +parentdir_prefix = "conda-" |
| 184 | +versionfile_source = "conda/_version.py" |
| 185 | + |
| 186 | +def get_versions(default={"version": "unknown", "full": ""}, verbose=False): |
| 187 | + variables = { "refnames": git_refnames, "full": git_full } |
| 188 | + ver = versions_from_expanded_variables(variables, tag_prefix, verbose) |
| 189 | + if not ver: |
| 190 | + ver = versions_from_vcs(tag_prefix, versionfile_source, verbose) |
| 191 | + if not ver: |
| 192 | + ver = versions_from_parentdir(parentdir_prefix, versionfile_source, |
| 193 | + verbose) |
| 194 | + if not ver: |
| 195 | + ver = default |
| 196 | + return ver |
| 197 | + |
0 commit comments