|
| 1 | +# Copyright (C) 2025 The Android Open Source Project |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | +import os |
| 17 | +import glob |
| 18 | +import time |
| 19 | + |
| 20 | +from golden_manager import GoldenManager, ACCESS_TYPE_SSH, ACCESS_TYPE_TOKEN |
| 21 | +from image_diff import same_image |
| 22 | + |
| 23 | +from utils import execute, ArgParseImpl |
| 24 | +from utils import prompt_helper, PROMPT_YES, PROMPT_NO |
| 25 | + |
| 26 | +def line_prompt(prompt, validator=lambda a:True): |
| 27 | + while True: |
| 28 | + res = input(f'{prompt} => ').strip() |
| 29 | + if validator(res): |
| 30 | + return res |
| 31 | + return None |
| 32 | + |
| 33 | +CONFIG_NEW_SRC_DIR = 'goldens_dir' |
| 34 | +CONFIG_GOLDENS_BRANCH = 'goldens_branch' |
| 35 | +CONFIG_GOLDENS_UPDATES = 'goldens_updates' |
| 36 | +CONFIG_GOLDENS_DELETES = 'goldens_deletes' |
| 37 | +CONFIG_AUTO_COMMIT = 'auto-commit' |
| 38 | +CONFIG_COMMIT_MSG = 'commit_msg' |
| 39 | + |
| 40 | +def _get_current_branch(): |
| 41 | + code, res = execute('git branch --show-current') |
| 42 | + return res.strip() |
| 43 | + |
| 44 | +def _file_as_str(fpath): |
| 45 | + with open(fpath, 'r') as f: |
| 46 | + return f.read() |
| 47 | + |
| 48 | +def _do_update(golden_manager, config): |
| 49 | + deletes = config[CONFIG_GOLDENS_DELETES] |
| 50 | + updates = config[CONFIG_GOLDENS_UPDATES] |
| 51 | + if len(deletes) == 0 and len(updates) == 0: |
| 52 | + print('Nothing to update. Exiting...') |
| 53 | + exit(0) |
| 54 | + |
| 55 | + branch = config[CONFIG_GOLDENS_BRANCH] |
| 56 | + src_dir = config[CONFIG_NEW_SRC_DIR] |
| 57 | + auto_commit = config[CONFIG_AUTO_COMMIT] |
| 58 | + commit_msg = config[CONFIG_COMMIT_MSG] |
| 59 | + golden_manager.source_from(src_dir, commit_msg, branch, |
| 60 | + updates=updates, |
| 61 | + deletes=deletes, |
| 62 | + push_to_remote=auto_commit) |
| 63 | + |
| 64 | +def _get_deletes_updates(update_dir, golden_dir): |
| 65 | + ret_delete = [] |
| 66 | + ret_update = [] |
| 67 | + for ext in ['tif', 'json']: |
| 68 | + base = set(glob.glob(f'./**/*.{ext}', root_dir=golden_dir, recursive=True)) |
| 69 | + new = set(glob.glob(f'./**/*.{ext}', root_dir=update_dir, recursive=True)) |
| 70 | + |
| 71 | + delete = list(base - new) |
| 72 | + update = list(new - base) |
| 73 | + |
| 74 | + for fpath in base.intersection(new): |
| 75 | + base_fpath = os.path.join(golden_dir, fpath) |
| 76 | + new_fpath = os.path.join(update_dir, fpath) |
| 77 | + if (ext == 'tif' and not same_image(new_fpath, base_fpath)) or \ |
| 78 | + (ext == 'json' and _file_as_str(new_fpath) != _file_as_str(base_fpath)): |
| 79 | + update.append(fpath) |
| 80 | + |
| 81 | + ret_update += update |
| 82 | + ret_delete += delete |
| 83 | + |
| 84 | + return ret_delete, ret_update |
| 85 | + |
| 86 | +# Ask a bunch of questions to gather the configuration for the update |
| 87 | +def _interactive_mode(base_golden_dir): |
| 88 | + config = {} |
| 89 | + cur_branch = _get_current_branch() |
| 90 | + if prompt_helper( |
| 91 | + f'Generate the new goldens from your local ' \ |
| 92 | + f'Filament branch? (branch={cur_branch})') == PROMPT_YES: |
| 93 | + code, res = execute('bash ./test/renderdiff/test.sh generate', |
| 94 | + capture_output=False) |
| 95 | + if code != 0: |
| 96 | + print('Failed to generate new goldens') |
| 97 | + exit(1) |
| 98 | + config[CONFIG_NEW_SRC_DIR] = os.path.join(os.getcwd(), './out/renderdiff_tests/') |
| 99 | + else: |
| 100 | + def validator(src_dir): |
| 101 | + if not os.path.exists(src_dir): |
| 102 | + print(f'Cannot find directory {src_dir}. Please try again.') |
| 103 | + return False |
| 104 | + return True |
| 105 | + |
| 106 | + config[CONFIG_NEW_SRC_DIR] = line_prompt( |
| 107 | + 'Please provide path of directory containing new goldens', |
| 108 | + validator) |
| 109 | + |
| 110 | + if prompt_helper(f'Update new goldens to branch={cur_branch}? ' |
| 111 | + '(Note that this refers to a branch in the goldens repo, not the Filament repo.)' |
| 112 | + ) == PROMPT_YES: |
| 113 | + config[CONFIG_GOLDENS_BRANCH] = cur_branch |
| 114 | + else: |
| 115 | + config[CONFIG_GOLDENS_BRANCH] = line_prompt('Please provide new branch name for update') |
| 116 | + |
| 117 | + if prompt_helper(f'Provide a commit message?') == PROMPT_YES: |
| 118 | + config[CONFIG_COMMIT_MSG] = line_prompt('Message:') |
| 119 | + else: |
| 120 | + config[CONFIG_COMMIT_MSG] = f'Update {time.time()} from filament ({cur_branch})' |
| 121 | + |
| 122 | + new_golden_dir = config[CONFIG_NEW_SRC_DIR] |
| 123 | + deletes, updates = _get_deletes_updates(new_golden_dir, base_golden_dir) |
| 124 | + if len(deletes) + len(updates) != 0: |
| 125 | + prompt = 'The following files will be changed:\n' + \ |
| 126 | + '\n'.join([f' {fname} [delete]' for fname in deletes]) + \ |
| 127 | + '\n'.join([f' {fname} [update]' for fname in updates]) + \ |
| 128 | + '\nIs that ok?' |
| 129 | + if prompt_helper(prompt) == PROMPT_YES: |
| 130 | + config[CONFIG_GOLDENS_DELETES] = deletes |
| 131 | + config[CONFIG_GOLDENS_UPDATES] = updates |
| 132 | + else: |
| 133 | + # We cannot proceed if user answered no. |
| 134 | + exit(1) |
| 135 | + else: |
| 136 | + config[CONFIG_GOLDENS_DELETES] = [] |
| 137 | + config[CONFIG_GOLDENS_UPDATES] = [] |
| 138 | + |
| 139 | + config[CONFIG_AUTO_COMMIT] = \ |
| 140 | + prompt_helper(f'Commit golden repo changes to remote?') == PROMPT_YES |
| 141 | + return config |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + parser = ArgParseImpl() |
| 145 | + parser.add_argument('--branch', help='Branch of the golden repo to write to') |
| 146 | + parser.add_argument('--source', help='Directory containing the new goldens') |
| 147 | + parser.add_argument('--commit-msg', help='Message for the commit to the golden repo') |
| 148 | + parser.add_argument('--golden-repo-token', help='Access token for the golden repo') |
| 149 | + |
| 150 | + args, _ = parser.parse_known_args(sys.argv[1:]) |
| 151 | + config = {} |
| 152 | + golden_manager = GoldenManager( |
| 153 | + os.getcwd(), |
| 154 | + access_type=ACCESS_TYPE_SSH if not args.golden_repo_token else ACCESS_TYPE_TOKEN, |
| 155 | + access_token=args.golden_repo_token |
| 156 | + ) |
| 157 | + base_golden_dir = golden_manager.directory() |
| 158 | + if args.branch and args.source and args.commit_msg: |
| 159 | + assert os.path.exists(args.source), f'{args.source} (--source) directory not found' |
| 160 | + deletes, updates = _get_deletes_updates(args.source, base_golden_dir) |
| 161 | + config = { |
| 162 | + CONFIG_AUTO_COMMIT: True, |
| 163 | + CONFIG_GOLDENS_BRANCH: args.branch, |
| 164 | + CONFIG_NEW_SRC_DIR: args.source, |
| 165 | + CONFIG_GOLDENS_UPDATES: updates, |
| 166 | + CONFIG_GOLDENS_DELETES: deletes, |
| 167 | + CONFIG_COMMIT_MSG: args.commit_msg, |
| 168 | + } |
| 169 | + else: |
| 170 | + config = _interactive_mode(base_golden_dir) |
| 171 | + _do_update(golden_manager, config) |
0 commit comments