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