8 def __init__(self, src_dir, dst_dir):
9 self._base_src_dir = src_dir
10 self._base_dst_dir = dst_dir
12 def migrate(self, remove):
13 for src_file_name, src_file_path in misc.walk_media_files(self._base_src_dir):
14 logging.debug('Migrating %s...', src_file_name)
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))
20 def _migrate_single(self, src_file_name, src_file_path, remove):
21 meta_time = misc.extract_meta_time(src_file_path)
24 dst_sub_dir = datetime.datetime.fromtimestamp(meta_time).strftime("%Y/%m")
28 dst_dir = os.path.join(self._base_dst_dir, dst_sub_dir)
29 dst_file_path = os.path.join(dst_dir, src_file_name)
31 if not os.path.exists(dst_file_path):
32 alt_dst_dir = misc.find_alt_file(self._base_dst_dir,
34 os.path.getsize(src_file_path),
36 exclude_dir=self._base_src_dir)
39 dst_file_path = os.path.join(dst_dir, src_file_name)
41 if not os.path.exists(dst_file_path):
42 if not os.path.exists(dst_dir):
44 misc.import_file(src_file_path, dst_file_path)
46 src_time = os.path.getmtime(src_file_path)
47 dst_time = os.path.getmtime(dst_file_path)
48 if src_time > dst_time:
49 misc.import_file(src_file_path, dst_file_path)
52 os.remove(src_file_path)
55 for root, dirs, files in os.walk(self._base_src_dir, topdown=False):
57 full_path = os.path.join(root, name)
58 if not misc.is_media_file(full_path):
61 except Exception as e:
62 logging.warn('Error cleaning file %s: %s', full_path, str(e))
64 full_path = os.path.join(root, name)
67 except Exception as e:
68 logging.warn('Error cleaning dir %s: %s', full_path, str(e))