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