You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_version.py 7.8 KiB

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