optimized logic

This commit is contained in:
Kevin Veen-Birkenbach 2023-11-14 11:44:51 +01:00
parent 7c4010c59e
commit a68530abe0

37
main.py
View File

@ -20,32 +20,35 @@ def find_duplicates(directories):
hashes[file_hash].append(path) hashes[file_hash].append(path)
return {file_hash: paths for file_hash, paths in hashes.items() if len(paths) > 1} return {file_hash: paths for file_hash, paths in hashes.items() if len(paths) > 1}
def handle_modification(files, modification, mode, apply_to): def handle_file_modification(file, modification):
if mode == 'preview':
if modification == 'delete': if modification == 'delete':
print("Would delete the following duplicate files:")
for file in files:
if file.startswith(tuple(apply_to)):
print(file)
elif mode == 'act':
if modification == 'delete':
for file in files:
if file.startswith(tuple(apply_to)):
print(f"Deleting {file}") print(f"Deleting {file}")
os.remove(file) os.remove(file)
elif modification == 'hardlink': elif modification == 'hardlink':
# Implement hardlink logic here # Find the first file that is not the same as `file` to link to
pass original_file = next((f for f in files if f != file), None)
if original_file:
os.remove(file)
os.link(original_file, file)
print(f"Replaced {file} with a hardlink to {original_file}")
elif modification == 'symlink': elif modification == 'symlink':
# Implement symlink logic here original_file = next((f for f in files if f != file), None)
pass if original_file:
elif mode == 'interactive': os.remove(file)
os.symlink(original_file, file)
print(f"Replaced {file} with a symlink to {original_file}")
def handle_modification(files, modification, mode, apply_to):
for file in files: for file in files:
if file.startswith(tuple(apply_to)): if file.startswith(tuple(apply_to)):
if mode == 'preview':
print(f"Would perform {modification} on {file}")
elif mode == 'act':
handle_file_modification(file, modification)
elif mode == 'interactive':
answer = input(f"Do you want to {modification} this file? {file} [y/N] ") answer = input(f"Do you want to {modification} this file? {file} [y/N] ")
if answer.lower() in ['y', 'yes']: if answer.lower() in ['y', 'yes']:
# Implement deletion, hardlink or symlink logic here handle_file_modification(file, modification)
pass
def main(args): def main(args):
directories = args.directories directories = args.directories