]> git.treefish.org Git - photosort.git/blob - src/migrator.py
6438342f609b6440b7044cff6e89d2e69f4396ee
[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, db_file=None):
9         self._base_src_dir = src_dir
10         self._base_dst_dir = dst_dir
11         if db_file:
12             self._locator = Locator(dst_dir, src_dir, db_file)
13
14     def migrate(self, remove):
15         for src_file_name, src_file_path in misc.walk_media_files(self._base_src_dir):
16             logging.debug('Migrating %s...', src_file_name)
17             try:
18                 self._migrate_single(src_file_name, src_file_path, remove)
19             except Exception as e:
20                 logging.error('Error migrating %s: %s', src_file_path, str(e))
21
22     def close(self):
23         if self._locator:
24             self._locator.close()
25
26     def _migrate_single(self, src_file_name, src_file_path, remove):
27         meta_time = misc.extract_meta_time(src_file_path)
28
29         if meta_time:
30             dst_sub_dir = datetime.datetime.fromtimestamp(meta_time).strftime("%Y/%m")
31         else:
32             dst_sub_dir = "na"
33
34         dst_dir = os.path.join(self._base_dst_dir, dst_sub_dir)
35         dst_file_path = os.path.join(dst_dir, src_file_name)
36
37         if not os.path.exists(dst_file_path):
38             if self._locator:
39                 alt_dst_dir = self._locator.locate_file(src_file_name,
40                                                         os.path.getsize(src_file_path),
41                                                         meta_time)
42             else:
43                 alt_dst_dir = misc.find_file(self._base_dst_dir,
44                                              src_file_name,
45                                              os.path.getsize(src_file_path),
46                                              meta_time,
47                                              exclude_dir=self._base_src_dir)
48             if alt_dst_dir:
49                 dst_dir = alt_dst_dir
50                 dst_file_path = os.path.join(dst_dir, src_file_name)
51
52         if not os.path.exists(dst_file_path):
53             if not os.path.exists(dst_dir):
54                 os.makedirs(dst_dir)
55             misc.import_file(src_file_path, dst_file_path)
56         else:
57             src_time = os.path.getmtime(src_file_path)
58             dst_time = os.path.getmtime(dst_file_path)
59             if src_time > dst_time:
60                 misc.import_file(src_file_path, dst_file_path)
61
62         if remove:
63             os.remove(src_file_path)
64
65     def cleanup(self):
66         for root, dirs, files in os.walk(self._base_src_dir, topdown=False):
67             for name in files:
68                 full_path = os.path.join(root, name)
69                 if not misc.is_media_file(full_path):
70                     try:
71                         os.remove(full_path)
72                     except Exception as e:
73                         logging.warn('Error cleaning file %s: %s', full_path, str(e))
74             for name in dirs:
75                 full_path = os.path.join(root, name)
76                 try:
77                     os.rmdir(full_path)
78                 except Exception as e:
79                     logging.warn('Error cleaning dir %s: %s', full_path, str(e))