]> git.treefish.org Git - photosort.git/blob - src/migrator.py
configurable trigger times
[photosort.git] / src / migrator.py
1 import datetime
2 import logging
3 import os
4
5 import misc
6
7 class Migrator:
8     def __init__(self, src_dir, dst_dir):
9         self._base_src_dir = src_dir
10         self._base_dst_dir = dst_dir
11
12     def migrate(self, remove):
13         for src_file_name, src_file_path in misc.walk_media_files(self._base_src_dir):
14             logging.info('Migrating %s...', src_file_name)
15             try:
16                 self._migrate_single(src_file_name, src_file_path, remove)
17             except Exception as e:
18                 logging.error('Error migrating %s: %s', src_file_path, str(e))
19
20     def _migrate_single(self, src_file_name, src_file_path, remove):
21         meta_time = misc.extract_timestamp(src_file_path, use_meta=True)
22
23         dst_dir = os.path.join(self._base_dst_dir,
24                                datetime.datetime.fromtimestamp(meta_time).strftime("%Y/%m"))
25         dst_file_path = os.path.join(dst_dir, src_file_name)
26
27         if not os.path.exists(dst_file_path):
28             alt_dst_dir = misc.find_file(self._base_dst_dir,
29                                          src_file_name,
30                                          os.path.getsize(src_file_path),
31                                          exclude_dir=self._base_src_dir)
32             if alt_dst_dir:
33                 dst_dir = alt_dst_dir
34                 dst_file_path = os.path.join(dst_dir, src_file_name)
35
36         if not os.path.exists(dst_file_path):
37             if not os.path.exists(dst_dir):
38                 os.makedirs(dst_dir)
39             misc.import_file(src_file_path, dst_file_path)
40         else:
41             src_time = misc.extract_timestamp(src_file_path)
42             dst_time = misc.extract_timestamp(dst_file_path)
43             if src_time > dst_time:
44                 misc.import_file(src_file_path, dst_file_path)
45
46         if remove:
47             os.remove(src_file_path)
48
49     def cleanup(self):
50         for root, dirs, files in os.walk(self._base_src_dir, topdown=False):
51             for name in files:
52                 full_path = os.path.join(root, name)
53                 if not misc.is_media_file(full_path):
54                     try:
55                         os.remove(full_path)
56                     except Exception as e:
57                         logging.warn('Error cleaning file %s: %s', full_path, str(e))
58             for name in dirs:
59                 full_path = os.path.join(root, name)
60                 try:
61                     os.rmdir(full_path)
62                 except Exception as e:
63                     logging.warn('Error cleaning dir %s: %s', full_path, str(e))