Lock the cache file
This commit is contained in:
parent
79032c1021
commit
a3a1f8c0d4
18
ianardap.py
18
ianardap.py
@ -12,6 +12,7 @@ import datetime
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import fcntl
|
||||||
|
|
||||||
IANABASE = "https://data.iana.org/rdap/dns.json"
|
IANABASE = "https://data.iana.org/rdap/dns.json"
|
||||||
CACHE = os.environ["HOME"] + "/.ianardapcache.json"
|
CACHE = os.environ["HOME"] + "/.ianardapcache.json"
|
||||||
@ -22,7 +23,9 @@ class IanaRDAPDatabase():
|
|||||||
def __init__(self, maxage=MAXAGE, cachefile=CACHE):
|
def __init__(self, maxage=MAXAGE, cachefile=CACHE):
|
||||||
""" Retrieves the IANA databse, if not already cached. maxage is in hours. """
|
""" Retrieves the IANA databse, if not already cached. maxage is in hours. """
|
||||||
cache_valid = False
|
cache_valid = False
|
||||||
# TODO we should lock it
|
self.cachefile = cachefile
|
||||||
|
self.lockname = self.cachefile + ".lock"
|
||||||
|
self.lock()
|
||||||
if os.path.exists(cachefile) and \
|
if os.path.exists(cachefile) and \
|
||||||
datetime.datetime.fromtimestamp(os.path.getmtime(cachefile)) >= \
|
datetime.datetime.fromtimestamp(os.path.getmtime(cachefile)) >= \
|
||||||
(datetime.datetime.utcnow() - datetime.timedelta(hours = maxage)):
|
(datetime.datetime.utcnow() - datetime.timedelta(hours = maxage)):
|
||||||
@ -30,7 +33,9 @@ class IanaRDAPDatabase():
|
|||||||
content = cache.read()
|
content = cache.read()
|
||||||
cache.close()
|
cache.close()
|
||||||
cache_valid = True
|
cache_valid = True
|
||||||
|
self.unlock()
|
||||||
else:
|
else:
|
||||||
|
self.unlock()
|
||||||
response = requests.get(IANABASE)
|
response = requests.get(IANABASE)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise Exception("Invalid HTTPS return code when trying to get %s: %s" % (IANABASE, response.status_code))
|
raise Exception("Invalid HTTPS return code when trying to get %s: %s" % (IANABASE, response.status_code))
|
||||||
@ -45,11 +50,20 @@ class IanaRDAPDatabase():
|
|||||||
for server in service[1]:
|
for server in service[1]:
|
||||||
self.services[tld] = server
|
self.services[tld] = server
|
||||||
if not cache_valid:
|
if not cache_valid:
|
||||||
# TODO we should lock it
|
self.lock()
|
||||||
cache = open(cachefile, "wb")
|
cache = open(cachefile, "wb")
|
||||||
cache.write(content)
|
cache.write(content)
|
||||||
cache.close()
|
cache.close()
|
||||||
|
self.unlock()
|
||||||
|
|
||||||
|
def lock(self):
|
||||||
|
self.lockhandle = open(self.lockname, 'w')
|
||||||
|
fcntl.lockf(self.lockhandle, fcntl.LOCK_EX)
|
||||||
|
|
||||||
|
def unlock(self):
|
||||||
|
fcntl.lockf(self.lockhandle, fcntl.LOCK_UN)
|
||||||
|
self.lockhandle.close()
|
||||||
|
|
||||||
def find(self, domain):
|
def find(self, domain):
|
||||||
""" Get the RDAP server for a given domain name. None if there is none."""
|
""" Get the RDAP server for a given domain name. None if there is none."""
|
||||||
labels = domain.split(".")
|
labels = domain.split(".")
|
||||||
|
Loading…
Reference in New Issue
Block a user