|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026 Google LLC |
| 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 | + |
| 16 | +import sys |
| 17 | +import yaml |
| 18 | +import re |
| 19 | + |
| 20 | +def update_config(target_path, source_path, prefix): |
| 21 | + """ |
| 22 | + Reads source_path, prepends prefix to paths in deep-remove-regex and deep-preserve-regex, |
| 23 | + and writes to target_path. |
| 24 | + """ |
| 25 | + with open(source_path, 'r') as f: |
| 26 | + source_content = f.read() |
| 27 | + |
| 28 | + # Load source data |
| 29 | + source_data = yaml.safe_load(source_content) or {} |
| 30 | + |
| 31 | + # Define fields to update |
| 32 | + fields_to_update = ['deep-remove-regex', 'deep-preserve-regex'] |
| 33 | + |
| 34 | + for field in fields_to_update: |
| 35 | + if field in source_data: |
| 36 | + updated_list = [] |
| 37 | + for item in source_data[field]: |
| 38 | + # If item is a string, prepend prefix |
| 39 | + # Regex might need handling if it starts with ^ |
| 40 | + # But usually these are just paths. |
| 41 | + # Assuming simple concatenation for now as per requirement. |
| 42 | + # "When referencing paths in the deep-remove-regex and deep-preserve-regex, the new directory name should be prefixed" |
| 43 | + |
| 44 | + # If the regex starts with ^, insert the prefix after it. |
| 45 | + if item.startswith('^'): |
| 46 | + updated_list.append(f"^{prefix}/{item[1:]}") |
| 47 | + else: |
| 48 | + updated_list.append(f"/{prefix}{item}") |
| 49 | + source_data[field] = updated_list |
| 50 | + |
| 51 | + if 'deep-copy-regex' in source_data: |
| 52 | + for item in source_data['deep-copy-regex']: |
| 53 | + if 'dest' in item and item['dest'].startswith('/owl-bot-staging/'): |
| 54 | + item['dest'] = item['dest'].replace('/owl-bot-staging/', f'/owl-bot-staging/{prefix}/', 1) |
| 55 | + |
| 56 | + # Write to target_path |
| 57 | + with open(target_path, 'w') as f: |
| 58 | + # Check if there was a license header in the original file |
| 59 | + # Match a block of lines starting with # at the beginning of the file |
| 60 | + header_match = re.search(r'^((?:#[^\n]*\n)+)', source_content) |
| 61 | + if header_match: |
| 62 | + f.write(header_match.group(1)) |
| 63 | + f.write("\n") # Add a newline after the header |
| 64 | + |
| 65 | + # Use yaml.dump to write the data. |
| 66 | + yaml.dump(source_data, f, sort_keys=False, default_flow_style=False, indent=2) |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + if len(sys.argv) != 4: |
| 70 | + print("Usage: python3 update_owlbot_hermetic.py <target_file> <source_file> <prefix>") |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + target_path = sys.argv[1] |
| 74 | + source_path = sys.argv[2] |
| 75 | + prefix = sys.argv[3] |
| 76 | + |
| 77 | + update_config(target_path, source_path, prefix) |
0 commit comments