Refactor/cleanup _export_photo

This commit is contained in:
Pablo 'merKur' Kohan
2020-08-17 21:43:57 +03:00
parent 2bf5fae093
commit eefa1f181f

View File

@@ -797,7 +797,7 @@ def _export_photo(
): ):
""" Helper function for export() """ Helper function for export()
Does the actual copy or hardlink taking the appropriate Does the actual copy or hardlink taking the appropriate
action depending on update, overwrite action depending on update, overwrite, export_as_hardlink
Assumes destination is the right destination (e.g. UUID matches) Assumes destination is the right destination (e.g. UUID matches)
sets UUID and JSON info foo exported file using set_uuid_for_file, set_inf_for_uuido sets UUID and JSON info foo exported file using set_uuid_for_file, set_inf_for_uuido
@@ -824,139 +824,59 @@ def _export_photo(
dest_str = str(dest) dest_str = str(dest)
dest_exists = dest.exists() dest_exists = dest.exists()
if export_as_hardlink: if export_as_hardlink:
# use hardlink instead of copy op_desc = "export_as_hardlink"
if not update:
# not update, do the the hardlink
if overwrite and dest.exists():
# need to remove the destination first
# dest.unlink()
fileutil.unlink(dest)
logging.debug(f"Not update: export_as_hardlink linking file {src} {dest}")
fileutil.hardlink(src, dest)
export_db.set_data(
dest_str,
self.uuid,
fileutil.file_sig(dest_str),
(None, None, None),
self.json(),
None,
)
exported_files.append(dest_str)
elif dest_exists and dest.samefile(src):
# update, hardlink and it already points to the right file, do nothing
logging.debug(
f"Update: skipping samefile with export_as_hardlink {src} {dest}"
)
update_skipped_files.append(dest_str)
elif dest_exists:
# update, not the same file (e.g. user may not have used export_as_hardlink last time it was run
logging.debug(
f"Update: removing existing file prior to export_as_hardlink {src} {dest}"
)
# dest.unlink()
fileutil.unlink(dest)
fileutil.hardlink(src, dest)
export_db.set_data(
dest_str,
self.uuid,
fileutil.file_sig(dest_str),
(None, None, None),
self.json(),
None,
)
update_updated_files.append(dest_str)
exported_files.append(dest_str)
else:
# update, hardlink, destination doesn't exist (new file)
logging.debug(
f"Update: exporting new file with export_as_hardlink {src} {dest}"
)
fileutil.hardlink(src, dest)
export_db.set_data(
dest_str,
self.uuid,
fileutil.file_sig(dest_str),
(None, None, None),
self.json(),
None,
)
exported_files.append(dest_str)
update_new_files.append(dest_str)
else: else:
if not update: op_desc = "export_by_copying"
# not update, do the the copy
if overwrite and dest.exists(): if not update:
# need to remove the destination first # not update, do the the hardlink
# dest.unlink() logging.debug(f"Not update: {op_desc} linking file {src} {dest}")
fileutil.unlink(dest) exported_files.append(dest_str)
logging.debug(f"Not update: copying file {src} {dest}") else: #updating
fileutil.copy(src, dest_str, norsrc=no_xattr) if not dest_exists:
exported_files.append(dest_str) # update, destination doesn't exist (new file)
export_db.set_data( logging.debug(f"Update: exporting new file with {op_desc} {src} {dest}")
dest_str, update_new_files.append(dest_str)
self.uuid, else:
fileutil.file_sig(dest_str), # update, destination exists, but we might not need to replace it...
(None, None, None), if (( export_as_hardlink and dest.samefile(src)) or
self.json(), (not export_as_hardlink and not dest.samefile(src) and (
None, ( exiftool and fileutil.cmp_sig(dest_str, export_db.get_stat_exif_for_file(dest_str))) or
) (not exiftool and filecmp.cmp(src, dest)))
# elif dest_exists and not exiftool and cmp_file(dest_str, export_db.get_stat_orig_for_file(dest_str)): )):
elif ( # destination exists but its signature is "identical"
dest_exists logging.debug(f"Update: skipping identical original files {src} {dest}")
and not exiftool # call set_stat because code can reach this spot if no export DB but exporting a RAW or live photo
and filecmp.cmp(src, dest) # potentially re-writes the data in the database but ensures database is complete
and not dest.samefile(src) export_db.set_stat_orig_for_file(dest_str, fileutil.file_sig(dest_str))
): update_skipped_files.append(dest_str)
# destination exists but is identical else:
logging.debug(f"Update: skipping identifical original files {src} {dest}") # destination exists but is different
# call set_stat because code can reach this spot if no export DB but exporting a RAW or live photo logging.debug(f"Update: removing existing file prior to {op_desc} {src} {dest}")
# potentially re-writes the data in the database but ensures database is complete update_updated_files.append(dest_str)
export_db.set_stat_orig_for_file(dest_str, fileutil.file_sig(dest_str))
update_skipped_files.append(dest_str) if not update_skipped_files:
elif ( if dest_exists and (update or overwrite):
dest_exists # need to remove the destination first
and exiftool logging.debug(f"Update: removing existing file prior to export_as_hardlink {src} {dest}")
and fileutil.cmp_sig(dest_str, export_db.get_stat_exif_for_file(dest_str))
and not dest.samefile(src)
):
# destination exists but is identical
logging.debug(f"Update: skipping identifical exiftool files {src} {dest}")
update_skipped_files.append(dest_str)
elif dest_exists:
# destination exists but is different or is a hardlink
logging.debug(f"Update: removing existing file prior to copy {src} {dest}")
stat_src = os.stat(src)
stat_dest = os.stat(dest)
# dest.unlink() # dest.unlink()
fileutil.unlink(dest) fileutil.unlink(dest)
fileutil.copy(src, dest_str, norsrc=no_xattr) if export_as_hardlink:
export_db.set_data( fileutil.hardlink(src, dest)
dest_str,
self.uuid,
fileutil.file_sig(dest_str),
(None, None, None),
self.json(),
None,
)
exported_files.append(dest_str)
update_updated_files.append(dest_str)
else: else:
# destination doesn't exist, copy the file
logging.debug(f"Update: copying new file {src} {dest}")
fileutil.copy(src, dest_str, norsrc=no_xattr) fileutil.copy(src, dest_str, norsrc=no_xattr)
export_db.set_data(
dest_str, export_db.set_data(
self.uuid, dest_str,
fileutil.file_sig(dest_str), self.uuid,
(None, None, None), fileutil.file_sig(dest_str),
self.json(), (None, None, None),
None, self.json(),
) None,
exported_files.append(dest_str) )
update_new_files.append(dest_str)
return ExportResults( return ExportResults(
exported_files, update_new_files, update_updated_files, update_skipped_files, [] exported_files + update_new_files + update_updated_files, update_new_files, update_updated_files, update_skipped_files, []
) )