IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 24, 2019, 2:41:04 PM (7 years ago)
Author:
fairlamb
Message:

Merge IPP-259 into trunk: Improved backup programs

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tools

  • trunk/tools/backups/utils/generic_utils.py

    r40898 r40967  
     1import glob
    12import os.path as path
    23import shutil
     4from distutils.dir_util import copy_tree
    35
    4 from distutils.dir_util import copy_tree
    5 from backups.utils.errors import ValidationError
     6from .subprocess_utils import simple_unix_wrapper
     7from .errors import ValidationError
    68
    79
     
    3537
    3638
     39def _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
     50def 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
    3769def copy_multiple_items_to_multiple_dirs(source_paths: [str], destination_paths: [str], verbose: bool=False):
    3870    """ Copies the sources to the destination paths.
    3971
    4072This 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)
    5174
    5275    for dpath in destination_paths:
     
    6689
    6790    return 0
     91
     92
     93def move_dirs(source_dirs: [str], destination_dirs: [str], verbose: bool=False):
     94    """ Moves all files from inside source paths to the destination dits.
     95It's important to note that it moves index to index
     96i.e. source[0] -> dest[0]; source[1] -> dest[1]
     97
     98This 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.