1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*8975f5c5SAndroid Build Coastguard Worker# 3*8975f5c5SAndroid Build Coastguard Worker# Copyright 2014 The Chromium Authors 4*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 5*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file. 6*8975f5c5SAndroid Build Coastguard Worker 7*8975f5c5SAndroid Build Coastguard Worker"""Copies files to a directory.""" 8*8975f5c5SAndroid Build Coastguard Worker 9*8975f5c5SAndroid Build Coastguard Worker 10*8975f5c5SAndroid Build Coastguard Workerimport filecmp 11*8975f5c5SAndroid Build Coastguard Workerimport itertools 12*8975f5c5SAndroid Build Coastguard Workerimport optparse 13*8975f5c5SAndroid Build Coastguard Workerimport os 14*8975f5c5SAndroid Build Coastguard Workerimport shutil 15*8975f5c5SAndroid Build Coastguard Workerimport sys 16*8975f5c5SAndroid Build Coastguard Worker 17*8975f5c5SAndroid Build Coastguard Workerfrom util import build_utils 18*8975f5c5SAndroid Build Coastguard Workerimport action_helpers # build_utils adds //build to sys.path. 19*8975f5c5SAndroid Build Coastguard Worker 20*8975f5c5SAndroid Build Coastguard Worker 21*8975f5c5SAndroid Build Coastguard Workerdef _get_all_files(base): 22*8975f5c5SAndroid Build Coastguard Worker """Returns a list of all the files in |base|. Each entry is relative to the 23*8975f5c5SAndroid Build Coastguard Worker last path entry of |base|.""" 24*8975f5c5SAndroid Build Coastguard Worker result = [] 25*8975f5c5SAndroid Build Coastguard Worker dirname = os.path.dirname(base) 26*8975f5c5SAndroid Build Coastguard Worker for root, _, files in os.walk(base): 27*8975f5c5SAndroid Build Coastguard Worker result.extend([os.path.join(root[len(dirname):], f) for f in files]) 28*8975f5c5SAndroid Build Coastguard Worker return result 29*8975f5c5SAndroid Build Coastguard Worker 30*8975f5c5SAndroid Build Coastguard Workerdef CopyFile(f, dest, deps): 31*8975f5c5SAndroid Build Coastguard Worker """Copy file or directory and update deps.""" 32*8975f5c5SAndroid Build Coastguard Worker if os.path.isdir(f): 33*8975f5c5SAndroid Build Coastguard Worker shutil.copytree(f, os.path.join(dest, os.path.basename(f))) 34*8975f5c5SAndroid Build Coastguard Worker deps.extend(_get_all_files(f)) 35*8975f5c5SAndroid Build Coastguard Worker else: 36*8975f5c5SAndroid Build Coastguard Worker if os.path.isfile(os.path.join(dest, os.path.basename(f))): 37*8975f5c5SAndroid Build Coastguard Worker dest = os.path.join(dest, os.path.basename(f)) 38*8975f5c5SAndroid Build Coastguard Worker 39*8975f5c5SAndroid Build Coastguard Worker deps.append(f) 40*8975f5c5SAndroid Build Coastguard Worker 41*8975f5c5SAndroid Build Coastguard Worker if os.path.isfile(dest): 42*8975f5c5SAndroid Build Coastguard Worker if filecmp.cmp(dest, f, shallow=False): 43*8975f5c5SAndroid Build Coastguard Worker return 44*8975f5c5SAndroid Build Coastguard Worker # The shutil.copy() below would fail if the file does not have write 45*8975f5c5SAndroid Build Coastguard Worker # permissions. Deleting the file has similar costs to modifying the 46*8975f5c5SAndroid Build Coastguard Worker # permissions. 47*8975f5c5SAndroid Build Coastguard Worker os.unlink(dest) 48*8975f5c5SAndroid Build Coastguard Worker 49*8975f5c5SAndroid Build Coastguard Worker shutil.copy(f, dest) 50*8975f5c5SAndroid Build Coastguard Worker 51*8975f5c5SAndroid Build Coastguard Workerdef DoCopy(options, deps): 52*8975f5c5SAndroid Build Coastguard Worker """Copy files or directories given in options.files and update deps.""" 53*8975f5c5SAndroid Build Coastguard Worker files = list( 54*8975f5c5SAndroid Build Coastguard Worker itertools.chain.from_iterable( 55*8975f5c5SAndroid Build Coastguard Worker action_helpers.parse_gn_list(f) for f in options.files)) 56*8975f5c5SAndroid Build Coastguard Worker 57*8975f5c5SAndroid Build Coastguard Worker for f in files: 58*8975f5c5SAndroid Build Coastguard Worker if os.path.isdir(f) and not options.clear: 59*8975f5c5SAndroid Build Coastguard Worker print('To avoid stale files you must use --clear when copying ' 60*8975f5c5SAndroid Build Coastguard Worker 'directories') 61*8975f5c5SAndroid Build Coastguard Worker sys.exit(-1) 62*8975f5c5SAndroid Build Coastguard Worker CopyFile(f, options.dest, deps) 63*8975f5c5SAndroid Build Coastguard Worker 64*8975f5c5SAndroid Build Coastguard Workerdef DoRenaming(options, deps): 65*8975f5c5SAndroid Build Coastguard Worker """Copy and rename files given in options.renaming_sources and update deps.""" 66*8975f5c5SAndroid Build Coastguard Worker src_files = list( 67*8975f5c5SAndroid Build Coastguard Worker itertools.chain.from_iterable( 68*8975f5c5SAndroid Build Coastguard Worker action_helpers.parse_gn_list(f) for f in options.renaming_sources)) 69*8975f5c5SAndroid Build Coastguard Worker 70*8975f5c5SAndroid Build Coastguard Worker dest_files = list( 71*8975f5c5SAndroid Build Coastguard Worker itertools.chain.from_iterable( 72*8975f5c5SAndroid Build Coastguard Worker action_helpers.parse_gn_list(f) 73*8975f5c5SAndroid Build Coastguard Worker for f in options.renaming_destinations)) 74*8975f5c5SAndroid Build Coastguard Worker 75*8975f5c5SAndroid Build Coastguard Worker if (len(src_files) != len(dest_files)): 76*8975f5c5SAndroid Build Coastguard Worker print('Renaming source and destination files not match.') 77*8975f5c5SAndroid Build Coastguard Worker sys.exit(-1) 78*8975f5c5SAndroid Build Coastguard Worker 79*8975f5c5SAndroid Build Coastguard Worker for src, dest in zip(src_files, dest_files): 80*8975f5c5SAndroid Build Coastguard Worker if os.path.isdir(src): 81*8975f5c5SAndroid Build Coastguard Worker print('renaming diretory is not supported.') 82*8975f5c5SAndroid Build Coastguard Worker sys.exit(-1) 83*8975f5c5SAndroid Build Coastguard Worker else: 84*8975f5c5SAndroid Build Coastguard Worker CopyFile(src, os.path.join(options.dest, dest), deps) 85*8975f5c5SAndroid Build Coastguard Worker 86*8975f5c5SAndroid Build Coastguard Workerdef main(args): 87*8975f5c5SAndroid Build Coastguard Worker args = build_utils.ExpandFileArgs(args) 88*8975f5c5SAndroid Build Coastguard Worker 89*8975f5c5SAndroid Build Coastguard Worker parser = optparse.OptionParser() 90*8975f5c5SAndroid Build Coastguard Worker action_helpers.add_depfile_arg(parser) 91*8975f5c5SAndroid Build Coastguard Worker 92*8975f5c5SAndroid Build Coastguard Worker parser.add_option('--dest', help='Directory to copy files to.') 93*8975f5c5SAndroid Build Coastguard Worker parser.add_option('--files', action='append', 94*8975f5c5SAndroid Build Coastguard Worker help='List of files to copy.') 95*8975f5c5SAndroid Build Coastguard Worker parser.add_option('--clear', action='store_true', 96*8975f5c5SAndroid Build Coastguard Worker help='If set, the destination directory will be deleted ' 97*8975f5c5SAndroid Build Coastguard Worker 'before copying files to it. This is highly recommended to ' 98*8975f5c5SAndroid Build Coastguard Worker 'ensure that no stale files are left in the directory.') 99*8975f5c5SAndroid Build Coastguard Worker parser.add_option('--stamp', help='Path to touch on success.') 100*8975f5c5SAndroid Build Coastguard Worker parser.add_option('--renaming-sources', 101*8975f5c5SAndroid Build Coastguard Worker action='append', 102*8975f5c5SAndroid Build Coastguard Worker help='List of files need to be renamed while being ' 103*8975f5c5SAndroid Build Coastguard Worker 'copied to dest directory') 104*8975f5c5SAndroid Build Coastguard Worker parser.add_option('--renaming-destinations', 105*8975f5c5SAndroid Build Coastguard Worker action='append', 106*8975f5c5SAndroid Build Coastguard Worker help='List of destination file name without path, the ' 107*8975f5c5SAndroid Build Coastguard Worker 'number of elements must match rename-sources.') 108*8975f5c5SAndroid Build Coastguard Worker 109*8975f5c5SAndroid Build Coastguard Worker options, _ = parser.parse_args(args) 110*8975f5c5SAndroid Build Coastguard Worker 111*8975f5c5SAndroid Build Coastguard Worker if options.clear: 112*8975f5c5SAndroid Build Coastguard Worker build_utils.DeleteDirectory(options.dest) 113*8975f5c5SAndroid Build Coastguard Worker build_utils.MakeDirectory(options.dest) 114*8975f5c5SAndroid Build Coastguard Worker 115*8975f5c5SAndroid Build Coastguard Worker deps = [] 116*8975f5c5SAndroid Build Coastguard Worker 117*8975f5c5SAndroid Build Coastguard Worker if options.files: 118*8975f5c5SAndroid Build Coastguard Worker DoCopy(options, deps) 119*8975f5c5SAndroid Build Coastguard Worker 120*8975f5c5SAndroid Build Coastguard Worker if options.renaming_sources: 121*8975f5c5SAndroid Build Coastguard Worker DoRenaming(options, deps) 122*8975f5c5SAndroid Build Coastguard Worker 123*8975f5c5SAndroid Build Coastguard Worker if options.depfile: 124*8975f5c5SAndroid Build Coastguard Worker action_helpers.write_depfile(options.depfile, options.stamp, deps) 125*8975f5c5SAndroid Build Coastguard Worker 126*8975f5c5SAndroid Build Coastguard Worker if options.stamp: 127*8975f5c5SAndroid Build Coastguard Worker build_utils.Touch(options.stamp) 128*8975f5c5SAndroid Build Coastguard Worker 129*8975f5c5SAndroid Build Coastguard Worker 130*8975f5c5SAndroid Build Coastguard Workerif __name__ == '__main__': 131*8975f5c5SAndroid Build Coastguard Worker sys.exit(main(sys.argv[1:])) 132