update_build_version.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016 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. # Updates an output file with version info unless the new content is the same
  16. # as the existing content.
  17. #
  18. # Args: <changes-file> <output-file>
  19. #
  20. # The output file will contain a line of text consisting of two C source syntax
  21. # string literals separated by a comma:
  22. # - The software version deduced from the given CHANGES file.
  23. # - A longer string with the project name, the software version number, and
  24. # git commit information for the CHANGES file's directory. The commit
  25. # information is the content of the FORCED_BUILD_VERSION_DESCRIPTION
  26. # environement variable is it exists, else the output of "git describe" if
  27. # that succeeds, or "git rev-parse HEAD" if that succeeds, or otherwise a
  28. # message containing the phrase "unknown hash".
  29. # The string contents are escaped as necessary.
  30. import datetime
  31. import errno
  32. import os
  33. import os.path
  34. import re
  35. import subprocess
  36. import logging
  37. import sys
  38. import time
  39. # Format of the output generated by this script. Example:
  40. # "v2023.1", "SPIRV-Tools v2023.1 0fc5526f2b01a0cc89192c10cf8bef77f1007a62, 2023-01-18T14:51:49"
  41. OUTPUT_FORMAT = '"{version_tag}", "SPIRV-Tools {version_tag} {description}"\n'
  42. def mkdir_p(directory):
  43. """Make the directory, and all its ancestors as required. Any of the
  44. directories are allowed to already exist."""
  45. if directory == "":
  46. # We're being asked to make the current directory.
  47. return
  48. try:
  49. os.makedirs(directory)
  50. except OSError as e:
  51. if e.errno == errno.EEXIST and os.path.isdir(directory):
  52. pass
  53. else:
  54. raise
  55. def command_output(cmd, directory):
  56. """Runs a command in a directory and returns its standard output stream.
  57. Returns (False, None) if the command fails to launch or otherwise fails.
  58. """
  59. try:
  60. # Set shell=True on Windows so that Chromium's git.bat can be found when
  61. # 'git' is invoked.
  62. p = subprocess.Popen(cmd,
  63. cwd=directory,
  64. stdout=subprocess.PIPE,
  65. stderr=subprocess.PIPE,
  66. shell=os.name == 'nt')
  67. (stdout, _) = p.communicate()
  68. if p.returncode != 0:
  69. return False, None
  70. except Exception as e:
  71. return False, None
  72. return p.returncode == 0, stdout
  73. def deduce_software_version(changes_file):
  74. """Returns a tuple (success, software version number) parsed from the
  75. given CHANGES file.
  76. Success is set to True if the software version could be deduced.
  77. Software version is undefined if success if False.
  78. Function expects the CHANGES file to describes most recent versions first.
  79. """
  80. # Match the first well-formed version-and-date line
  81. # Allow trailing whitespace in the checked-out source code has
  82. # unexpected carriage returns on a linefeed-only system such as
  83. # Linux.
  84. pattern = re.compile(r'^(v\d+\.\d+(-dev)?) \d\d\d\d-\d\d-\d\d\s*$')
  85. with open(changes_file, mode='r') as f:
  86. for line in f.readlines():
  87. match = pattern.match(line)
  88. if match:
  89. return True, match.group(1)
  90. return False, None
  91. def describe(repo_path):
  92. """Returns a string describing the current Git HEAD version as descriptively
  93. as possible.
  94. Runs 'git describe', or alternately 'git rev-parse HEAD', in directory. If
  95. successful, returns the output; otherwise returns 'unknown hash, <date>'."""
  96. # if we're in a git repository, attempt to extract version info
  97. success, output = command_output(["git", "rev-parse", "--show-toplevel"], repo_path)
  98. if success:
  99. success, output = command_output(["git", "describe", "--tags", "--match=v*", "--long"], repo_path)
  100. if not success:
  101. success, output = command_output(["git", "rev-parse", "HEAD"], repo_path)
  102. if success:
  103. # decode() is needed here for Python3 compatibility. In Python2,
  104. # str and bytes are the same type, but not in Python3.
  105. # Popen.communicate() returns a bytes instance, which needs to be
  106. # decoded into text data first in Python3. And this decode() won't
  107. # hurt Python2.
  108. return output.rstrip().decode()
  109. # This is the fallback case where git gives us no information,
  110. # e.g. because the source tree might not be in a git tree or
  111. # git is not available on the system.
  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. iso_date = datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc).isoformat()
  118. return "unknown hash, {}".format(iso_date)
  119. def main():
  120. FORMAT = '%(asctime)s %(message)s'
  121. logging.basicConfig(format="[%(asctime)s][%(levelname)-8s] %(message)s", datefmt="%H:%M:%S")
  122. if len(sys.argv) != 3:
  123. logging.error("usage: {} <repo-path> <output-file>".format(sys.argv[0]))
  124. sys.exit(1)
  125. changes_file_path = os.path.realpath(sys.argv[1])
  126. output_file_path = sys.argv[2]
  127. success, version = deduce_software_version(changes_file_path)
  128. if not success:
  129. logging.error("Could not deduce latest release version from {}.".format(changes_file_path))
  130. sys.exit(1)
  131. repo_path = os.path.dirname(changes_file_path)
  132. description = os.getenv("FORCED_BUILD_VERSION_DESCRIPTION", describe(repo_path))
  133. content = OUTPUT_FORMAT.format(version_tag=version, description=description)
  134. # Escape file content.
  135. content.replace('"', '\\"')
  136. if os.path.isfile(output_file_path):
  137. with open(output_file_path, 'r') as f:
  138. if content == f.read():
  139. return
  140. mkdir_p(os.path.dirname(output_file_path))
  141. with open(output_file_path, 'w') as f:
  142. f.write(content)
  143. if __name__ == '__main__':
  144. main()