]> git.treefish.org Git - photosort.git/commitdiff
remember processed files
authorAlexander Schmidt <alex@treefish.org>
Tue, 27 Oct 2020 00:02:55 +0000 (01:02 +0100)
committerAlexander Schmidt <alex@treefish.org>
Tue, 27 Oct 2020 00:02:55 +0000 (01:02 +0100)
doc/photosort-config.json
src/bunch.py
src/migrator.py
src/photosort-daemon.py
src/registry.py [new file with mode: 0644]

index edd4a51581a4b0e5aa77a01c7cdbb378ee52780d..ea47c6194fc488aece3122910f9a6a2e947a5456 100644 (file)
@@ -1,4 +1,5 @@
 {
 {
+    "cache_dir": "/some/path",
     "bunches": [
         {
             "dst_dir": {
     "bunches": [
         {
             "dst_dir": {
index 228131a9e1ec5e6b8157f86813b9fbffceb15634..cfadb2d7b1cca8f7ffb3c7d42aa619c300da4fb5 100644 (file)
@@ -1,4 +1,5 @@
 import logging
 import logging
+import os
 import time
 import threading
 
 import time
 import threading
 
@@ -13,17 +14,18 @@ class Bunch:
             self.migrator = migrator
             self.cleanup = cleanup
 
             self.migrator = migrator
             self.cleanup = cleanup
 
-    def __init__(self, idx, cfg):
+    def __init__(self, idx, cache_dir, cfg):
         self._idx = idx
         source_idx = 1
         self._sources = []
         for src_dir_cfg in cfg['src_dirs']:
             self._sources.append(
         self._idx = idx
         source_idx = 1
         self._sources = []
         for src_dir_cfg in cfg['src_dirs']:
             self._sources.append(
+                reg_db = os.path.join(cache_dir, "reg_%d_%d.db" % (idx, source_idx))
                 Bunch.TriggeredSource(
                     source_idx,
                     DirTrigger(src_dir_cfg['path'], src_dir_cfg['cool_time'],
                                src_dir_cfg['max_time']),
                 Bunch.TriggeredSource(
                     source_idx,
                     DirTrigger(src_dir_cfg['path'], src_dir_cfg['cool_time'],
                                src_dir_cfg['max_time']),
-                    Migrator(src_dir_cfg['path'], cfg['dst_dir']['path']),
+                    Migrator(src_dir_cfg['path'], cfg['dst_dir']['path'], reg_db),
                     src_dir_cfg['cleanup']
                 )
             )
                     src_dir_cfg['cleanup']
                 )
             )
index 7bd32e6d93f5bc187c96c7ee402780645c63233f..b406689346d3da777233da4b60174061f1d6b010 100644 (file)
@@ -5,15 +5,20 @@ import os
 import misc
 
 class Migrator:
 import misc
 
 class Migrator:
-    def __init__(self, src_dir, dst_dir):
+    def __init__(self, src_dir, dst_dir, reg_db=None):
         self._base_src_dir = src_dir
         self._base_dst_dir = dst_dir
         self._base_src_dir = src_dir
         self._base_dst_dir = dst_dir
+        self._reg = Registry(reg_db)
 
     def migrate(self, remove):
         for src_file_name, src_file_path in misc.walk_media_files(self._base_src_dir):
             logging.debug('Migrating %s...', src_file_name)
             try:
 
     def migrate(self, remove):
         for src_file_name, src_file_path in misc.walk_media_files(self._base_src_dir):
             logging.debug('Migrating %s...', src_file_name)
             try:
-                self._migrate_single(src_file_name, src_file_path, remove)
+                if not self._reg.is_registered(src_file_path):
+                    self._migrate_single(src_file_name, src_file_path, remove)
+                    self._reg.register(src_file_path)
+                else:
+                    self._reg.refresh(src_file_path)
             except Exception as e:
                 logging.error('Error migrating %s: %s', src_file_path, str(e))
 
             except Exception as e:
                 logging.error('Error migrating %s: %s', src_file_path, str(e))
 
index e345b837188b3fe5fed584630753c530a3a8bc59..523d8cfedface2fe7719d25f2a558e1031dcfa1b 100755 (executable)
@@ -39,7 +39,7 @@ with open(args.config_file) as f:
 bunch_idx = 1
 bunches = []
 for bunch_cfg in cfg['bunches']:
 bunch_idx = 1
 bunches = []
 for bunch_cfg in cfg['bunches']:
-    bunches.append( Bunch(bunch_idx, bunch_cfg) )
+    bunches.append( Bunch(bunch_idx, cfg['cache_dir'], bunch_cfg) )
     bunch_idx += 1
 
 for bunch in bunches:
     bunch_idx += 1
 
 for bunch in bunches:
diff --git a/src/registry.py b/src/registry.py
new file mode 100644 (file)
index 0000000..3d8cfd3
--- /dev/null
@@ -0,0 +1,47 @@
+import logging
+import os
+import sqlite3
+import time
+
+import misc
+
+class Registry:
+    def __init__(self, db_file):
+        if not db_file: return
+        if not os.path.isfile(db_file):
+            self._conn = sqlite3.connect(db_file, check_same_thread=False)
+            self._create_db()
+        else:
+            self._conn = sqlite3.connect(db_file, check_same_thread=False)
+
+    def is_registered(self, name):
+        if not db_file: return False
+        c = self._conn.cursor()
+        c.execute("SELECT COUNT(*) FROM cache WHERE name=?", (name,))
+        return c.fetchone() != None
+
+    def clean(self):
+        if not db_file: return
+        c = self._conn.cursor()
+        c.execute("DELETE FROM cache WHERE access_time<?", (int(time.time()) - 604800,))
+        self._conn.commit()
+
+
+    def register(self, name):
+        if not db_file: return
+        c = self._conn.cursor()
+        c.execute( '''INSERT INTO cache (name, access_time) VALUES (?, ?)''',
+                   (name, int(time.time())) )
+        self._conn.commit()
+
+    def refresh(self, name):
+        if not db_file: return
+        c = self._conn.cursor()
+        c.execute( "UPDATE cache SET access_time=? WHERE name=?",
+                   (int(time.time()), name) )
+        self._conn.commit()
+
+    def _create_db(self):
+        c = self._conn.cursor()
+        c.execute('''CREATE TABLE cache (name TEXT PRIMARY KEY, access_time INTEGER)''')
+        self._conn.commit()