-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from mcveanlab/zero-map-bugfix
Fix race condition in map cache.
- Loading branch information
Showing
9 changed files
with
246 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,3 @@ sphinx-argparse | |
sphinx_rtd_theme | ||
msprime | ||
appdirs | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# Requirements needed when building on readthedocs. | ||
setuptools_scm | ||
appdirs | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Cache handling for downloaded data. | ||
""" | ||
import pathlib | ||
import logging | ||
import os | ||
|
||
import appdirs | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_cache_dir = None | ||
|
||
|
||
def set_cache_dir(cache_dir=None): | ||
""" | ||
The cache_dir is the directory in which stdpopsim stores and checks for | ||
downloaded data. If the specified cache_dir is not None, this value is | ||
converted to a pathlib.Path instance, which is used as the cache directory. | ||
If cache_dir is None (the default), the cache directory is set either from | ||
the environment variable `STDPOPSIM_CACHE` if it exists, or set to the | ||
default location using the :mod:`appdirs` module. | ||
No checks for existance, writability, etc. are performed by this function. | ||
""" | ||
if cache_dir is None: | ||
cache_dir = os.environ.get("STDPOPSIM_CACHE", None) | ||
if cache_dir is None: | ||
cache_dir = appdirs.user_cache_dir("stdpopsim", "popgensims") | ||
global _cache_dir | ||
_cache_dir = pathlib.Path(cache_dir) | ||
logging.info(f"Set cache_dir to {_cache_dir}") | ||
|
||
|
||
def get_cache_dir(): | ||
""" | ||
Returns the directory used to cache material downloaded by stdpopsim as a | ||
pathlib.Path instance. Defaults to a directory 'stdpopsim' in a user cache directory | ||
(e.g., ~/.cache/stdopsim on Unix flavours). See the :func:`.set_cache_dir` function | ||
for how this value can be set. | ||
""" | ||
return _cache_dir | ||
|
||
|
||
set_cache_dir() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
Package definition for tests. Defined to allow cross-importing. | ||
""" | ||
import unittest | ||
import tempfile | ||
|
||
import stdpopsim | ||
|
||
|
||
class CacheReadingTest(unittest.TestCase): | ||
""" | ||
This should be used as the superclass of all tests that use downloaded data. | ||
Rather than using the standard userdir for downloaded data, we use this | ||
local directory that can be easily removed and modified without fear of | ||
interfering with production code. | ||
""" | ||
cache_dir = "test_cache" | ||
saved_cache_dir = None | ||
saved_urls = {} | ||
|
||
def setUp(self): | ||
self.saved_cache_dir = stdpopsim.get_cache_dir() | ||
stdpopsim.set_cache_dir(self.cache_dir) | ||
|
||
def tearDown(self): | ||
stdpopsim.set_cache_dir(self.saved_cache_dir) | ||
|
||
|
||
class CacheWritingTest(unittest.TestCase): | ||
""" | ||
This should be used as the superclass of all tests that alter the | ||
downloaded data cache in any non-standard way. | ||
""" | ||
saved_cache_dir = None | ||
tmp_cache_dir = None | ||
|
||
def setUp(self): | ||
self.saved_cache_dir = stdpopsim.get_cache_dir() | ||
self.tmp_cache_dir = tempfile.TemporaryDirectory() | ||
stdpopsim.set_cache_dir(self.tmp_cache_dir.name) | ||
|
||
def tearDown(self): | ||
stdpopsim.set_cache_dir(self.saved_cache_dir) | ||
del self.tmp_cache_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Tests for the cache management code. | ||
""" | ||
import pathlib | ||
import os | ||
|
||
import appdirs | ||
|
||
import stdpopsim | ||
import tests | ||
|
||
|
||
class TestSetCacheDir(tests.CacheWritingTest): | ||
""" | ||
Tests the set_cache_dir function. | ||
""" | ||
paths = [ | ||
"/somefile", "/some/other/file/", "relative/path", "relative/path/"] | ||
|
||
def test_paths(self): | ||
for test in self.paths: | ||
stdpopsim.set_cache_dir(test) | ||
self.assertEqual(stdpopsim.get_cache_dir(), pathlib.Path(test)) | ||
stdpopsim.set_cache_dir(pathlib.Path(test)) | ||
self.assertEqual(stdpopsim.get_cache_dir(), pathlib.Path(test)) | ||
|
||
def test_none(self): | ||
stdpopsim.set_cache_dir(None) | ||
cache_dir = pathlib.Path(appdirs.user_cache_dir("stdpopsim", "popgensims")) | ||
self.assertEqual(stdpopsim.get_cache_dir(), cache_dir) | ||
|
||
def test_environment_var(self): | ||
try: | ||
for test in self.paths: | ||
os.environ["STDPOPSIM_CACHE"] = test | ||
stdpopsim.set_cache_dir() | ||
self.assertEqual(stdpopsim.get_cache_dir(), pathlib.Path(test)) | ||
finally: | ||
os.environ.pop("STDPOPSIM_CACHE") |
Oops, something went wrong.