1*344a7f5eSAndroid Build Coastguard Worker#!/usr/bin/python3 2*344a7f5eSAndroid Build Coastguard Worker 3*344a7f5eSAndroid Build Coastguard Workerimport glob 4*344a7f5eSAndroid Build Coastguard Workerimport os 5*344a7f5eSAndroid Build Coastguard Worker 6*344a7f5eSAndroid Build Coastguard Workerfrom shutil import rmtree, move, copy, copytree 7*344a7f5eSAndroid Build Coastguard Workerfrom sys import stderr 8*344a7f5eSAndroid Build Coastguard Worker 9*344a7f5eSAndroid Build Coastguard Worker 10*344a7f5eSAndroid Build Coastguard Workerdef append(text, more_text): 11*344a7f5eSAndroid Build Coastguard Worker if text: 12*344a7f5eSAndroid Build Coastguard Worker return f'{text}, {more_text}' 13*344a7f5eSAndroid Build Coastguard Worker return more_text 14*344a7f5eSAndroid Build Coastguard Worker 15*344a7f5eSAndroid Build Coastguard Worker 16*344a7f5eSAndroid Build Coastguard Workerdef print_e(*args, **kwargs): 17*344a7f5eSAndroid Build Coastguard Worker print(*args, file=stderr, **kwargs) 18*344a7f5eSAndroid Build Coastguard Worker 19*344a7f5eSAndroid Build Coastguard Worker 20*344a7f5eSAndroid Build Coastguard Workerdef touch(filename, times=None): 21*344a7f5eSAndroid Build Coastguard Worker with open(filename, 'a'): 22*344a7f5eSAndroid Build Coastguard Worker os.utime(filename, times) 23*344a7f5eSAndroid Build Coastguard Worker 24*344a7f5eSAndroid Build Coastguard Worker 25*344a7f5eSAndroid Build Coastguard Workerdef rm(path): 26*344a7f5eSAndroid Build Coastguard Worker """Removes the file or directory tree at the specified path, if it exists. 27*344a7f5eSAndroid Build Coastguard Worker 28*344a7f5eSAndroid Build Coastguard Worker Args: 29*344a7f5eSAndroid Build Coastguard Worker path: Path to remove 30*344a7f5eSAndroid Build Coastguard Worker """ 31*344a7f5eSAndroid Build Coastguard Worker if os.path.isdir(path): 32*344a7f5eSAndroid Build Coastguard Worker rmtree(path) 33*344a7f5eSAndroid Build Coastguard Worker elif os.path.exists(path): 34*344a7f5eSAndroid Build Coastguard Worker os.remove(path) 35*344a7f5eSAndroid Build Coastguard Worker 36*344a7f5eSAndroid Build Coastguard Worker 37*344a7f5eSAndroid Build Coastguard Workerdef mv(src_path, dst_path): 38*344a7f5eSAndroid Build Coastguard Worker """Moves the file or directory tree at the source path to the destination path. 39*344a7f5eSAndroid Build Coastguard Worker 40*344a7f5eSAndroid Build Coastguard Worker This method does not merge directory contents. If the destination is a directory that already 41*344a7f5eSAndroid Build Coastguard Worker exists, it will be removed and replaced by the source. If the destination is rooted at a path 42*344a7f5eSAndroid Build Coastguard Worker that does not exist, it will be created. 43*344a7f5eSAndroid Build Coastguard Worker 44*344a7f5eSAndroid Build Coastguard Worker Args: 45*344a7f5eSAndroid Build Coastguard Worker src_path: Source path 46*344a7f5eSAndroid Build Coastguard Worker dst_path: Destination path 47*344a7f5eSAndroid Build Coastguard Worker """ 48*344a7f5eSAndroid Build Coastguard Worker if os.path.exists(dst_path): 49*344a7f5eSAndroid Build Coastguard Worker rm(dst_path) 50*344a7f5eSAndroid Build Coastguard Worker if not os.path.exists(os.path.dirname(dst_path)): 51*344a7f5eSAndroid Build Coastguard Worker os.makedirs(os.path.dirname(dst_path)) 52*344a7f5eSAndroid Build Coastguard Worker for f in (glob.glob(src_path)): 53*344a7f5eSAndroid Build Coastguard Worker if '*' in dst_path: 54*344a7f5eSAndroid Build Coastguard Worker dst = os.path.join(os.path.dirname(dst_path), os.path.basename(f)) 55*344a7f5eSAndroid Build Coastguard Worker else: 56*344a7f5eSAndroid Build Coastguard Worker dst = dst_path 57*344a7f5eSAndroid Build Coastguard Worker move(f, dst) 58*344a7f5eSAndroid Build Coastguard Worker 59*344a7f5eSAndroid Build Coastguard Worker 60*344a7f5eSAndroid Build Coastguard Workerdef cp(src_path, dst_path): 61*344a7f5eSAndroid Build Coastguard Worker """Copies the file or directory tree at the source path to the destination path. 62*344a7f5eSAndroid Build Coastguard Worker 63*344a7f5eSAndroid Build Coastguard Worker This method does not merge directory contents. If the destination is a directory that already 64*344a7f5eSAndroid Build Coastguard Worker exists, it will be removed and replaced by the source. If the destination is rooted at a path 65*344a7f5eSAndroid Build Coastguard Worker that does not exist, it will be created. 66*344a7f5eSAndroid Build Coastguard Worker 67*344a7f5eSAndroid Build Coastguard Worker Note that the implementation of this method differs from mv, in that it does not handle "*" in 68*344a7f5eSAndroid Build Coastguard Worker the destination path. 69*344a7f5eSAndroid Build Coastguard Worker 70*344a7f5eSAndroid Build Coastguard Worker Args: 71*344a7f5eSAndroid Build Coastguard Worker src_path: Source path 72*344a7f5eSAndroid Build Coastguard Worker dst_path: Destination path 73*344a7f5eSAndroid Build Coastguard Worker """ 74*344a7f5eSAndroid Build Coastguard Worker if os.path.exists(dst_path): 75*344a7f5eSAndroid Build Coastguard Worker rm(dst_path) 76*344a7f5eSAndroid Build Coastguard Worker if not os.path.exists(os.path.dirname(dst_path)): 77*344a7f5eSAndroid Build Coastguard Worker os.makedirs(os.path.dirname(dst_path)) 78*344a7f5eSAndroid Build Coastguard Worker for f in (glob.glob(src_path)): 79*344a7f5eSAndroid Build Coastguard Worker if os.path.isdir(f): 80*344a7f5eSAndroid Build Coastguard Worker copytree(f, dst_path) 81*344a7f5eSAndroid Build Coastguard Worker else: 82*344a7f5eSAndroid Build Coastguard Worker copy(f, dst_path) 83