Touch file upon image date - Issue #194

This commit is contained in:
Pablo 'merKur' Kohan
2020-08-17 21:43:57 +03:00
parent eefa1f181f
commit 78d494ff2c
3 changed files with 36 additions and 0 deletions

View File

@@ -1121,6 +1121,11 @@ def query(
help="Hardlink files instead of copying them. " help="Hardlink files instead of copying them. "
"Cannot be used with --exiftool which creates copies of the files with embedded EXIF data.", "Cannot be used with --exiftool which creates copies of the files with embedded EXIF data.",
) )
@click.option(
"--touch-file",
is_flag=True,
help="Sets the file's modification time upon photo date.",
)
@click.option( @click.option(
"--overwrite", "--overwrite",
is_flag=True, is_flag=True,
@@ -1299,6 +1304,7 @@ def export(
update, update,
dry_run, dry_run,
export_as_hardlink, export_as_hardlink,
touch_file,
overwrite, overwrite,
export_by_date, export_by_date,
skip_edited, skip_edited,
@@ -1564,6 +1570,7 @@ def export(
export_db=export_db, export_db=export_db,
fileutil=fileutil, fileutil=fileutil,
dry_run=dry_run, dry_run=dry_run,
touch_file=touch_file,
edited_suffix=edited_suffix, edited_suffix=edited_suffix,
) )
results_exported.extend(results.exported) results_exported.extend(results.exported)
@@ -1601,6 +1608,7 @@ def export(
export_db=export_db, export_db=export_db,
fileutil=fileutil, fileutil=fileutil,
dry_run=dry_run, dry_run=dry_run,
touch_file=touch_file,
edited_suffix=edited_suffix, edited_suffix=edited_suffix,
) )
results_exported.extend(results.exported) results_exported.extend(results.exported)
@@ -2073,6 +2081,7 @@ def export_photo(
export_db=None, export_db=None,
fileutil=FileUtil, fileutil=FileUtil,
dry_run=None, dry_run=None,
touch_file=None,
edited_suffix="_edited", edited_suffix="_edited",
): ):
""" Helper function for export that does the actual export """ Helper function for export that does the actual export
@@ -2101,6 +2110,7 @@ def export_photo(
export_db: export database instance compatible with ExportDB_ABC export_db: export database instance compatible with ExportDB_ABC
fileutil: file util class compatible with FileUtilABC fileutil: file util class compatible with FileUtilABC
dry_run: boolean; if True, doesn't actually export or update any files dry_run: boolean; if True, doesn't actually export or update any files
touch_file: boolean; sets file's modification time upon photo date
Returns: Returns:
list of path(s) of exported photo or None if photo was missing list of path(s) of exported photo or None if photo was missing
@@ -2178,6 +2188,7 @@ def export_photo(
export_db=export_db, export_db=export_db,
fileutil=fileutil, fileutil=fileutil,
dry_run=dry_run, dry_run=dry_run,
touch_file=touch_file,
) )
results_exported.extend(export_results.exported) results_exported.extend(export_results.exported)
@@ -2234,6 +2245,7 @@ def export_photo(
export_db=export_db, export_db=export_db,
fileutil=fileutil, fileutil=fileutil,
dry_run=dry_run, dry_run=dry_run,
touch_file=touch_file,
) )
results_exported.extend(export_results_edited.exported) results_exported.extend(export_results_edited.exported)

View File

