BitMover/_import_files.py

69 lines
2.4 KiB
Python

#!/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 _hashing import xx_hash
def copy_with_progress(s,d):
""" Copy a file with the progress bar """
# size = path.getsize(s)
with open(s, 'rb') as fs:
with open(d, 'wb') as fd:
while True:
chunk = fs.read(4096)
if not chunk:
break
fd.write(chunk)
def copy_from_source(source_path,dest_path,file_name):
""" Copy file from source to destination """
file_exists = path_exists(path.join(dest_path,file_name))
if file_exists is True:
print(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:
print(f'\nFound duplicate for {source_path}/{file_name}, \
renaming destination with hash appended.')
base, extension = path.splitext(file_name)
f_xxhash = xx_hash(path.join(dest_path, file_name))
file_name_hash = base + '_' + f_xxhash + extension
rename(path.join(dest_path, file_name),
path.join(dest_path, file_name_hash))
else:
print(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))
def copy_files(f,config):
""" Copy Files. """
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'])