1# Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights 2# reserved. Use of this source code is governed by a BSD-style license that 3# can be found in the LICENSE file. 4 5from __future__ import absolute_import 6from glob import iglob 7from io import open 8import os 9import shutil 10import sys 11import time 12 13 14def read_file(name, normalize=True): 15 """ Read a file. """ 16 try: 17 with open(name, 'r', encoding='utf-8') as f: 18 # read the data 19 data = f.read() 20 if normalize: 21 # normalize line endings 22 data = data.replace("\r\n", "\n") 23 return data 24 except IOError as e: 25 (errno, strerror) = e.args 26 sys.stderr.write('Failed to read file ' + name + ': ' + strerror) 27 raise 28 29 30def write_file(name, data): 31 """ Write a file. """ 32 try: 33 with open(name, 'w', encoding='utf-8') as f: 34 # write the data 35 if sys.version_info.major == 2: 36 f.write(data.decode('utf-8')) 37 else: 38 f.write(data) 39 except IOError as e: 40 (errno, strerror) = e.args 41 sys.stderr.write('Failed to write file ' + name + ': ' + strerror) 42 raise 43 44 45def path_exists(name): 46 """ Returns true if the path currently exists. """ 47 return os.path.exists(name) 48 49 50def write_file_if_changed(name, data): 51 """ Write a file if the contents have changed. Returns True if the file was written. """ 52 if path_exists(name): 53 old_contents = read_file(name) 54 else: 55 old_contents = '' 56 57 if (data != old_contents): 58 write_file(name, data) 59 return True 60 return False 61 62 63def backup_file(name): 64 """ Rename the file to a name that includes the current time stamp. """ 65 move_file(name, name + '.' + time.strftime('%Y-%m-%d-%H-%M-%S')) 66 67 68def copy_file(src, dst, quiet=True): 69 """ Copy a file. """ 70 try: 71 shutil.copy2(src, dst) 72 if not quiet: 73 sys.stdout.write('Transferring ' + src + ' file.\n') 74 except IOError as e: 75 (errno, strerror) = e.args 76 sys.stderr.write('Failed to copy file from ' + src + ' to ' + dst + ': ' + 77 strerror) 78 raise 79 80 81def move_file(src, dst, quiet=True): 82 """ Move a file. """ 83 try: 84 shutil.move(src, dst) 85 if not quiet: 86 sys.stdout.write('Moving ' + src + ' file.\n') 87 except IOError as e: 88 (errno, strerror) = e.args 89 sys.stderr.write('Failed to move file from ' + src + ' to ' + dst + ': ' + 90 strerror) 91 raise 92 93 94def copy_files(src_glob, dst_folder, quiet=True): 95 """ Copy multiple files. """ 96 for fname in iglob(src_glob): 97 dst = os.path.join(dst_folder, os.path.basename(fname)) 98 if os.path.isdir(fname): 99 copy_dir(fname, dst, quiet) 100 else: 101 copy_file(fname, dst, quiet) 102 103 104def remove_file(name, quiet=True): 105 """ Remove the specified file. """ 106 try: 107 if path_exists(name): 108 os.remove(name) 109 if not quiet: 110 sys.stdout.write('Removing ' + name + ' file.\n') 111 except IOError as e: 112 (errno, strerror) = e.args 113 sys.stderr.write('Failed to remove file ' + name + ': ' + strerror) 114 raise 115 116 117def copy_dir(src, dst, quiet=True): 118 """ Copy a directory tree. """ 119 try: 120 remove_dir(dst, quiet) 121 shutil.copytree(src, dst) 122 if not quiet: 123 sys.stdout.write('Transferring ' + src + ' directory.\n') 124 except IOError as e: 125 (errno, strerror) = e.args 126 sys.stderr.write('Failed to copy directory from ' + src + ' to ' + dst + 127 ': ' + strerror) 128 raise 129 130 131def remove_dir(name, quiet=True): 132 """ Remove the specified directory. """ 133 try: 134 if path_exists(name): 135 shutil.rmtree(name) 136 if not quiet: 137 sys.stdout.write('Removing ' + name + ' directory.\n') 138 except IOError as e: 139 (errno, strerror) = e.args 140 sys.stderr.write('Failed to remove directory ' + name + ': ' + strerror) 141 raise 142 143 144def make_dir(name, quiet=True): 145 """ Create the specified directory. """ 146 try: 147 if not path_exists(name): 148 if not quiet: 149 sys.stdout.write('Creating ' + name + ' directory.\n') 150 os.makedirs(name) 151 except IOError as e: 152 (errno, strerror) = e.args 153 sys.stderr.write('Failed to create directory ' + name + ': ' + strerror) 154 raise 155 156 157def get_files(search_glob): 158 """ Returns all files matching the search glob. """ 159 # Sort the result for consistency across platforms. 160 return sorted(iglob(search_glob)) 161 162 163def read_version_file(file, args): 164 """ Read and parse a version file (key=value pairs, one per line). """ 165 lines = read_file(file).split("\n") 166 for line in lines: 167 parts = line.split('=', 1) 168 if len(parts) == 2: 169 args[parts[0]] = parts[1] 170 171 172def eval_file(src): 173 """ Loads and evaluates the contents of the specified file. """ 174 return eval(read_file(src), {'__builtins__': None}, None) 175 176 177def normalize_path(path): 178 """ Normalizes the path separator to match the Unix standard. """ 179 if sys.platform == 'win32': 180 return path.replace('\\', '/') 181 return path 182