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

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:824a9b3db67a
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 (build 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 versioneer-0.6
9 # (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
16 import subprocess
17
18 def run_command(args, cwd=None, verbose=False):
19 try:
20 # remember shell=False, so use git.cmd on windows, not just git
21 p = subprocess.Popen(args, stdout=subprocess.PIPE, cwd=cwd)
22 except EnvironmentError, e:
23 if verbose:
24 print "unable to run %s" % args[0]
25 print e
26 return None
27 stdout = p.communicate()[0].strip()
28 if p.returncode != 0:
29 if verbose:
30 print "unable to run %s (error)" % args[0]
31 return None
32 return stdout
33
34
35 import sys
36 import re
37 import os.path
38
39 def get_expanded_variables(versionfile_source):
40 # the code embedded in _version.py can just fetch the value of these
41 # variables. When used from setup.py, we don't want to import
42 # _version.py, so we do it with a regexp instead. This function is not
43 # used from _version.py.
44 variables = {}
45 try:
46 for line in open(versionfile_source,"r").readlines():
47 if line.strip().startswith("git_refnames ="):
48 mo = re.search(r'=\s*"(.*)"', line)
49 if mo:
50 variables["refnames"] = mo.group(1)
51 if line.strip().startswith("git_full ="):
52 mo = re.search(r'=\s*"(.*)"', line)
53 if mo:
54 variables["full"] = mo.group(1)
55 except EnvironmentError:
56 pass
57 return variables
58
59 def versions_from_expanded_variables(variables, tag_prefix):
60 refnames = variables["refnames"].strip()
61 if refnames.startswith("$Format"):
62 return {} # unexpanded, so not in an unpacked git-archive tarball
63 refs = set([r.strip() for r in refnames.strip("()").split(",")])
64 for ref in list(refs):
65 if not re.search(r'\d', ref):
66 refs.discard(ref)
67 # Assume all version tags have a digit. git's %d expansion
68 # behaves like git log --decorate=short and strips out the
69 # refs/heads/ and refs/tags/ prefixes that would let us
70 # distinguish between branches and tags. By ignoring refnames
71 # without digits, we filter out many common branch names like
72 # "release" and "stabilization", as well as "HEAD" and "master".
73 for ref in sorted(refs):
74 # sorting will prefer e.g. "2.0" over "2.0rc1"
75 if ref.startswith(tag_prefix):
76 r = ref[len(tag_prefix):]
77 return { "version": r,
78 "full": variables["full"].strip() }
79 # no suitable tags, so we use the full revision id
80 return { "version": variables["full"].strip(),
81 "full": variables["full"].strip() }
82
83 def versions_from_vcs(tag_prefix, versionfile_source, verbose=False):
84 # this runs 'git' from the root of the source tree. That either means
85 # someone ran a setup.py command (and this code is in versioneer.py, thus
86 # the containing directory is the root of the source tree), or someone
87 # ran a project-specific entry point (and this code is in _version.py,
88 # thus the containing directory is somewhere deeper in the source tree).
89 # This only gets called if the git-archive 'subst' variables were *not*
90 # expanded, and _version.py hasn't already been rewritten with a short
91 # version string, meaning we're inside a checked out source tree.
92
93 try:
94 here = os.path.abspath(__file__)
95 except NameError:
96 # some py2exe/bbfreeze/non-CPython implementations don't do __file__
97 return {} # not always correct
98
99 # versionfile_source is the relative path from the top of the source tree
100 # (where the .git directory might live) to this file. Invert this to find
101 # the root from __file__.
102 root = here
103 for i in range(len(versionfile_source.split("/"))):
104 root = os.path.dirname(root)
105 if not os.path.exists(os.path.join(root, ".git")):
106 return {}
107
108 GIT = "git"
109 if sys.platform == "win32":
110 GIT = "git.cmd"
111 stdout = run_command([GIT, "describe", "--tags", "--dirty", "--always"],
112 cwd=root)
113 if stdout is None:
114 return {}
115 if not stdout.startswith(tag_prefix):
116 if verbose:
117 print "tag '%s' doesn't start with prefix '%s'" % (stdout, tag_prefix)
118 return {}
119 tag = stdout[len(tag_prefix):]
120 stdout = run_command([GIT, "rev-parse", "HEAD"], cwd=root)
121 if stdout is None:
122 return {}
123 full = stdout.strip()
124 if tag.endswith("-dirty"):
125 full += "-dirty"
126 return {"version": tag, "full": full}
127
128
129 def versions_from_parentdir(parentdir_prefix, versionfile_source, verbose=False):
130 try:
131 here = os.path.abspath(__file__)
132 # versionfile_source is the relative path from the top of the source
133 # tree (where the .git directory might live) to _version.py, when
134 # this is used by the runtime. Invert this to find the root from
135 # __file__.
136 root = here
137 for i in range(len(versionfile_source.split("/"))):
138 root = os.path.dirname(root)
139 except NameError:
140 # try a couple different things to handle py2exe, bbfreeze, and
141 # non-CPython implementations which don't do __file__. This code
142 # either lives in versioneer.py (used by setup.py) or _version.py
143 # (used by the runtime). In the versioneer.py case, sys.argv[0] will
144 # be setup.py, in the root of the source tree. In the _version.py
145 # case, we have no idea what sys.argv[0] is (some
146 # application-specific runner).
147 root = os.path.dirname(os.path.abspath(sys.argv[0]))
148 # Source tarballs conventionally unpack into a directory that includes
149 # both the project name and a version string.
150 dirname = os.path.basename(root)
151 if not dirname.startswith(parentdir_prefix):
152 if verbose:
153 print "dirname '%s' doesn't start with prefix '%s'" % (dirname, parentdir_prefix)
154 return None
155 return {"version": dirname[len(parentdir_prefix):], "full": ""}
156
157 tag_prefix = ""
158 parentdir_prefix = "addon-sdk-"
159 versionfile_source = "python-lib/cuddlefish/_version.py"
160
161 def get_versions():
162 variables = { "refnames": git_refnames, "full": git_full }
163 ver = versions_from_expanded_variables(variables, tag_prefix)
164 if not ver:
165 ver = versions_from_vcs(tag_prefix, versionfile_source)
166 if not ver:
167 ver = versions_from_parentdir(parentdir_prefix, versionfile_source)
168 if not ver:
169 ver = {"version": "unknown", "full": ""}
170 return ver
171

mercurial