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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial