build_info.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env python
  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. return {
  85. "major": match.group(1),
  86. "minor": match.group(2),
  87. "patch": match.group(3),
  88. "flavor": match.group(4).lstrip("-"),
  89. "-flavor": match.group(4),
  90. "date": match.group(5),
  91. }
  92. raise Exception('No version number found in {}'.format(changes_file))
  93. def describe(directory):
  94. """Returns a string describing the current Git HEAD version as descriptively
  95. as possible.
  96. Runs 'git describe', or alternately 'git rev-parse HEAD', in directory. If
  97. successful, returns the output; otherwise returns 'unknown hash, <date>'."""
  98. try:
  99. # decode() is needed here for Python3 compatibility. In Python2,
  100. # str and bytes are the same type, but not in Python3.
  101. # Popen.communicate() returns a bytes instance, which needs to be
  102. # decoded into text data first in Python3. And this decode() won't
  103. # hurt Python2.
  104. return command_output(['git', 'describe'], directory).rstrip().decode()
  105. except:
  106. try:
  107. return command_output(
  108. ['git', 'rev-parse', 'HEAD'], directory).rstrip().decode()
  109. except:
  110. # This is the fallback case where git gives us no information,
  111. # e.g. because the source tree might not be in a git tree.
  112. # In this case, usually use a timestamp. However, to ensure
  113. # reproducible builds, allow the builder to override the wall
  114. # clock time with environment variable SOURCE_DATE_EPOCH
  115. # containing a (presumably) fixed timestamp.
  116. timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
  117. formatted = datetime.datetime.utcfromtimestamp(timestamp).isoformat()
  118. return 'unknown hash, {}'.format(formatted)
  119. def parse_args():
  120. directory = None
  121. input_string = None
  122. input_file = None
  123. output_file = None
  124. if len(sys.argv) < 2:
  125. raise Exception("Invalid number of arguments")
  126. directory = sys.argv[1]
  127. i = 2
  128. if not sys.argv[i].startswith("-"):
  129. input_string = sys.argv[i]
  130. i = i + 1
  131. while i < len(sys.argv):
  132. opt = sys.argv[i]
  133. i = i + 1
  134. if opt == "-i" or opt == "-o":
  135. if i == len(sys.argv):
  136. raise Exception("Expected path after {}".format(opt))
  137. val = sys.argv[i]
  138. i = i + 1
  139. if (opt == "-i"):
  140. input_file = val
  141. elif (opt == "-o"):
  142. output_file = val
  143. else:
  144. raise Exception("Unknown flag {}".format(opt))
  145. return {
  146. "directory": directory,
  147. "input_string": input_string,
  148. "input_file": input_file,
  149. "output_file": output_file,
  150. }
  151. def main():
  152. args = None
  153. try:
  154. args = parse_args()
  155. except Exception as e:
  156. print(e)
  157. print("\nUsage:\n")
  158. print(usage.format(sys.argv[0]))
  159. sys.exit(1)
  160. directory = args["directory"]
  161. template = args["input_string"]
  162. if template == None:
  163. with open(args["input_file"], 'r') as f:
  164. template = f.read()
  165. output_file = args["output_file"]
  166. software_version = deduce_software_version(directory)
  167. commit = describe(directory)
  168. output = template \
  169. .replace("<major>", software_version["major"]) \
  170. .replace("<minor>", software_version["minor"]) \
  171. .replace("<patch>", software_version["patch"]) \
  172. .replace("<flavor>", software_version["flavor"]) \
  173. .replace("<-flavor>", software_version["-flavor"]) \
  174. .replace("<date>", software_version["date"]) \
  175. .replace("<commit>", commit)
  176. if output_file is None:
  177. print(output)
  178. else:
  179. mkdir_p(os.path.dirname(output_file))
  180. if os.path.isfile(output_file):
  181. with open(output_file, 'r') as f:
  182. if output == f.read():
  183. return
  184. with open(output_file, 'w') as f:
  185. f.write(output)
  186. if __name__ == '__main__':
  187. main()