]> git.ipfire.org Git - location/libloc.git/blobdiff - src/python/location.in
python: Fix download of database
[location/libloc.git] / src / python / location.in
index c4987a5ccae6672606b60ad2602f770520bf42f6..b30beae0f1a859bad546bef6c27c590c536d6832 100644 (file)
@@ -199,9 +199,14 @@ class CLI(object):
                try:
                        db = location.Database(args.database)
                except FileNotFoundError as e:
-                       sys.stderr.write("location: Could not open database %s: %s\n" \
-                               % (args.database, e))
-                       sys.exit(1)
+                       # Allow continuing without a database
+                       if args.func == self.handle_update:
+                               db = None
+
+                       else:
+                               sys.stderr.write("location: Could not open database %s: %s\n" \
+                                       % (args.database, e))
+                               sys.exit(1)
 
                # Translate family (if present)
                if "family" in args:
@@ -248,6 +253,7 @@ class CLI(object):
                                network = db.lookup(address)
                        except ValueError:
                                print(_("Invalid IP address: %s") % address, file=sys.stderr)
+                               return 2
 
                        args = {
                                "address" : address,
@@ -393,10 +399,7 @@ class CLI(object):
 
        def handle_update(self, db, ns):
                if ns.cron and db:
-                       now = datetime.datetime.utcnow()
-
-                       # Parse the database timestamp
-                       t = datetime.datetime.fromtimestamp(db.created_at)
+                       now = time.time()
 
                        if ns.cron == "daily":
                                delta = datetime.timedelta(days=1)
@@ -405,22 +408,20 @@ class CLI(object):
                        elif ns.cron == "monthly":
                                delta = datetime.timedelta(days=30)
 
+                       delta = delta.total_seconds()
+
                        # Check if the database has recently been updated
-                       if t >= (now - delta):
+                       if db.created_at >= (now - delta):
                                log.info(
-                                       _("The datase has recently be updated recently (%s)") % \
-                                               format_timedelta(now - t),
+                                       _("The database has been updated recently"),
                                )
-                               return
+                               return 3
 
                # Fetch the timestamp we need from DNS
                t = location.discover_latest_version()
 
-               # Parse timestamp into datetime format
-               timestamp = datetime.datetime.fromtimestamp(t) if t else None
-
                # Check the version of the local database
-               if db and timestamp and db.created_at >= timestamp.timestamp():
+               if db and t and db.created_at >= t:
                        log.info("Already on the latest version")
                        return
 
@@ -432,7 +433,7 @@ class CLI(object):
 
                # Try downloading a new database
                try:
-                       t = d.download(public_key=ns.public_key, timestamp=timestamp, tmpdir=tmpdir)
+                       t = d.download(public_key=ns.public_key, timestamp=t, tmpdir=tmpdir)
 
                # If no file could be downloaded, log a message
                except FileNotFoundError as e:
@@ -448,13 +449,7 @@ class CLI(object):
 
                return 0
 
-       def handle_verify(self, ns):
-               try:
-                       db = location.Database(ns.database)
-               except FileNotFoundError as e:
-                       log.error("%s: %s" % (ns.database, e))
-                       return 127
-
+       def handle_verify(self, db, ns):
                # Verify the database
                with open(ns.public_key, "r") as f:
                        if not db.verify(f):