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