diff --git a/pillar/application/__init__.py b/pillar/application/__init__.py index 76366fee..1da7d02a 100644 --- a/pillar/application/__init__.py +++ b/pillar/application/__init__.py @@ -1,6 +1,6 @@ import logging import os -import json +import tempfile from bson import ObjectId from datetime import datetime import bugsnag @@ -101,7 +101,14 @@ if from_envvar: # configfile doesn't exist, it should error out (i.e. silent=False). app.config.from_pyfile(from_envvar, silent=False) -# Set the TMP +# Set the TMP environment variable to manage where uploads are stored. +# These are all used by tempfile.mkstemp(), but we don't knwow in whic +# order. As such, we remove all used variables but the one we set. +tempfile.tempdir = app.config['STORAGE_DIR'] +os.environ['TMP'] = app.config['STORAGE_DIR'] +os.environ.pop('TEMP', None) +os.environ.pop('TMPDIR', None) + # Configure logging logging.basicConfig( diff --git a/tests/test_file_storage.py b/tests/test_file_storage.py new file mode 100644 index 00000000..decacd81 --- /dev/null +++ b/tests/test_file_storage.py @@ -0,0 +1,23 @@ +import os +import tempfile + +from common_test_class import AbstractPillarTest + + +class TempDirTest(AbstractPillarTest): + def test_tempfiles_location(self): + # After importing the application, tempfiles should be created in the STORAGE_DIR + storage = self.app.config['STORAGE_DIR'] + self.assertEqual(os.environ['TMP'], storage) + self.assertNotIn('TEMP', os.environ) + self.assertNotIn('TMPDIR', os.environ) + + handle, filename = tempfile.mkstemp() + os.close(handle) + dirname = os.path.dirname(filename) + self.assertEqual(dirname, storage) + + tmpfile = tempfile.NamedTemporaryFile() + dirname = os.path.dirname(tmpfile.name) + self.assertEqual(dirname, storage) +