def __init__(self, input_dir, output_dir, **kwargs): # Get keyword arguments. directory = kwargs.get(str('directory'), '') create = kwargs.get(str('create_directory'), False) overwrite = kwargs.get(str('overwrite_existing'), False) # Check required argument. if len(directory) > 0: directory = os.path.expanduser(directory) self.check_target_directory(directory, create) else: sys.exit('No directory passed to copy to') # Process files and directories. for item_name in os.listdir(input_dir): item_path = os.path.join(input_dir, item_name) if os.path.isfile(item_path): copy_file(item_path, os.path.join(directory, item_name), overwrite) elif os.path.isdir(item_path): copy_tree(item_path, os.path.join(directory, item_name), overwrite) # Up to this point our Task has no output. Which means the next Task has no input to work with. # So we're re-using the previous Task's output, by passing it down. pass_input_to_output(input_dir, output_dir)
def pass_input_to_output(input_dir, output_dir): """ Copy all the files and folders in the input_dir to the output_dir without changing them. """ for item_name in os.listdir(input_dir): item_path = os.path.join(input_dir, item_name) if os.path.isfile(item_path): copy_file(item_path, os.path.join(output_dir, item_name), False) elif os.path.isdir(item_path): copy_tree(item_path, os.path.join(output_dir, item_name), False)
def test_copy_file(tmpdir): input_dir = tmpdir.join('0') os.makedirs('%s' % input_dir) shutil.copyfile('%s' % files_dir.join('this_side_up.png'), '%s' % input_dir.join('this_side_up.png')) output_dir = tmpdir.join('1') os.makedirs('%s' % output_dir) copy_file(input_file='%s' % input_dir.join('this_side_up.png'), output_file='%s' % output_dir.join('this_side_up.png')) assert output_dir.join('this_side_up.png').check() is True
def test_copy_file_overwrite_false(tmpdir): input_dir = tmpdir.join('0') os.makedirs('%s' % input_dir) shutil.copyfile('%s' % files_dir.join('this_side_up.png'), '%s' % input_dir.join('this_side_up.png')) output_dir = tmpdir.join('1') os.makedirs('%s' % output_dir) shutil.copyfile('%s' % files_dir.join('this_side_up.png'), '%s' % output_dir.join('this_side_up.png')) with pytest.raises(SystemExit) as exc_info: copy_file(input_file='%s' % input_dir.join('this_side_up.png'), output_file='%s' % output_dir.join('this_side_up.png'), overwrite=False) assert exc_info.type == SystemExit
def match_and_copy(cls, input_path, compiled_patterns, directories, overwrite, create): for n, compiled_pattern in enumerate(compiled_patterns): if re.match(compiled_pattern, input_path): target_name = os.path.basename(input_path) target_path = os.path.join(directories[n], target_name) cls.check_target_directory(directories[n], create) if os.path.isfile(input_path): copy_file(os.path.expanduser(input_path), os.path.expanduser(target_path), overwrite) print('Successfully copied file: %s' % target_name) elif os.path.isdir(input_path): copy_tree(os.path.expanduser(input_path), os.path.expanduser(target_path), overwrite) print('Successfully copied directory: %s' % target_name) break
def __init__(self, input_dir, output_dir, **kwargs): # Get keyword arguments. name_pattern = kwargs.get(str('name_pattern'), '%Y-%m-%d_%H%M%S') # Check required argument. if len(name_pattern) == 0: sys.exit('No name_pattern passed') timestamp_dir = self.create_directory(output_dir, name_pattern) # Process files and directories. for item_name in os.listdir(input_dir): item_path = os.path.join(input_dir, item_name) if os.path.isfile(item_path): copy_file(item_path, os.path.join(timestamp_dir, item_name), False) elif os.path.isdir(item_path): copy_tree(item_path, os.path.join(timestamp_dir, item_name), False)
def __init__(self, input_dir, output_dir, **kwargs): # Get keyword arguments. overwrite = kwargs.get(str('overwrite_existing'), False) fallback_path = kwargs.get(str('fallback_path'), '') original_paths = get_original_paths(output_dir) parent_path = self.get_parent_path(original_paths, fallback_path) # Process files and directories. for item_name in os.listdir(input_dir): item_path = os.path.join(input_dir, item_name) if os.path.isfile(item_path): copy_file(item_path, os.path.join(parent_path, item_name), overwrite) elif os.path.isdir(item_path): copy_tree(item_path, os.path.join(parent_path, item_name), overwrite) # Up to this point our Task has no output. Which means the next Task has no input to work with. # So we're re-using the previous Task's output, by passing it down. pass_input_to_output(input_dir, output_dir)