#!/usr/bin/env python """ Functions to copy bits around ... """ from os import system,path,rename from tqdm import tqdm from file_stuff import create_folder, cmp_files, path_exists from lumberjack import timber from hashing import xx_hash # from configure import Configure, CONFIG_FILE # c = Configure(CONFIG_FILE) # config = c.load_config() log = timber(__name__) def copy_with_progress(s,d,f): """ Copy a file with the progress bar """ log.debug(f'copy_with_progress({s},{d},{f})') size = path.getsize(s) with open(s, 'rb') as fs: with open(d, 'wb') as fd: with tqdm(total=size, unit='B', unit_scale=True, desc=f'Copying {f}') as pbar: while True: chunk = fs.read(4096) if not chunk: break fd.write(chunk) pbar.update(len(chunk)) def copy_from_source(source_path,dest_path,file_name): """ Copy file from source to destination """ log.debug(f'copy_from_source({source_path},{dest_path},{file_name}') file_exists = path_exists(path.join(dest_path,file_name)) if file_exists is True: log.debug(f'\nFound {file_name} at destination, checking if they match.') check_match = cmp_files( path.join(source_path,file_name), path.join(dest_path, file_name)) if check_match is False: log.warn(f'\nFound duplicate for {source_path}/{file_name}, \ renaming destination with hash appended.') base, extension = path.splitext(file_name) #md5 = md5_hash(os.path.join(dest_path, file_name)) f_xxhash = xx_hash(path.join(dest_path, file_name)) #file_name_hash = base + '_' + md5 + extension file_name_hash = base + '_' + f_xxhash + extension rename(path.join(dest_path, file_name), path.join(dest_path, file_name_hash)) else: log.info(f'\n{file_name} hashes match') # remove(path.join(source_path,file_name)) # f.pop(file_name) return # create_folder(dest_path) # shutil.copy(os.path.join(source_path,file_name), dest_path) copy_with_progress(path.join(source_path, file_name), path.join(dest_path, file_name), file_name) system('clear') def copy_files(f,config): """ Copy Files. """ log.debug(f'copy_files({f})') system('clear') for file in tqdm(f, desc="Copying Files:"): create_folder(f[file]['folders']['destination']) copy_from_source(f[file]['folders']['source_path'], f[file]['folders']['destination'], f[file]['name']) if config['store_originals'] is True: if f[file]['type'] == 'image': create_folder(f[file]['folders']['destination_original']) copy_from_source(f[file]['folders']['destination'], f[file]['folders']['destination_original'], f[file]['name'])