Merge pull request #141 from jmathai/phifogg-gh-139

Remove `copy2()` and replace with `copy()` and `utime()`
This commit is contained in:
Jaisen Mathai 2016-10-25 20:56:27 -07:00 committed by GitHub
commit df6d80934b
3 changed files with 85 additions and 41 deletions

View File

@ -48,9 +48,6 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates):
print('{"source":"%s", "error_msg":"Not a supported file"}' % _file) print('{"source":"%s", "error_msg":"Not a supported file"}' % _file)
return return
if media.__name__ == 'Video':
FILESYSTEM.set_date_from_path_video(media)
if album_from_folder: if album_from_folder:
media.set_album_from_folder() media.set_album_from_folder()

View File

@ -213,53 +213,42 @@ class FileSystem(object):
shutil.move(_file, dest_path) shutil.move(_file, dest_path)
os.utime(dest_path, (stat.st_atime, stat.st_mtime)) os.utime(dest_path, (stat.st_atime, stat.st_mtime))
else: else:
shutil.copy2(_file, dest_path) # Do not use copy2(), will have an issue when copying to a
# network/mounted drive using copy and manual
# set_date_from_filename gets the job done
shutil.copy(_file, dest_path)
self.set_utime(media)
db.add_hash(checksum, dest_path) db.add_hash(checksum, dest_path)
db.update_hash_db() db.update_hash_db()
return dest_path return dest_path
def set_date_from_path_video(self, video): def set_utime(self, media):
"""Set the modification time on the file based on the file path. """ Set the modification time on the file base on the file name.
Noop if the path doesn't match the format YYYY-MM/DD-IMG_0001.JPG.
:param elodie.media.video.Video video: An instance of Video.
""" """
date_taken = None
video_file_path = video.get_file_path()
# Initialize date taken to what's returned from the metadata function. # Initialize date taken to what's returned from the metadata function.
# If the folder and file name follow a time format of # If the folder and file name follow a time format of
# YYYY-MM/DD-IMG_0001.JPG then we override the date_taken # YYYY-MM-DD_HH-MM-SS-IMG_0001.JPG then we override the date_taken
(year, month, day) = [None] * 3 file_path = media.get_file_path()
directory = os.path.dirname(video_file_path) metadata = media.get_metadata()
# If the directory matches we get back a match with date_taken = metadata['date_taken']
# groups() = (year, month) base_name = metadata['base_name']
year_month_match = re.search('(\d{4})-(\d{2})', directory) year_month_day_match = re.search(
if(year_month_match is not None): '^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})',
(year, month) = year_month_match.groups() base_name
day_match = re.search(
'^(\d{2})',
os.path.basename(video.get_file_path())
) )
if(day_match is not None): if(year_month_day_match is not None):
day = day_match.group(1) (year, month, day, hour, minute, second) = year_month_day_match.groups() # noqa
# check if the file system path indicated a date and if so we
# override the metadata value
if(year is not None and month is not None):
if(day is not None):
date_taken = time.strptime( date_taken = time.strptime(
'{}-{}-{}'.format(year, month, day), '{}-{}-{} {}:{}:{}'.format(year, month, day, hour, minute, second), # noqa
'%Y-%m-%d' '%Y-%m-%d %H:%M:%S'
) )
os.utime(file_path, (time.time(), time.mktime(date_taken)))
else: else:
date_taken = time.strptime( # We don't make any assumptions about time zones and
'{}-{}'.format(year, month), # assume local time zone.
'%Y-%m' date_taken_in_seconds = time.mktime(date_taken)
) os.utime(file_path, (time.time(), (date_taken_in_seconds)))
os.utime(video_file_path, (time.time(), time.mktime(date_taken)))

View File

@ -364,3 +364,61 @@ def test_process_video_with_album_then_title():
assert origin_checksum is not None, origin_checksum assert origin_checksum is not None, origin_checksum
assert origin_checksum != destination_checksum, destination_checksum assert origin_checksum != destination_checksum, destination_checksum
assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination
def test_set_utime_with_exif_date():
filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder()
origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('plain.jpg'), origin)
media_initial = Photo(origin)
metadata_initial = media_initial.get_metadata()
initial_stat = os.stat(origin)
initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
initial_checksum = helper.checksum(origin)
assert initial_time != time.mktime(metadata_initial['date_taken'])
filesystem.set_utime(media_initial)
final_stat = os.stat(origin)
final_checksum = helper.checksum(origin)
media_final = Photo(origin)
metadata_final = media_final.get_metadata()
shutil.rmtree(folder)
assert initial_stat.st_mtime != final_stat.st_mtime
assert final_stat.st_mtime == time.mktime(metadata_final['date_taken'])
assert initial_checksum == final_checksum
def test_set_utime_without_exif_date():
filesystem = FileSystem()
temporary_folder, folder = helper.create_working_folder()
origin = os.path.join(folder,'photo.jpg')
shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
media_initial = Photo(origin)
metadata_initial = media_initial.get_metadata()
initial_stat = os.stat(origin)
initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
initial_checksum = helper.checksum(origin)
assert initial_time == time.mktime(metadata_initial['date_taken'])
filesystem.set_utime(media_initial)
final_stat = os.stat(origin)
final_checksum = helper.checksum(origin)
media_final = Photo(origin)
metadata_final = media_final.get_metadata()
shutil.rmtree(folder)
assert initial_time == final_stat.st_mtime
assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']), (final_stat.st_mtime, time.mktime(metadata_final['date_taken']))
assert initial_checksum == final_checksum