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:
network = db.lookup(address)
except ValueError:
print(_("Invalid IP address: %s") % address, file=sys.stderr)
+ return 2
args = {
"address" : address,
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)
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
# 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:
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):