build_info.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2020 Google Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import datetime
  16. import errno
  17. import os
  18. import os.path
  19. import re
  20. import subprocess
  21. import sys
  22. import time
  23. usage = """{} emits a string to stdout or file with project version information.
  24. args: <project-dir> [<input-string>] [-i <input-file>] [-o <output-file>]
  25. Either <input-string> or -i <input-file> needs to be provided.
  26. The tool will output the provided string or file content with the following
  27. tokens substituted:
  28. <major> - The major version point parsed from the CHANGES.md file.
  29. <minor> - The minor version point parsed from the CHANGES.md file.
  30. <patch> - The point version point parsed from the CHANGES.md file.
  31. <flavor> - The optional dash suffix parsed from the CHANGES.md file (excluding
  32. dash prefix).
  33. <-flavor> - The optional dash suffix parsed from the CHANGES.md file (including
  34. dash prefix).
  35. <date> - The optional date of the release in the form YYYY-MM-DD
  36. <commit> - The git commit information for the directory taken from
  37. "git describe" if that succeeds, or "git rev-parse HEAD"
  38. if that succeeds, or otherwise a message containing the phrase
  39. "unknown hash".
  40. -o is an optional flag for writing the output string to the given file. If
  41. ommitted then the string is printed to stdout.
  42. """
  43. try:
  44. utc = datetime.timezone.utc
  45. except AttributeError:
  46. # Python 2? In datetime.date.today().year? Yes.
  47. class UTC(datetime.tzinfo):
  48. ZERO = datetime.timedelta(0)
  49. def utcoffset(self, dt):
  50. return self.ZERO
  51. def tzname(self, dt):
  52. return "UTC"
  53. def dst(self, dt):
  54. return self.ZERO
  55. utc = UTC()
  56. def mkdir_p(directory):
  57. """Make the directory, and all its ancestors as required. Any of the
  58. directories are allowed to already exist."""
  59. if directory == "":
  60. # We're being asked to make the current directory.
  61. return
  62. try:
  63. os.makedirs(directory)
  64. except OSError as e:
  65. if e.errno == errno.EEXIST and os.path.isdir(directory):
  66. pass
  67. else:
  68. raise
  69. def command_output(cmd, directory):
  70. """Runs a command in a directory and returns its standard output stream.
  71. Captures the standard error stream.
  72. Raises a RuntimeError if the command fails to launch or otherwise fails.
  73. """
  74. p = subprocess.Popen(cmd,
  75. cwd=directory,
  76. stdout=subprocess.PIPE,
  77. stderr=subprocess.PIPE)
  78. (stdout, _) = p.communicate()
  79. if p.returncode != 0:
  80. raise RuntimeError('Failed to run %s in %s' % (cmd, directory))
  81. return stdout
  82. def deduce_software_version(directory):
  83. """Returns a software version number parsed from the CHANGES.md file
  84. in the given directory.
  85. The CHANGES.md file describes most recent versions first.
  86. """
  87. # Match the first well-formed version-and-date line.
  88. # Allow trailing whitespace in the checked-out source code has
  89. # unexpected carriage returns on a linefeed-only system such as
  90. # Linux.
  91. pattern = re.compile(r'^#* +(\d+)\.(\d+)\.(\d+)(-\w+)? (\d\d\d\d-\d\d-\d\d)? *$')
  92. changes_file = os.path.join(directory, 'CHANGES.md')
  93. with open(changes_file, mode='r') as f:
  94. for line in f.readlines():
  95. match = pattern.match(line)
  96. if match:
  97. flavor = match.group(4)
  98. if flavor == None:
  99. flavor = ""
  100. return {
  101. "major": match.group(1),
  102. "minor": match.group(2),
  103. "patch": match.group(3),
  104. "flavor": flavor.lstrip("-"),
  105. "-flavor": flavor,
  106. "date": match.group(5),
  107. }
  108. raise Exception('No version number found in {}'.format(changes_file))
  109. def describe(directory):
  110. """Returns a string describing the current Git HEAD version as descriptively
  111. as possible.
  112. Runs 'git describe', or alternately 'git rev-parse HEAD', in directory. If
  113. successful, returns the output; otherwise returns 'unknown hash, <date>'."""
  114. try:
  115. # decode() is needed here for Python3 compatibility. In Python2,
  116. # str and bytes are the same type, but not in Python3.
  117. # Popen.communicate() returns a bytes instance, which needs to be
  118. # decoded into text data first in Python3. And this decode() won't
  119. # hurt Python2.
  120. return command_output(['git', 'describe'], directory).rstrip().decode()
  121. except:
  122. try:
  123. return command_output(
  124. ['git', 'rev-parse', 'HEAD'], directory).rstrip().decode()
  125. except:
  126. # This is the fallback case where git gives us no information,
  127. # e.g. because the source tree might not be in a git tree.
  128. # In this case, usually use a timestamp. However, to ensure
  129. # reproducible builds, allow the builder to override the wall
  130. # clock time with environment variable SOURCE_DATE_EPOCH
  131. # containing a (presumably) fixed timestamp.
  132. timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
  133. formatted = datetime.datetime.fromtimestamp(timestamp, utc).isoformat()
  134. return 'unknown hash, {}'.format(formatted)
  135. def parse_args():
  136. directory = None
  137. input_string = None
  138. input_file = None
  139. output_file = None
  140. if len(sys.argv) < 2:
  141. raise Exception("Invalid number of arguments")
  142. directory = sys.argv[1]
  143. i = 2
  144. if not sys.argv[i].startswith("-"):
  145. input_string = sys.argv[i]
  146. i = i + 1
  147. while i < len(sys.argv):
  148. opt = sys.argv[i]
  149. i = i + 1
  150. if opt == "-i" or opt == "-o":
  151. if i == len(sys.argv):
  152. raise Exception("Expected path after {}".format(opt))
  153. val = sys.argv[i]
  154. i = i + 1
  155. if (opt == "-i"):
  156. input_file = val
  157. elif (opt == "-o"):
  158. output_file = val
  159. else:
  160. raise Exception("Unknown flag {}".format(opt))
  161. return {
  162. "directory": directory,
  163. "input_string": input_string,
  164. "input_file": input_file,
  165. "output_file": output_file,
  166. }
  167. def main():
  168. args = None
  169. try:
  170. args = parse_args()
  171. except Exception as e:
  172. print(e)
  173. print("\nUsage:\n")
  174. print(usage.format(sys.argv[0]))
  175. sys.exit(1)
  176. directory = args["directory"]
  177. template = args["input_string"]
  178. if template == None:
  179. with open(args["input_file"], 'r') as f:
  180. template = f.read()
  181. output_file = args["output_file"]
  182. software_version = deduce_software_version(directory)
  183. commit = describe(directory)
  184. output = template \
  185. .replace("@major@", software_version["major"]) \
  186. .replace("@minor@", software_version["minor"]) \
  187. .replace("@patch@", software_version["patch"]) \
  188. .replace("@flavor@", software_version["flavor"]) \
  189. .replace("@-flavor@", software_version["-flavor"]) \
  190. .replace("@date@", software_version["date"]) \
  191. .replace("@commit@", commit)
  192. if output_file is None:
  193. print(output)
  194. else:
  195. mkdir_p(os.path.dirname(output_file))
  196. if os.path.isfile(output_file):
  197. with open(output_file, 'r') as f:
  198. if output == f.read():
  199. return
  200. with open(output_file, 'w') as f:
  201. f.write(output)
  202. if __name__ == '__main__':
  203. main()