Skip to content

Commit be310bb

Browse files
committed
fix: Revert "switch from lockfile to filelock"
This reverts commit f0cd6b4.
1 parent 3daedfe commit be310bb

File tree

7 files changed

+42
-20
lines changed

7 files changed

+42
-20
lines changed

cachecontrol/caches/file_cache.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,33 @@ def __init__(
6666
forever=False,
6767
filemode=0o0600,
6868
dirmode=0o0700,
69+
use_dir_lock=None,
6970
lock_class=None,
7071
):
7172

73+
if use_dir_lock is not None and lock_class is not None:
74+
raise ValueError("Cannot use use_dir_lock and lock_class together")
75+
7276
try:
73-
if lock_class is None:
74-
from filelock import FileLock
75-
lock_class = FileLock
77+
from lockfile import LockFile
78+
from lockfile.mkdirlockfile import MkdirLockFile
7679
except ImportError:
7780
notice = dedent(
7881
"""
7982
NOTE: In order to use the FileCache you must have
80-
filelock installed. You can install it via pip:
81-
pip install filelock
83+
lockfile installed. You can install it via pip:
84+
pip install lockfile
8285
"""
8386
)
8487
raise ImportError(notice)
8588

89+
else:
90+
if use_dir_lock:
91+
lock_class = MkdirLockFile
92+
93+
elif lock_class is None:
94+
lock_class = LockFile
95+
8696
self.directory = directory
8797
self.forever = forever
8898
self.filemode = filemode
@@ -123,9 +133,9 @@ def _write(self, path, data: bytes):
123133
except (IOError, OSError):
124134
pass
125135

126-
with self.lock_class(path + ".lock"):
136+
with self.lock_class(path) as lock:
127137
# Write our actual file
128-
with _secure_open_write(path, self.filemode) as fh:
138+
with _secure_open_write(lock.path, self.filemode) as fh:
129139
fh.write(data)
130140

131141
def _delete(self, key, suffix):

dev_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mock
1111
cherrypy
1212
sphinx
1313
redis
14-
filelock
14+
lockfile
1515
bumpversion
1616
twine
1717
black

docs/storage.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ FileCache
2828
=========
2929

3030
The `FileCache` is similar to the caching mechanism provided by
31-
httplib2_. It requires `filelock`_ be installed as it prevents
31+
httplib2_. It requires `lockfile`_ be installed as it prevents
3232
multiple threads from writing to the same file at the same time.
3333

3434
.. note::
@@ -64,7 +64,7 @@ This is similar to ``FileCache``, but far more memory efficient, and therefore r
6464

6565
The body of the request is stored in a separate file than metadata, and streamed in and out.
6666

67-
It requires `filelock`_ be installed as it prevents multiple threads from writing to the same file at the same time.
67+
It requires `lockfile`_ be installed as it prevents multiple threads from writing to the same file at the same time.
6868

6969
.. note::
7070

@@ -127,7 +127,7 @@ Third-Party Cache Providers
127127

128128

129129
.. _httplib2: https://github.com/httplib2/httplib2
130-
.. _filelock: https://github.com/tox-dev/py-filelock
130+
.. _lockfile: https://github.com/smontanaro/pylockfile
131131
.. _requests 2.1: http://docs.python-requests.org/en/latest/community/updates/#id2
132132
.. _redis: https://github.com/andymccurdy/redis-py
133133
.. _cachecontrol-django: https://github.com/glassesdirect/cachecontrol-django

docs/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ the provided `FileCache` from CacheControl: ::
6262

6363
from cachecontrol import CacheControl
6464

65-
# NOTE: This requires filelock be installed
65+
# NOTE: This requires lockfile be installed
6666
from cachecontrol.caches import FileCache
6767

6868
sess = CacheControl(requests.Session(),

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
description="httplib2 caching for requests",
2323
long_description=long_description,
2424
install_requires=["requests", "msgpack>=0.5.2"],
25-
extras_require={"filecache": ["filelock>=3.8.0"], "redis": ["redis>=2.10.5"]},
25+
extras_require={"filecache": ["lockfile>=0.9"], "redis": ["redis>=2.10.5"]},
2626
entry_points={"console_scripts": ["doesitcache = cachecontrol._cmd:main"]},
2727
python_requires=">=3.6",
2828
classifiers=[

tests/test_storage_filecache.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import requests
1515
from cachecontrol import CacheControl
1616
from cachecontrol.caches import FileCache, SeparateBodyFileCache
17-
from filelock import FileLock
17+
from lockfile import LockFile
18+
from lockfile.mkdirlockfile import MkdirLockFile
1819

1920

2021
def randomdata():
@@ -93,10 +94,21 @@ def test_key_length(self, sess):
9394
assert len(self.cache.encode(url0)) < 200
9495
assert len(self.cache.encode(url0)) == len(self.cache.encode(url1))
9596

96-
def test_simple_lockfile_arg(self, tmpdir):
97-
cache = self.FileCacheClass(str(tmpdir))
98-
99-
assert issubclass(cache.lock_class, FileLock)
97+
def test_cant_use_dir_and_lock_class(self, tmpdir):
98+
with pytest.raises(ValueError):
99+
self.FileCacheClass(str(tmpdir), use_dir_lock=True, lock_class=object())
100+
101+
@pytest.mark.parametrize(
102+
("value", "expected"),
103+
[(None, LockFile), (True, MkdirLockFile), (False, LockFile)],
104+
)
105+
def test_simple_lockfile_arg(self, tmpdir, value, expected):
106+
if value is not None:
107+
cache = self.FileCacheClass(str(tmpdir), use_dir_lock=value)
108+
else:
109+
cache = self.FileCacheClass(str(tmpdir))
110+
111+
assert issubclass(cache.lock_class, expected)
100112
cache.close()
101113

102114
def test_lock_class(self, tmpdir):
@@ -122,7 +134,7 @@ class TestFileCache(FileCacheTestsMixin):
122134
"""
123135
Tests for ``FileCache``.
124136
"""
125-
137+
126138
FileCacheClass = FileCache
127139

128140
def test_body_stored_inline(self, sess):

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ deps = pytest
1818
mock
1919
cherrypy
2020
redis
21-
filelock
21+
lockfile
2222
commands = py.test {posargs:tests/}

0 commit comments

Comments
 (0)