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