generate_changelog.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2023 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. # Args: <CHANGES-file> <tag> <output-file>
  16. # Updates an output file with changelog from the given CHANGES file and tag.
  17. # - search for first line matching <tag> in file <CHANGES-file>
  18. # - search for the next line with a tag
  19. # - writes all the lines in between those 2 tags into <output-file>
  20. import errno
  21. import os
  22. import os.path
  23. import re
  24. import subprocess
  25. import logging
  26. import sys
  27. # Regex to match the SPIR-V version tag.
  28. # Example of matching tags:
  29. # - v2020.1
  30. # - v2020.1-dev
  31. # - v2020.1.rc1
  32. VERSION_REGEX = re.compile(r'^(v\d+\.\d+) +[0-9]+-[0-9]+-[0-9]+$')
  33. def mkdir_p(directory):
  34. """Make the directory, and all its ancestors as required. Any of the
  35. directories are allowed to already exist."""
  36. if directory == "":
  37. # We're being asked to make the current directory.
  38. return
  39. try:
  40. os.makedirs(directory)
  41. except OSError as e:
  42. if e.errno == errno.EEXIST and os.path.isdir(directory):
  43. pass
  44. else:
  45. raise
  46. def main():
  47. FORMAT = '%(asctime)s %(message)s'
  48. logging.basicConfig(format="[%(asctime)s][%(levelname)-8s] %(message)s", datefmt="%H:%M:%S")
  49. if len(sys.argv) != 4:
  50. logging.error("usage: {} <CHANGES-path> <tag> <output-file>".format(sys.argv[0]))
  51. sys.exit(1)
  52. changes_path = sys.argv[1]
  53. start_tag = sys.argv[2]
  54. output_file_path = sys.argv[3]
  55. changelog = []
  56. has_found_start = False
  57. with open(changes_path, "r") as file:
  58. for line in file.readlines():
  59. m = VERSION_REGEX.match(line)
  60. if m:
  61. print(m.groups()[0])
  62. print(start_tag)
  63. if has_found_start:
  64. break;
  65. if start_tag == m.groups()[0]:
  66. has_found_start = True
  67. continue
  68. if has_found_start:
  69. changelog.append(line)
  70. if not has_found_start:
  71. logging.error("No tag matching {} found.".format(start_tag))
  72. sys.exit(1)
  73. content = "".join(changelog)
  74. if os.path.isfile(output_file_path):
  75. with open(output_file_path, 'r') as f:
  76. if content == f.read():
  77. sys.exit(0)
  78. mkdir_p(os.path.dirname(output_file_path))
  79. with open(output_file_path, 'w') as f:
  80. f.write(content)
  81. sys.exit(0)
  82. if __name__ == '__main__':
  83. main()