1#!/usr/bin/env python3 2# Copyright 2020 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6""" 7Utility to copy missing files from Chromium tree to Chromium OS libchrome tree 8based on hard coded rules. 9 10This utility is used to diff current HEAD against given commit in Chromium 11browser master branch, copy missing files after hard-coded filter rules and 12remove unnecessary files. libchrome original files in hard-coded filter rules 13will be untounched. 14""" 15 16import argparse 17import os 18import os.path 19import subprocess 20import sys 21 22import filters 23import utils 24 25def main(): 26 # Init args 27 parser = argparse.ArgumentParser( 28 description='Copy file from given commits') 29 parser.add_argument( 30 'commit_hash', 31 metavar='commit', 32 type=str, 33 nargs=1, 34 help='commit hash to copy files from') 35 parser.add_argument( 36 '--dry_run', 37 dest='dry_run', 38 action='store_const', 39 const=True, 40 default=False) 41 arg = parser.parse_args(sys.argv[1:]) 42 43 # Read file list from HEAD and upstream commit. 44 upstream_files = utils.get_file_list(arg.commit_hash[0]) 45 our_files = utils.get_file_list('HEAD') 46 47 # Calculate target file list 48 target_files = filters.filter_file(our_files, upstream_files) 49 50 # Calculate operations needed 51 ops = utils.gen_op(our_files, target_files) 52 53 if arg.dry_run: 54 # Print ops only on dry-run mode. 55 print('\n'.join(repr(x) for x in ops)) 56 return 57 for op, f in ops: 58 # Ignore if op is REP because we only want to copy missing files, not to 59 # revert custom Chromium OS libchrome patch. 60 assert type(op) == utils.DiffOperations 61 if op == utils.DiffOperations.DEL: 62 subprocess.check_call(['git', 'rm', f.path]), 63 elif op == utils.DiffOperations.ADD: 64 # Create directory recursively if not exist. 65 os.makedirs(os.path.dirname(f.path), exist_ok=True) 66 # Read file by git cat-file with blob object id to avoid heavy git checkout. 67 with open(f.path, 'wb') as outfile: 68 subprocess.check_call(['git', 'cat-file', 'blob', f.id], 69 stdout=outfile) 70 # Add to git index 71 subprocess.check_call(['git', 'add', f.path]) 72 73if __name__ == '__main__': 74 main() 75