build_info.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. def mkdir_p(directory):
  44. """Make the directory, and all its ancestors as required. Any of the
  45. directories are allowed to already exist."""
  46. if directory == "":
  47. # We're being asked to make the current directory.
  48. return
  49. try:
  50. os.makedirs(directory)
  51. except OSError as e:
  52. if e.errno == errno.EEXIST and os.path.isdir(directory):
  53. pass
  54. else:
  55. raise
  56. def command_output(cmd, directory):
  57. """Runs a command in a directory and returns its standard output stream.
  58. Captures the standard error stream.
  59. Raises a RuntimeError if the command fails to launch or otherwise fails.
  60. """
  61. p = subprocess.Popen(cmd,
  62. cwd=directory,
  63. stdout=subprocess.PIPE,
  64. stderr=subprocess.PIPE)
  65. (stdout, _) = p.communicate()
  66. if p.returncode != 0:
  67. raise RuntimeError('Failed to run %s in %s' % (cmd, directory))
  68. return stdout
  69. def deduce_software_version(directory):
  70. """Returns a software version number parsed from the CHANGES.md file
  71. in the given directory.
  72. The CHANGES.md file describes most recent versions first.
  73. """
  74. # Match the first well-formed version-and-date line.
  75. # Allow trailing whitespace in the checked-out source code has
  76. # unexpected carriage returns on a linefeed-only system such as
  77. # Linux.
  78. pattern = re.compile(r'^#* +(\d+)\.(\d+)\.(\d+)(-\w+)? (\d\d\d\d-\d\d-\d\d)? *$')
  79. changes_file = os.path.join(directory, 'CHANGES.md')
  80. with open(changes_file, mode='r') as f:
  81. for line in f.readlines():
  82. match = pattern.match(line)
  83. if match:
  84. flavor = match.group(4)
  85. if flavor == None:
  86. flavor = ""
  87. return {
  88. "major": match.group(1),
  89. "minor": match.group(2),
  90. "patch": match.group(3),
  91. "flavor": flavor.lstrip("-"),
  92. "-flavor": flavor,
  93. "date": match.group(5),
  94. }
  95. raise Exception('No version number found in {}'.format(changes_file))
  96. def describe(directory):
  97. """Returns a string describing the current Git HEAD version as descriptively
  98. as possible.
  99. Runs 'git describe', or alternately 'git rev-parse HEAD', in directory. If
  100. successful, returns the output; otherwise returns 'unknown hash, <date>'."""
  101. try:
  102. # decode() is needed here for Python3 compatibility. In Python2,
  103. # str and bytes are the same type, but not in Python3.
  104. # Popen.communicate() returns a bytes instance, which needs to be
  105. # decoded into text data first in Python3. And this decode() won't
  106. # hurt Python2.
  107. return command_output(['git', 'describe'], directory).rstrip().decode()
  108. except:
  109. try:
  110. return command_output(
  111. ['git', 'rev-parse', 'HEAD'], directory).rstrip().decode()
  112. except:
  113. # This is the fallback case where git gives us no information,
  114. # e.g. because the source tree might not be in a git tree.
  115. # In this case, usually use a timestamp. However, to ensure
  116. # reproducible builds, allow the builder to override the wall
  117. # clock time with environment variable SOURCE_DATE_EPOCH
  118. # containing a (presumably) fixed timestamp.
  119. timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
  120. formatted = datetime.datetime.utcfromtimestamp(timestamp).isoformat()
  121. return 'unknown hash, {}'.format(formatted)
  122. def parse_args():
  123. directory = None
  124. input_string = None
  125. input_file = None
  126. output_file = None
  127. if len(sys.argv) < 2:
  128. raise Exception("Invalid number of arguments")
  129. directory = sys.argv[1]
  130. i = 2
  131. if not sys.argv[i].startswith("-"):
  132. input_string = sys.argv[i]
  133. i = i + 1
  134. while i < len(sys.argv):
  135. opt = sys.argv[i]
  136. i = i + 1
  137. if opt == "-i" or opt == "-o":
  138. if i == len(sys.argv):
  139. raise Exception("Expected path after {}".format(opt))
  140. val = sys.argv[i]
  141. i = i + 1
  142. if (opt == "-i"):
  143. input_file = val
  144. elif (opt == "-o"):
  145. output_file = val
  146. else:
  147. raise Exception("Unknown flag {}".format(opt))
  148. return {
  149. "directory": directory,
  150. "input_string": input_string,
  151. "input_file": input_file,
  152. "output_file": output_file,
  153. }
  154. def main():
  155. args = None
  156. try:
  157. args = parse_args()
  158. except Exception as e:
  159. print(e)
  160. print("\nUsage:\n")
  161. print(usage.format(sys.argv[0]))
  162. sys.exit(1)
  163. directory = args["directory"]
  164. template = args["input_string"]
  165. if template == None:
  166. with open(args["input_file"], 'r') as f:
  167. template = f.read()
  168. output_file = args["output_file"]
  169. software_version = deduce_software_version(directory)
  170. commit = describe(directory)
  171. output = template \
  172. .replace("@major@", software_version["major"]) \
  173. .replace("@minor@", software_version["minor"]) \
  174. .replace("@patch@", software_version["patch"]) \
  175. .replace("@flavor@", software_version["flavor"]) \
  176. .replace("@-flavor@", software_version["-flavor"]) \
  177. .replace("@date@", software_version["date"]) \
  178. .replace("@commit@", commit)
  179. if output_file is None:
  180. print(output)
  181. else:
  182. mkdir_p(os.path.dirname(output_file))
  183. if os.path.isfile(output_file):
  184. with open(output_file, 'r') as f:
  185. if output == f.read():
  186. return
  187. with open(output_file, 'w') as f:
  188. f.write(output)
  189. if __name__ == '__main__':
  190. main()