1*ac7df209SYuntao Xu#!/usr/bin/env python 2*ac7df209SYuntao Xu# 3*ac7df209SYuntao Xu# Copyright (C) 2012 The Android Open Source Project 4*ac7df209SYuntao Xu# 5*ac7df209SYuntao Xu# Licensed under the Apache License, Version 2.0 (the 'License'); 6*ac7df209SYuntao Xu# you may not use this file except in compliance with the License. 7*ac7df209SYuntao Xu# You may obtain a copy of the License at 8*ac7df209SYuntao Xu# 9*ac7df209SYuntao Xu# http://www.apache.org/licenses/LICENSE-2.0 10*ac7df209SYuntao Xu# 11*ac7df209SYuntao Xu# Unless required by applicable law or agreed to in writing, software 12*ac7df209SYuntao Xu# distributed under the License is distributed on an 'AS IS' BASIS, 13*ac7df209SYuntao Xu# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*ac7df209SYuntao Xu# See the License for the specific language governing permissions and 15*ac7df209SYuntao Xu# limitations under the License. 16*ac7df209SYuntao Xu# 17*ac7df209SYuntao Xu 18*ac7df209SYuntao Xu# copy related utils for all PDK scripts 19*ac7df209SYuntao Xu 20*ac7df209SYuntao Xuimport os, string, sys, shutil, zipfile 21*ac7df209SYuntao Xu 22*ac7df209SYuntao Xudef copy_dir(src_top, dest_top, dir_name, cp_option = ""): 23*ac7df209SYuntao Xu """copy all the files under src_top/dir_name to dest_top/dir_name.""" 24*ac7df209SYuntao Xu src_full_path = src_top + "/" + dir_name 25*ac7df209SYuntao Xu # do not create the leaf dir as cp will create it 26*ac7df209SYuntao Xu [mid_path, leaf_path] = dir_name.rsplit("/", 1) 27*ac7df209SYuntao Xu dest_full_path = dest_top + "/" + mid_path 28*ac7df209SYuntao Xu if not os.path.isdir(dest_full_path): 29*ac7df209SYuntao Xu os.makedirs(dest_full_path) 30*ac7df209SYuntao Xu print "copy dir ", src_full_path, " to ", dest_full_path 31*ac7df209SYuntao Xu os.system("cp -a " + " " + cp_option + " " + src_full_path + " " + dest_full_path) 32*ac7df209SYuntao Xu 33*ac7df209SYuntao Xu 34*ac7df209SYuntao Xudef copy_dir_only_file(src_top, dest_top, dir_name): 35*ac7df209SYuntao Xu """copy only files directly under the given dir_name""" 36*ac7df209SYuntao Xu src_full_path = src_top + "/" + dir_name 37*ac7df209SYuntao Xu dest_full_path = dest_top + "/" + dir_name 38*ac7df209SYuntao Xu if not os.path.isdir(dest_full_path): 39*ac7df209SYuntao Xu os.makedirs(dest_full_path) 40*ac7df209SYuntao Xu children = os.listdir(src_full_path) 41*ac7df209SYuntao Xu for child in children: 42*ac7df209SYuntao Xu child_full_name = src_full_path + "/" + child 43*ac7df209SYuntao Xu if os.path.isfile(child_full_name): 44*ac7df209SYuntao Xu print "copy file ", child_full_name, " to ", dest_full_path 45*ac7df209SYuntao Xu os.system("cp -a " + child_full_name + " " + dest_full_path) 46*ac7df209SYuntao Xu 47*ac7df209SYuntao Xu 48*ac7df209SYuntao Xudef copy_files(src_top, dest_top, files_name): 49*ac7df209SYuntao Xu """copy files from src_top to dest_top. 50*ac7df209SYuntao Xu Note that files_name can include directories which will be created 51*ac7df209SYuntao Xu under dest_top""" 52*ac7df209SYuntao Xu src_full_path = src_top + "/" + files_name 53*ac7df209SYuntao Xu # do not create the leaf dir as cp will create it 54*ac7df209SYuntao Xu [mid_path, leaf_path] = files_name.rsplit("/", 1) 55*ac7df209SYuntao Xu dest_full_path = dest_top + "/" + mid_path 56*ac7df209SYuntao Xu if not os.path.isdir(dest_full_path): 57*ac7df209SYuntao Xu os.makedirs(dest_full_path) 58*ac7df209SYuntao Xu print "copy files ", src_full_path, " to ", dest_full_path 59*ac7df209SYuntao Xu os.system("cp -a " + src_full_path + " " + dest_full_path) 60*ac7df209SYuntao Xu 61*ac7df209SYuntao Xu 62*ac7df209SYuntao Xudef copy_file_if_exists(src_top, dest_top, file_name): 63*ac7df209SYuntao Xu """copy file src_top/file_name to dest_top/file_name 64*ac7df209SYuntao Xu returns false if such file does not exist in source.""" 65*ac7df209SYuntao Xu src_full_name = src_top + "/" + file_name 66*ac7df209SYuntao Xu if not os.path.isfile(src_full_name): 67*ac7df209SYuntao Xu print "file " + src_full_name + " not found" 68*ac7df209SYuntao Xu return False 69*ac7df209SYuntao Xu dest_file = dest_top + "/" + file_name 70*ac7df209SYuntao Xu dest_dir = os.path.dirname(dest_file) 71*ac7df209SYuntao Xu if not os.path.isdir(dest_dir): 72*ac7df209SYuntao Xu os.makedirs(dest_dir) 73*ac7df209SYuntao Xu print "copy file ", src_full_name, " to ", dest_file 74*ac7df209SYuntao Xu os.system("cp -a " + src_full_name + " " + dest_file) 75*ac7df209SYuntao Xu return True 76*ac7df209SYuntao Xu 77*ac7df209SYuntao Xu 78*ac7df209SYuntao Xudef copy_file_new_name_if_exists(src_full_name, dest_dir, dest_file): 79*ac7df209SYuntao Xu """copy src_full_name (including dir + file name) to dest_dir/dest_file 80*ac7df209SYuntao Xu will be used when renaming is necessary""" 81*ac7df209SYuntao Xu if not os.path.isfile(src_full_name): 82*ac7df209SYuntao Xu print "file " + src_full_name + " not found" 83*ac7df209SYuntao Xu return False 84*ac7df209SYuntao Xu dest_full_name = dest_dir + "/" + dest_file 85*ac7df209SYuntao Xu if not os.path.isdir(dest_dir): 86*ac7df209SYuntao Xu os.makedirs(dest_dir) 87*ac7df209SYuntao Xu print "copy file ", src_full_name, " to ", dest_full_name 88*ac7df209SYuntao Xu os.system("cp -a " + src_full_name + " " + dest_full_name) 89*ac7df209SYuntao Xu return True 90*ac7df209SYuntao Xu 91*ac7df209SYuntao Xudef list_files(dir_name, dir_exclusion_filter = ""): 92*ac7df209SYuntao Xu """recursively list all files under given dir_name directory. 93*ac7df209SYuntao Xu exluding subdirs ending with dir_exlusion_filter in name 94*ac7df209SYuntao Xu returns list of files which can be [] if there is no file""" 95*ac7df209SYuntao Xu file_list = [] 96*ac7df209SYuntao Xu if dir_exclusion_filter != "" and dir_name.endswith(dir_exclusion_filter): 97*ac7df209SYuntao Xu return file_list 98*ac7df209SYuntao Xu for item in os.listdir(dir_name): 99*ac7df209SYuntao Xu item_full_path = dir_name + "/" + item 100*ac7df209SYuntao Xu # do not include symbolic link to recursion 101*ac7df209SYuntao Xu if os.path.islink(item_full_path) or os.path.isfile(item_full_path): 102*ac7df209SYuntao Xu file_list.append(item_full_path) 103*ac7df209SYuntao Xu elif os.path.isdir(item_full_path): 104*ac7df209SYuntao Xu result_list = list_files(item_full_path, dir_exclusion_filter) 105*ac7df209SYuntao Xu for file_name in result_list: 106*ac7df209SYuntao Xu file_list.append(file_name) 107*ac7df209SYuntao Xu return file_list 108*ac7df209SYuntao Xu 109*ac7df209SYuntao Xudef src_newer_than_dest(src, dest): 110*ac7df209SYuntao Xu """return True if src file/dir is newer than dest file/dir""" 111*ac7df209SYuntao Xu result = True 112*ac7df209SYuntao Xu src_mod_time = os.path.getmtime(src) 113*ac7df209SYuntao Xu if os.path.isfile(dest) or os.path.isdir(dest): 114*ac7df209SYuntao Xu dest_mod_time = os.path.getmtime(dest) 115*ac7df209SYuntao Xu if dest_mod_time > src_mod_time: 116*ac7df209SYuntao Xu result = False 117*ac7df209SYuntao Xu return result 118*ac7df209SYuntao Xu 119*ac7df209SYuntao Xudef remove_if_exists(entry): 120*ac7df209SYuntao Xu if os.path.exists(entry): 121*ac7df209SYuntao Xu os.system("rm -rf " + entry) 122*ac7df209SYuntao Xu 123*ac7df209SYuntao Xu 124*ac7df209SYuntao Xudef list_files_in_zip(zip_file_path, no_directory = True): 125*ac7df209SYuntao Xu """ list all files/directories inside the given zip_file_path. 126*ac7df209SYuntao Xu Directories are not included if no_directory is True""" 127*ac7df209SYuntao Xu entry_list = [] 128*ac7df209SYuntao Xu if not zipfile.is_zipfile(zip_file_path): 129*ac7df209SYuntao Xu return entry_list 130*ac7df209SYuntao Xu zip_file = zipfile.ZipFile(zip_file_path, 'r') 131*ac7df209SYuntao Xu entries = zip_file.namelist() 132*ac7df209SYuntao Xu for entry in entries: 133*ac7df209SYuntao Xu if not no_directory or not entry.endswith("/"): 134*ac7df209SYuntao Xu entry_list.append(entry) 135*ac7df209SYuntao Xu 136*ac7df209SYuntao Xu #print entry_list 137*ac7df209SYuntao Xu return entry_list 138*ac7df209SYuntao Xu 139*ac7df209SYuntao Xudef save_list(list_to_save, file_name): 140*ac7df209SYuntao Xu f = open(file_name, "w") 141*ac7df209SYuntao Xu for entry in list_to_save: 142*ac7df209SYuntao Xu f.write("%s\n" % entry) 143*ac7df209SYuntao Xu f.close() 144*ac7df209SYuntao Xu 145*ac7df209SYuntao Xudef load_list(file_name): 146*ac7df209SYuntao Xu result = [] 147*ac7df209SYuntao Xu if not os.path.isfile(file_name): 148*ac7df209SYuntao Xu return result 149*ac7df209SYuntao Xu 150*ac7df209SYuntao Xu for line in open(file_name, "r"): 151*ac7df209SYuntao Xu result.append(line.strip()) 152*ac7df209SYuntao Xu 153*ac7df209SYuntao Xu #print result 154*ac7df209SYuntao Xu return result 155*ac7df209SYuntao Xu 156*ac7df209SYuntao Xudef remove_files_listed(top_dir, files_list): 157*ac7df209SYuntao Xu top_dir_ = top_dir + "/" 158*ac7df209SYuntao Xu for entry in files_list: 159*ac7df209SYuntao Xu path = top_dir_ + entry 160*ac7df209SYuntao Xu print "remove " + path 161*ac7df209SYuntao Xu os.system("rm -f " + path) 162*ac7df209SYuntao Xu 163*ac7df209SYuntao Xudef execute_command(command, error_msg): 164*ac7df209SYuntao Xu if os.system(command) != 0: 165*ac7df209SYuntao Xu raise RuntimeError(error_msg) 166