Skip to content

Commit

Permalink
Add a function for creating cache dirs according to aboutcode-org#357
Browse files Browse the repository at this point in the history
Signed-off-by: Haiko Schol <[email protected]>
  • Loading branch information
Haiko Schol committed Dec 21, 2017
1 parent 8ab7adb commit cf3efd6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
39 changes: 38 additions & 1 deletion src/commoncode/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def logger_debug(*args):
# DIRECTORIES
#

TMP_DIR_ENV = 'SCANCODE_TMP'


def create_dir(location):
"""
Create directory and all sub-directories recursively at location ensuring these
Expand Down Expand Up @@ -141,7 +144,7 @@ def system_temp_dir():
"""
Return the global temp directory for the current user.
"""
temp_dir = os.getenv('SCANCODE_TMP')
temp_dir = os.getenv(TMP_DIR_ENV)
if not temp_dir:
sc = text.python_safe_name('scancode_' + system.username)
temp_dir = os.path.join(tempfile.gettempdir(), sc)
Expand All @@ -164,6 +167,40 @@ def get_temp_dir(base_dir, prefix=''):
create_dir(base)
return tempfile.mkdtemp(prefix=prefix, dir=base)


def create_cache_dir(dir_name, dev_mode=False):
"""
Create directory `dir_name` and return it as an absolute path.
If `dev_mode` is True, the cache directory will be created in the
root directory of the source tree (e.g. the git repo root). If it
is False, it will be created in the current users home directory.
This location can be overridden by setting the environment variable
SCANCODE_TMP_DIR.
"""
cache_path = build_cache_path(dir_name, dev_mode=dev_mode)
create_dir(cache_path)
return cache_path


def build_cache_path(dir_name, dev_mode=False):
from os.path import abspath, expanduser, dirname, join

cache_root = os.environ.get(TMP_DIR_ENV)

if cache_root and not os.path.exists(cache_root):
raise OSError('{} is set to non-existent directory: {}'.format(TMP_DIR_ENV, cache_root))

if not cache_root:
if dev_mode:
cache_root = dirname(dirname(dirname(abspath(__file__))))
else:
cache_root = expanduser('~')

return join(cache_root, '.cache', 'scancode', dir_name)


#
# FILE READING
#
Expand Down
44 changes: 43 additions & 1 deletion tests/commoncode/test_fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
from __future__ import absolute_import, print_function

import os
import tempfile
from os.path import abspath
from os.path import dirname
from os.path import join
from os.path import sep
from unittest.case import skipIf
from unittest import TestCase
from unittest import skipIf

from commoncode import filetype
from commoncode import fileutils
Expand Down Expand Up @@ -970,3 +974,41 @@ def test_parent_directory_on_path_and_location_10(self):
result = fileutils.parent_directory((os.path.join(test_dir, test_file)))
result = fileutils.as_posixpath(result)
assert result.endswith(expected_name)


class TestBuildCachePath(TestCase):

def setUp(self):
self._old_val = os.environ.get(fileutils.TMP_DIR_ENV)
if self._old_val:
del os.environ[fileutils.TMP_DIR_ENV]

def tearDown(self):
if self._old_val:
os.environ[fileutils.TMP_DIR_ENV] = self._old_val
elif fileutils.TMP_DIR_ENV in os.environ:
del os.environ[fileutils.TMP_DIR_ENV]

def test_in_dev_mode(self):
root_dir = dirname(dirname(dirname(abspath(__file__))))
expected_path = join(root_dir, '.cache', 'scancode', 'testing')
cache_path = fileutils.build_cache_path('testing', dev_mode=True)
assert expected_path == cache_path

def test_not_in_dev_mode(self):
expected_path = join(os.path.expanduser('~'), '.cache', 'scancode', 'testing')
cache_path = fileutils.build_cache_path('testing', dev_mode=False)
assert expected_path == cache_path

def test_env_var_overrides(self):
root_dir = tempfile.gettempdir()
os.environ[fileutils.TMP_DIR_ENV] = root_dir
expected_path = join(root_dir, '.cache', 'scancode', 'testing')
cache_path = fileutils.build_cache_path('testing', dev_mode=True)
assert cache_path == expected_path

def test_raises_on_invalid_env_var(self):
root_dir = '/nosuchdir'
os.environ[fileutils.TMP_DIR_ENV] = root_dir
expected_path = join(root_dir, '.cache', 'scancode', 'testing')
self.assertRaises(OSError, fileutils.build_cache_path, 'testing', dev_mode=True)

0 comments on commit cf3efd6

Please sign in to comment.