Commit 5f111319 authored by Matt Traudt's avatar Matt Traudt
Browse files

Remove dependency on filelock library

parent 0efac1bc
from sbws.globals import (fail_hard, is_initted, time_now, lock_directory)
from sbws.globals import (fail_hard, is_initted, time_now)
from argparse import ArgumentDefaultsHelpFormatter
from datetime import date
from datetime import timedelta
......@@ -7,6 +7,7 @@ import os
import gzip
import shutil
import logging
from sbws.util.filelock import DirectoryLock
log = logging.getLogger(__name__)
......@@ -75,7 +76,7 @@ def _remove_rotten_files(datadir, rotten_days, dry_run=True):
# Hold the lock for basically the entire time just in case someone else
# moves files between when we get the list of files and when we try to
# delete them.
with lock_directory(datadir):
with DirectoryLock(datadir):
fnames = _get_older_files_than(datadir, rotten_days,
['.txt', '.txt.gz'])
for fname in fnames:
......@@ -90,7 +91,7 @@ def _compress_stale_files(datadir, stale_days, dry_run=True):
# Hold the lock for basically the entire time just in case someone else
# moves files between when we get the list of files and when we try to
# compress them.
with lock_directory(datadir):
with DirectoryLock(datadir):
fnames = _get_older_files_than(datadir, stale_days, ['.txt'])
for fname in fnames:
log.info('Compressing %s', fname)
......
import os
import time
import logging
from filelock import FileLock
log = logging.getLogger(__name__)
......@@ -53,22 +52,6 @@ def time_now():
return time.time()
def lock_directory(dname):
'''
Holds a lock on a file in **dname** so that other sbws processes/threads
won't try to read/write while we are reading/writing in this directory.
>>> with lock_directory(dname):
>>> # do things while you have the lock
>>> # no longer have lock
:param str dname: Name of directory we want to obtain a lock for
:retrurns: the FileLock context manager for you to use in a with statement
'''
assert os.path.isdir(dname)
return FileLock(os.path.join(dname, 'lockfile'))
def touch_file(fname, times=None):
'''
If **fname** exists, update its last access and modified times to now. If
......
from sbws.globals import time_now
from sbws.globals import lock_directory
import os
import json
import logging
......@@ -14,6 +13,7 @@ from datetime import timedelta
from enum import Enum
from stem.descriptor.router_status_entry import RouterStatusEntryV3
from sbws import res_proto_ver
from sbws.util.filelock import DirectoryLock
log = logging.getLogger(__name__)
......@@ -42,7 +42,7 @@ def load_result_file(fname, success_only=False):
assert os.path.isfile(fname)
d = []
num_ignored = 0
with lock_directory(os.path.dirname(fname)):
with DirectoryLock(os.path.dirname(fname)):
with open(fname, 'rt') as fd:
for line in fd:
r = Result.from_dict(json.loads(line.strip()))
......@@ -114,7 +114,7 @@ def write_result_to_datadir(result, datadir):
ext = '.txt'
result_fname = os.path.join(
datadir, '{}{}'.format(dt, ext))
with lock_directory(datadir):
with DirectoryLock(datadir):
with open(result_fname, 'at') as fd:
fd.write('{}\n'.format(str(result)))
......
import os
import fcntl
import logging
from sbws.globals import fail_hard
log = logging.getLogger(__name__)
class _FLock:
def __init__(self, lock_fname):
self._lock_fname = lock_fname
self._fd = None
def __enter__(self):
mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
self._fd = os.open(self._lock_fname, mode)
log.debug('Going to lock %s', self._lock_fname)
try:
fcntl.flock(self._fd, fcntl.LOCK_EX)
except OSError as e:
fail_hard('We couldn\'t call flock. Are you on an unsupported '
'platform? Error: %s', e)
log.debug('Received lock %s', self._lock_fname)
def __exit__(self, exc_type, exc_val, exc_tb):
if self._fd is not None:
log.debug('Releasing lock %s', self._lock_fname)
os.close(self._fd)
class DirectoryLock(_FLock):
def __init__(self, dname):
assert os.path.isdir(dname)
lock_fname = os.path.join(dname, '.lockfile')
super().__init__(lock_fname)
class FileLock(_FLock):
def __init__(self, fname):
assert os.path.isdir(fname)
lock_fname = fname + '.lockfile'
super().__init__(lock_fname)
......@@ -73,7 +73,6 @@ setup(
install_requires=[
'stem',
'pysocks',
'filelock',
],
extras_require={
'dev': ['flake8'],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment