Bläddra i källkod

implemented status file

bigeagle 11 år sedan
förälder
incheckning
732f27ff8f
3 ändrade filer med 62 tillägg och 4 borttagningar
  1. 2 1
      README.md
  2. 59 2
      tunasync.py
  3. 1 1
      tunasync/jobs.py

+ 2 - 1
README.md

@@ -3,7 +3,8 @@ tunasync
 
 ## TODO
 
-- [ ] status file
+- [x] status file
 - [x] btrfs backend (create snapshot before syncing)
 - [x] add mirror job online
+- [ ] use toml as configuration
 - [ ] debmirror provider

+ 59 - 2
tunasync.py

@@ -2,12 +2,65 @@
 # -*- coding:utf-8 -*-
 import os
 import argparse
+import json
+from datetime import datetime
+
 from tunasync import TUNASync
+from tunasync.hook import JobHook
+
+
+class IndexPageHook(JobHook):
+
+    def __init__(self, parent, dbfile):
+        self.parent = parent
+        self.dbfile = dbfile
+
+        self.mirrors = {}
+        try:
+            with open(self.dbfile) as f:
+                _mirrors = json.load(f)
+                for m in _mirrors:
+                    self.mirrors[m["name"]] = m
+        except:
+            for name, _ in self.parent.mirrors.iteritems():
+                self.mirrors[name] = {
+                    'name': name,
+                    'last_update': '-',
+                    'status': 'unknown',
+                }
+
+    def before_job(self, *args, **kwargs):
+        pass
+
+    def after_job(self, name='unknown', status="unknown", *args, **kwargs):
+        print("Updating tunasync.json")
+        now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+        self.mirrors[name] = {
+            'name': name,
+            'last_update': now,
+            'status': status
+        }
+        with open(self.dbfile, 'wb') as f:
+
+            _mirrors = sorted(
+                [m for _, m in self.mirrors.items()],
+                key=lambda x: x['name']
+            )
+
+            json.dump(_mirrors, f)
+
 
 if __name__ == "__main__":
+    here = os.path.abspath(os.path.dirname(__file__))
+
     parser = argparse.ArgumentParser(prog="tunasync")
-    parser.add_argument("-c", "--config", default="tunasync.ini", help="config file")
-    parser.add_argument("--pidfile", default="/var/run/tunasync.pid", help="pidfile")
+    parser.add_argument("-c", "--config",
+                        default="tunasync.ini", help="config file")
+    parser.add_argument("--dbfile",
+                        default="tunasync.json",
+                        help="mirror status db file")
+    parser.add_argument("--pidfile", default="/var/run/tunasync.pid",
+                        help="pidfile")
 
     args = parser.parse_args()
 
@@ -17,6 +70,10 @@ if __name__ == "__main__":
     tunaSync = TUNASync()
     tunaSync.read_config(args.config)
 
+    index_hook = IndexPageHook(tunaSync, args.dbfile)
+
+    tunaSync.add_hook(index_hook)
+
     tunaSync.run_jobs()
 
 # vim: ts=4 sw=4 sts=4 expandtab

+ 1 - 1
tunasync/jobs.py

@@ -40,7 +40,7 @@ def run_job(sema, child_q, manager_q, provider):
 
         for hook in provider.hooks[::-1]:
             try:
-                hook.after_job(status=status)
+                hook.after_job(name=provider.name, status=status)
             except Exception:
                 import traceback
                 traceback.print_exc()