@@ -27,6 +27,11 @@ class FileUtilABC(ABC):
def unlink(cls, dest): def unlink(cls, dest):
pass pass
@classmethod
@abstractmethod
def utime(cls, path, times):
pass
@classmethod @classmethod
@abstractmethod @abstractmethod
def cmp_sig(cls, file1, file2): def cmp_sig(cls, file1, file2):
@@ -103,6 +108,11 @@ class FileUtilMacOS(FileUtilABC):
else: else:
os.unlink(filepath) os.unlink(filepath)
@classmethod
def utime(cls, path, times):
""" Set the access and modified time of path. """
os.utime(path, times)
@classmethod @classmethod
def cmp_sig(cls, f1, s2): def cmp_sig(cls, f1, s2):
"""Compare file f1 to signature s2. """Compare file f1 to signature s2.
@@ -172,6 +182,10 @@ class FileUtilNoOp(FileUtil):
def unlink(cls, dest): def unlink(cls, dest):
cls.verbose(f"unlink: {dest}") cls.verbose(f"unlink: {dest}")
@classmethod
def utime(cls, path, times):
cls.verbose(f"utime: {path}, {times}")
@classmethod @classmethod
def file_sig(cls, file1): def file_sig(cls, file1):
cls.verbose(f"file_sig: {file1}") cls.verbose(f"file_sig: {file1}")

View File

@@ -305,6 +305,7 @@ def export2(
export_db=None, export_db=None,
fileutil=FileUtil, fileutil=FileUtil,
dry_run=False, dry_run=False,
touch_file=False,
): ):
""" export photo, like export but with update and dry_run options """ export photo, like export but with update and dry_run options
dest: must be valid destination path or exception raised dest: must be valid destination path or exception raised
@@ -347,6 +348,7 @@ def export2(
for getting/setting data related to exported files to compare update state for getting/setting data related to exported files to compare update state
fileutil: (FileUtilABC); class that conforms to FileUtilABC with various file utilities fileutil: (FileUtilABC); class that conforms to FileUtilABC with various file utilities
dry_run: (boolean, default=False); set to True to run in "dry run" mode dry_run: (boolean, default=False); set to True to run in "dry run" mode
touch_file: (boolean, default=False); if True, sets file's modification time upon photo date
Returns: ExportResults namedtuple with fields: exported, new, updated, skipped Returns: ExportResults namedtuple with fields: exported, new, updated, skipped
where each field is a list of file paths where each field is a list of file paths
@@ -558,6 +560,7 @@ def export2(
no_xattr, no_xattr,
export_as_hardlink, export_as_hardlink,
exiftool, exiftool,
touch_file,
fileutil, fileutil,
) )
exported_files = results.exported exported_files = results.exported
@@ -583,6 +586,7 @@ def export2(
no_xattr, no_xattr,
export_as_hardlink, export_as_hardlink,
exiftool, exiftool,
touch_file,
fileutil, fileutil,
) )
exported_files.extend(results.exported) exported_files.extend(results.exported)
@@ -608,6 +612,7 @@ def export2(
no_xattr, no_xattr,
export_as_hardlink, export_as_hardlink,
exiftool, exiftool,
touch_file,
fileutil, fileutil,
) )
exported_files.extend(results.exported) exported_files.extend(results.exported)
@@ -793,6 +798,7 @@ def _export_photo(
no_xattr, no_xattr,
export_as_hardlink, export_as_hardlink,
exiftool, exiftool,
touch_file,
fileutil=FileUtil, fileutil=FileUtil,
): ):
""" Helper function for export() """ Helper function for export()
@@ -810,6 +816,7 @@ def _export_photo(
no_xattr: don't copy extended attributes no_xattr: don't copy extended attributes
export_as_hardlink: bool export_as_hardlink: bool
exiftool: bool exiftool: bool
touch_file: bool
fileutil: FileUtil class that conforms to fileutil.FileUtilABC fileutil: FileUtil class that conforms to fileutil.FileUtilABC
Returns: Returns:
@@ -865,6 +872,9 @@ def _export_photo(
fileutil.hardlink(src, dest) fileutil.hardlink(src, dest)
else: else:
fileutil.copy(src, dest_str, norsrc=no_xattr) fileutil.copy(src, dest_str, norsrc=no_xattr)
if touch_file:
ts=self.date.timestamp()
fileutil.utime(dest, (ts, ts))
export_db.set_data( export_db.set_data(
dest_str, dest_str,