1#!/usr/bin/env python3 2 3# Copyright (c) 2023 Google Inc. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# Args: <CHANGES-file> <tag> <output-file> 18# Updates an output file with changelog from the given CHANGES file and tag. 19# - search for first line matching <tag> in file <CHANGES-file> 20# - search for the next line with a tag 21# - writes all the lines in between those 2 tags into <output-file> 22 23import errno 24import os 25import os.path 26import re 27import subprocess 28import logging 29import sys 30 31# Regex to match the SPIR-V version tag. 32# Example of matching tags: 33# - v2020.1 34# - v2020.1-dev 35# - v2020.1.rc1 36VERSION_REGEX = re.compile(r'^(v\d+\.\d+) +[0-9]+-[0-9]+-[0-9]+$') 37 38def mkdir_p(directory): 39 """Make the directory, and all its ancestors as required. Any of the 40 directories are allowed to already exist.""" 41 42 if directory == "": 43 # We're being asked to make the current directory. 44 return 45 46 try: 47 os.makedirs(directory) 48 except OSError as e: 49 if e.errno == errno.EEXIST and os.path.isdir(directory): 50 pass 51 else: 52 raise 53 54def main(): 55 FORMAT = '%(asctime)s %(message)s' 56 logging.basicConfig(format="[%(asctime)s][%(levelname)-8s] %(message)s", datefmt="%H:%M:%S") 57 if len(sys.argv) != 4: 58 logging.error("usage: {} <CHANGES-path> <tag> <output-file>".format(sys.argv[0])) 59 sys.exit(1) 60 61 changes_path = sys.argv[1] 62 start_tag = sys.argv[2] 63 output_file_path = sys.argv[3] 64 65 changelog = [] 66 has_found_start = False 67 with open(changes_path, "r") as file: 68 for line in file.readlines(): 69 m = VERSION_REGEX.match(line) 70 if m: 71 print(m.groups()[0]) 72 print(start_tag) 73 if has_found_start: 74 break; 75 if start_tag == m.groups()[0]: 76 has_found_start = True 77 continue 78 79 if has_found_start: 80 changelog.append(line) 81 82 if not has_found_start: 83 logging.error("No tag matching {} found.".format(start_tag)) 84 sys.exit(1) 85 86 content = "".join(changelog) 87 if os.path.isfile(output_file_path): 88 with open(output_file_path, 'r') as f: 89 if content == f.read(): 90 sys.exit(0) 91 92 mkdir_p(os.path.dirname(output_file_path)) 93 with open(output_file_path, 'w') as f: 94 f.write(content) 95 sys.exit(0) 96 97if __name__ == '__main__': 98 main() 99