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