- Timestamp:
- Oct 4, 2019, 4:50:00 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-259_genericise_backups/tools/backups/utils/generic_utils.py
r40898 r40912 1 import glob 1 2 import os.path as path 2 3 import shutil 3 4 4 5 from distutils.dir_util import copy_tree 6 7 import backups.utils.subprocess_utils as sub_utils 5 8 from backups.utils.errors import ValidationError 6 9 … … 35 38 36 39 40 def _list_checks(sources: [str], targets: [str]): 41 if sources is None or targets is None: 42 raise ValidationError("Error: source for copy cannot be None type") 43 if targets is None: 44 raise ValidationError("Error: copy destination cannot be None type") 45 if type(sources) is not list or len(sources) == 0: 46 raise ValidationError("Error: sources must be given as a list with at least one entry") 47 if type(targets) is not list or len(targets) == 0: 48 raise ValidationError("Error: targets must be given as a list with at least one entry") 49 50 51 def copy_multiple_items_as_one_to_one(source_paths: [str], destination_paths: [str], verbose: bool=False): 52 _list_checks() 53 54 for i in range(0, len(source_paths)): 55 56 if not path.exists(destination_paths[i]): 57 if verbose: 58 print("Copy Warning: target path does not exist (skipping): ", destination_paths[i]) 59 continue 60 if not path.isdir(destination_paths[i]): 61 if verbose: 62 print("Copy Warning: target path is not a directory (skipping): ", destination_paths[i]) 63 continue 64 65 result = copy_to_dir(source_paths[i], destination_paths[i], verbose) 66 if result != 0: 67 return result 68 69 37 70 def copy_multiple_items_to_multiple_dirs(source_paths: [str], destination_paths: [str], verbose: bool=False): 38 71 """ Copies the sources to the destination paths. 39 72 40 73 This is a generic function that just wraps shutil.copy2""" 41 42 if source_paths is None: 43 raise ValidationError("Error: source for copy cannot be None type") 44 if destination_paths is None: 45 raise ValidationError("Error: copy destination cannot be None type") 46 47 if type(source_paths) is not list or len(source_paths) == 0: 48 raise ValidationError("Error: sources must be given as a list with at least one entry") 49 if type(destination_paths) is not list or len(destination_paths) == 0: 50 raise ValidationError("Error: destinations must be given as a list with at least one entry") 74 _list_checks(source_paths, destination_paths) 51 75 52 76 for dpath in destination_paths: … … 66 90 67 91 return 0 92 93 94 def move_dirs(source_dirs: [str], destination_dirs: [str], verbose: bool=False): 95 """ Moves all files from inside source paths to the destination dits. 96 It's important to note that it moves index to index 97 i.e. source[0] -> dest[0]; source[1] -> dest[1] 98 99 This is actually a pretty generic function, should be moved into a utils class. 100 """ 101 102 _list_checks(source_dirs, destination_dirs) 103 104 if len(source_dirs) != len(destination_dirs): 105 raise ValidationError("Error: source and target paths are not equal; aborting file movement") 106 107 for i in range(0, len(source_dirs)): 108 # Identify files in path, move each one: 109 wildcard_search = "{0}/*".format(source_dirs[i]) 110 filepaths = glob.glob(wildcard_search) 111 if len(filepaths) == 0: 112 print("Warning: directory is empty: ", source_dirs[i]) 113 continue 114 try: 115 for fp in filepaths: 116 if verbose: 117 print("Moving: {0} -> {1}".format(fp, destination_dirs[i])) 118 command_and_args = ["mv", fp, destination_dirs[i]] 119 # shutil.move(fp, destination_dirs[i]) 120 sub_utils.simple_unix_wrapper(command_and_args) 121 except IOError as e: 122 print("IOError copying: ", e.filename, e.filename2) 123 return 2 124 125 return 0
Note:
See TracChangeset
for help on using the changeset viewer.
