From f7fbe297c7504aab3d8248b7f1c0d05ec9cf2642 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Wed, 6 Mar 2019 17:16:40 +0000 Subject: [PATCH] Create the specified temporary directory when Loris starts (#447) * Add a test to show that Loris doesn't create the temp directory * Ensure Loris creates the temp directory on startup * Add a test for directory creation in InfoCache * Remove a missing import from webapp_t.py --- loris/webapp.py | 8 +++++++- tests/img_info_t.py | 17 +++++++++++++++++ tests/webapp_t.py | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/loris/webapp.py b/loris/webapp.py index 990815f0..e7bb4c1c 100755 --- a/loris/webapp.py +++ b/loris/webapp.py @@ -41,7 +41,7 @@ SyntaxException, TransformException, ) - +from loris.utils import mkdir_p getcontext().prec = 25 # Decimal precision. This should be plenty. @@ -328,6 +328,12 @@ def __init__(self, app_configs={}): # make the loris.Loris configs attrs for easier access _loris_config = self.app_configs['loris.Loris'] self.tmp_dp = _loris_config['tmp_dp'] + + try: + mkdir_p(self.tmp_dp) + except Exception as exc: + raise ConfigError("Error creating tmp_dp %s: %r" % (self.tmp_dp, exc)) + self.www_dp = _loris_config['www_dp'] self.enable_caching = _loris_config['enable_caching'] self.redirect_canonical_image_request = _loris_config['redirect_canonical_image_request'] diff --git a/tests/img_info_t.py b/tests/img_info_t.py index 0726e2be..40b63890 100644 --- a/tests/img_info_t.py +++ b/tests/img_info_t.py @@ -581,3 +581,20 @@ def test_looking_up_missing_item_is_keyerror(self): with pytest.raises(KeyError): cache[req] + + def test_creates_cache_dir(self): + root = os.path.join(tempfile.mkdtemp(), "doesnotexist") + assert not os.path.exists(root) + cache = img_info.InfoCache(root=root) + path = self.test_jpeg_fp + req = webapp_t._get_werkzeug_request(path=path) + + info = img_info.ImageInfo( + app=self.app, + ident=self.test_jpeg_uri, + src_img_fp=self.test_jpeg_fp, + src_format=self.test_jpeg_fmt + ) + + cache[req] = info + assert cache[req][0] == info diff --git a/tests/webapp_t.py b/tests/webapp_t.py index 1a81a4ee..23dcca75 100644 --- a/tests/webapp_t.py +++ b/tests/webapp_t.py @@ -19,6 +19,7 @@ from loris.authorizer import NullAuthorizer from loris.loris_exception import ConfigError from loris.transforms import KakaduJP2Transformer, OPJ_JP2Transformer +from loris.webapp import get_debug_config, Loris from tests import loris_t @@ -585,6 +586,21 @@ def test_cleans_up_when_not_caching(self): pass self._assert_tmp_has_no_files() + def test_can_use_tmp_dir_for_transforms(self): + config = get_debug_config('kdu') + config["loris.Loris"]["tmp_dp"] = "/tmp/doesnotexist" + self.build_client_from_config(config) + + assert self.app.tmp_dp == "/tmp/doesnotexist" + + self.client.get("/%s/full/full/0/default.jpg" % self.test_jpeg_id) + + def test_invalid_tmp_dir_is_configerror(self): + config = get_debug_config('kdu') + config["loris.Loris"]["tmp_dp"] = "/dev/null/tmp" + with pytest.raises(ConfigError, match="Error creating tmp_dp /dev/null/tmp:"): + Loris(config) + def _assert_tmp_has_no_files(self): # callback should delete the image before the test ends, so the tmp dir # should not contain any files (there may be dirs)