Lock while updating

This commit is contained in:
László Károlyi 2019-11-24 16:25:08 +01:00
parent 03eaed584f
commit c4152fbce8
Signed by untrusted user: karolyi
GPG Key ID: 2DCAF25E55735BFE
2 changed files with 23 additions and 11 deletions

View File

@ -11,8 +11,8 @@ blacklist_updater.process(force=False)
class DomainListValidator(object):
'Check the provided email against domain lists.'
domain_whitelist = frozenset()
domain_blacklist = frozenset()
domain_whitelist = set()
domain_blacklist = set('localhost')
def __init__(
self, whitelist: SetOrNone = None, blacklist: SetOrNone = None):
@ -30,8 +30,8 @@ class DomainListValidator(object):
lines = fd.readlines()
except FileNotFoundError:
return
self.domain_blacklist = \
set(x.strip().lower() for x in lines if x.strip())
self.domain_blacklist.update(
x.strip().lower() for x in lines if x.strip())
def __call__(self, user_part: str, domain_part: str) -> bool:
'Do the checking here.'

View File

@ -1,3 +1,4 @@
from fcntl import LOCK_EX, LOCK_UN, flock
from http.client import HTTPResponse
from os import makedirs
from pathlib import Path
@ -9,18 +10,20 @@ from urllib.request import Request, urlopen
BLACKLIST_URL = (
'https://raw.githubusercontent.com/martenson/disposable-email-domains/'
'master/disposable_email_blocklist.conf')
LIB_PATH = Path(__file__).resolve().parent.joinpath('lib')
BLACKLIST_FILE_PATH = LIB_PATH.joinpath('blacklist.txt')
LIB_PATH_DEFAULT = Path(__file__).resolve().parent.joinpath('lib')
BLACKLIST_FILE_PATH = LIB_PATH_DEFAULT.joinpath('blacklist.txt')
class BlacklistUpdater(object):
'Optionally auto-update the built-in `blacklist.txt`.'
_etag_file_path = LIB_PATH.joinpath('blacklist_etag.txt')
_etag_file_path = LIB_PATH_DEFAULT.joinpath('blacklist_etag.txt')
_lock_file_path = LIB_PATH_DEFAULT.joinpath('blacklist_lock')
_refresh_when_older_than = 5 * 24 * 60 * 60 # 5 days
def __init__(self):
makedirs(name=LIB_PATH, exist_ok=True)
def __init__(self, lib_path: str = LIB_PATH_DEFAULT):
makedirs(name=lib_path, exist_ok=True)
self._lock_file_path.touch(exist_ok=True)
def _read_etag(self) -> Optional[str]:
'Read the etag header from the stored etag file when exists.'
@ -62,8 +65,8 @@ class BlacklistUpdater(object):
with open(BLACKLIST_FILE_PATH, 'wb') as fd:
fd.write(response.fp.read())
def process(self, force: bool = False):
'Start optionally updating the blacklist.txt file.'
def _process(self, force: bool = False):
'Start optionally updating the blacklist.txt file, while locked.'
if not force and not self.is_local_old:
return
request = Request(
@ -77,3 +80,12 @@ class BlacklistUpdater(object):
print('not modified')
# Not modified, update date on the etag file
BLACKLIST_FILE_PATH.touch()
def process(self, force: bool = False):
'Start optionally updating the blacklist.txt file.'
with open(self._lock_file_path) as fd:
try:
flock(fd, LOCK_EX)
self._process(force=force)
finally:
flock(fd, LOCK_UN)