Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/delete duplicates #301

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion elodie.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
help='Import the file even if it\'s already been imported.')
@click.option('--debug', default=False, is_flag=True,
help='Override the value in constants.py with True.')
@click.option('--delete-duplicates', default=False, is_flag=True,
help='If True, duplicate files will get deleted on import.')
@click.argument('paths', nargs=-1, type=click.Path())
def _import(destination, source, file, album_from_folder, trash, allow_duplicates, debug, paths):
def _import(destination, source, file, album_from_folder, trash, allow_duplicates, debug, delete_duplicates, paths):
"""Import files or directories by reading their EXIF and organizing them accordingly.
"""
constants.debug = debug
constants.delete_duplicates = delete_duplicates
has_errors = False
result = Result()

Expand Down
3 changes: 3 additions & 0 deletions elodie/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#: If True, debug messages will be printed.
debug = False

#: If True, duplicate files will get deleted on import.
delete_duplicates = False

#: Directory in which to store Elodie settings.
application_directory = '{}/.elodie'.format(path.expanduser('~'))

Expand Down
29 changes: 25 additions & 4 deletions elodie/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ def delete_directory_if_empty(self, directory_path):

return False

def delete_file(self, file_path):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add tests for this including for when delete_duplicates is True and False.

"""Delete a file safely but permanently.
"""
try:
if os.path.exists(file_path):
os.remove(file_path)
return True
else:
print("The file does not exist.")
except OSError:
pass

return False

def get_all_files(self, path, extensions=None):
"""Recursively get all files which match a path and extension.

Expand Down Expand Up @@ -508,10 +522,17 @@ def process_file(self, _file, destination, media, **kwargs):
checksum_file = db.get_hash(checksum)
if(allow_duplicate is False and checksum_file is not None):
if(os.path.isfile(checksum_file)):
log.info('%s already exists at %s. Skipping...' % (
_file,
checksum_file
))
if constants.delete_duplicates:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to import constants at the top of the file.
https://travis-ci.org/jmathai/elodie/jobs/488449125

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather include this check in delete_file() so it can be called in a safer manner from other spots if needed.

log.info('%s already exists at %s. Deleting...' % (
_file,
checksum_file
))
self.delete_file(_file)
else:
log.info('%s already exists at %s. Skipping...' % (
_file,
checksum_file
))
return
else:
log.info('%s matched checksum but file not found at %s. Importing again...' % ( # noqa
Expand Down