1# Copyright 2025 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Extracts the zipfile from the cipd instance and commits it to the repo.""" 5 6import argparse 7import os 8import pathlib 9import shutil 10import subprocess 11import sys 12from typing import List 13import zipfile 14 15_SRC_PATH = pathlib.Path(__file__).resolve().parents[2] 16_TO_COMMIT_ZIP_NAME = 'to_commit.zip' 17_CHROMIUM_SRC_PREFIX = 'CHROMIUM_SRC' 18 19 20def _HasChanges(repo): 21 output = subprocess.check_output( 22 ['git', '-C', repo, 'status', '--porcelain=v1']) 23 return bool(output) 24 25 26def main(committed_dir_path): 27 parser = argparse.ArgumentParser( 28 description=__doc__, formatter_class=argparse.RawTextHelpFormatter) 29 parser.add_argument( 30 '--cipd-package-path', required=True, help='Path to cipd package.') 31 parser.add_argument( 32 '--no-git-add', 33 action='store_false', 34 dest='git_add', 35 help='Whether to git add the extracted files.') 36 options = parser.parse_args() 37 38 cipd_package_path = pathlib.Path(options.cipd_package_path) 39 to_commit_zip_path = cipd_package_path / _TO_COMMIT_ZIP_NAME 40 if not to_commit_zip_path.exists(): 41 print(f'No zipfile found at {to_commit_zip_path}', file=sys.stderr) 42 print('Doing nothing', file=sys.stderr) 43 return 44 if os.path.exists(committed_dir_path): 45 # Delete original contents. 46 shutil.rmtree(committed_dir_path) 47 os.makedirs(committed_dir_path) 48 # Replace with the contents of the zip. 49 with zipfile.ZipFile(to_commit_zip_path) as z: 50 z.extractall(committed_dir_path) 51 changed_file_paths: List[str] = [ 52 str(committed_dir_path.relative_to(_SRC_PATH)) 53 ] 54 55 committed_chromium_src_dir = committed_dir_path / _CHROMIUM_SRC_PREFIX 56 for root, _, files in os.walk(committed_chromium_src_dir): 57 for file in files: 58 file_path = os.path.join(root, file) 59 path_relative_to_src = os.path.relpath(file_path, 60 committed_chromium_src_dir) 61 full_src_path = _SRC_PATH / path_relative_to_src 62 if full_src_path.exists(): 63 full_src_path.unlink() 64 shutil.move(file_path, full_src_path) 65 changed_file_paths.append(path_relative_to_src) 66 67 if not _HasChanges(_SRC_PATH): 68 print("No changes found after extracting zip. Did you run this script " 69 "before?") 70 return 71 72 if options.git_add: 73 git_add_cmd = ['git', '-C', _SRC_PATH, 'add'] + changed_file_paths 74 subprocess.check_call(git_add_cmd) 75