_version.py 7.0 KB

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