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