Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Fix for #439 so that Cate's config dirs are always existent
Browse files Browse the repository at this point in the history
  • Loading branch information
forman committed Mar 18, 2018
1 parent a222613 commit bf9ed24
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
44 changes: 31 additions & 13 deletions cate/conf/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from typing import Any, Dict, Optional, Sequence, Union

from .defaults import GLOBAL_CONF_FILE, LOCAL_CONF_FILE, LOCATION_FILE, VERSION_CONF_FILE, \
VARIABLE_DISPLAY_SETTINGS, DEFAULT_DATA_PATH, DEFAULT_COLOR_MAP, DEFAULT_RES_PATTERN, \
VARIABLE_DISPLAY_SETTINGS, DEFAULT_DATA_PATH, DEFAULT_VERSION_DATA_PATH, DEFAULT_COLOR_MAP, DEFAULT_RES_PATTERN, \
WEBAPI_USE_WORKSPACE_IMAGERY_CACHE

_CONFIG = None
Expand Down Expand Up @@ -112,23 +112,27 @@ def get_config() -> dict:
"""
global _CONFIG
if _CONFIG is None:
_init_config([os.path.expanduser(GLOBAL_CONF_FILE),
os.path.expanduser(VERSION_CONF_FILE),
os.path.expanduser(LOCAL_CONF_FILE)])
_init_config([GLOBAL_CONF_FILE,
VERSION_CONF_FILE,
LOCAL_CONF_FILE],
[DEFAULT_DATA_PATH,
DEFAULT_VERSION_DATA_PATH])
return _CONFIG


def _init_config(config_files: Sequence[str], template_module: str = 'cate.conf.template') -> None:
def _init_config(config_files: Sequence[str], config_dirs, template_module: str = 'cate.conf.template') -> None:
"""
Initialize the Cate configuration.
:param config_files: A non-empty sequence of configuration files, for example
``["~/.cate/conf.py", "~/.cate/<version>/conf.py", "./cate-conf.py"]``.
Configuration files are read in the given order. If the first file does not exist, it will be created
and its content will be taken from the given *template_module*.
:param config_dirs: list of directories in which a location file will be written.
:param template_module: Qualified name of a Python module that serves as a configuration template file.
If given, this file will be copied into the parent directory of *default_config_file*.
"""
_write_location_files(config_dirs)
new_config = _read_config_files(config_files, template_module=template_module)
global _CONFIG
if new_config is not None:
Expand Down Expand Up @@ -170,14 +174,28 @@ def _read_config_files(config_files: Sequence[str],
new_config = config
else:
new_config.update(config)
# For any configuration read, write a LOCATION_FILE if not yet existent
location_file = os.path.join(os.path.dirname(config_file), LOCATION_FILE)
if config_file == default_config_file or not os.path.exists(location_file):
try:
with open(location_file, 'w') as fp:
fp.write(sys.prefix)
except Exception as error:
print('warning: failed writing %s: %s' % (location_file, str(error)))
return new_config


def _write_location_files(config_dirs: Sequence[str]):
"""
Write a location file into the given directories.
:param config_dirs: Sequence of directories paths.
"""
new_config = None
for config_dir in config_dirs:
if not os.path.isdir(config_dir):
try:
os.makedirs(config_dir, exist_ok=True)
except Exception as error:
print('warning: failed creating %s: %s' % (config_dir, str(error)))
location_file = os.path.join(config_dir, LOCATION_FILE)
try:
with open(location_file, 'w') as fp:
fp.write(sys.prefix)
except Exception as error:
print('warning: failed writing %s: %s' % (location_file, str(error)))
return new_config


Expand Down
3 changes: 3 additions & 0 deletions cate/util/web/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def run_main(name: str,
caller=args_obj.caller,
service_info_file=args_obj.file)

if not os.path.isdir(os.path.dirname(log_file_prefix)):
os.makedirs(os.path.dirname(log_file_prefix), exist_ok=True)

if args_obj.command == 'start':
service = WebAPI()
service.start(name, application_factory,
Expand Down
17 changes: 12 additions & 5 deletions test/conf/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_read_python_config_file(self):
self.assertEqual(config['root_dir'], os.path.join('user', 'home', 'norman'))

def test_read_config_files(self):
test_dir = os.path.join(tempfile.gettempdir(), "cate_test_init_config")
test_dir = os.path.join(tempfile.gettempdir(), "cate_test_read_config_files")
os.mkdir(test_dir)

cate_dir = os.path.join(test_dir, ".cate")
Expand Down Expand Up @@ -80,14 +80,21 @@ def test_read_config_files(self):
self.assertEqual(config.get('b'), 3)
self.assertEqual(config.get('c'), 5)
self.assertEqual(config.get('d'), 6)
finally:
shutil.rmtree(test_dir)

def test_write_location_files(self):
test_dir = os.path.join(tempfile.gettempdir(), "cate_test_write_location_files")
cate_dir = os.path.join(test_dir, ".cate")
version_dir = os.path.join(cate_dir, "2.0.0")

try:
conf._write_location_files([cate_dir, version_dir])
self.assertTrue(os.path.exists(os.path.join(cate_dir, conf.LOCATION_FILE)))
self.assertTrue(os.path.exists(os.path.join(version_dir, conf.LOCATION_FILE)))
self.assertTrue(os.path.exists(os.path.join(test_dir, conf.LOCATION_FILE)))
with open(os.path.join(cate_dir, conf.LOCATION_FILE)) as fp:
self.assertEqual(fp.read(), sys.prefix)
with open(os.path.join(version_dir, conf.LOCATION_FILE)) as fp:
self.assertEqual(fp.read(), sys.prefix)
with open(os.path.join(test_dir, conf.LOCATION_FILE)) as fp:
self.assertEqual(fp.read(), sys.prefix)
finally:
shutil.rmtree(test_dir)
shutil.rmtree(test_dir)

0 comments on commit bf9ed24

Please sign in to comment.