6 from registry import Registry
9 def __init__(self, src_dir, dst_dir, reg_db=None):
10 self._base_src_dir = src_dir
11 self._base_dst_dir = dst_dir
12 self._reg = Registry(reg_db)
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)
18 if not self._reg.is_registered(src_file_path):
19 self._migrate_single(src_file_name, src_file_path, remove)
21 self._reg.register(src_file_path)
23 self._reg.refresh(src_file_path)
24 except Exception as e:
25 logging.error('Error migrating %s: %s', src_file_path, str(e))
27 def _migrate_single(self, src_file_name, src_file_path, remove):
28 meta_time = misc.extract_meta_time(src_file_path)
31 dst_sub_dir = datetime.datetime.fromtimestamp(meta_time).strftime("%Y/%m")
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)
38 if not os.path.exists(dst_file_path):
39 alt_dst_dir = misc.find_alt_file(self._base_dst_dir,
41 os.path.getsize(src_file_path),
43 exclude_dir=self._base_src_dir)
46 dst_file_path = os.path.join(dst_dir, src_file_name)
48 if not os.path.exists(dst_file_path):
49 if not os.path.exists(dst_dir):
51 misc.import_file(src_file_path, dst_file_path)
53 src_time = os.path.getmtime(src_file_path)
54 dst_time = os.path.getmtime(dst_file_path)
55 if src_time > dst_time:
56 misc.import_file(src_file_path, dst_file_path)
59 os.remove(src_file_path)
62 for root, dirs, files in os.walk(self._base_src_dir, topdown=False):
64 full_path = os.path.join(root, name)
65 if not misc.is_media_file(full_path):
68 except Exception as e:
69 logging.warn('Error cleaning file %s: %s', full_path, str(e))
71 full_path = os.path.join(root, name)
74 except Exception as e:
75 logging.warn('Error cleaning dir %s: %s', full_path, str(e))