diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3bcd486ef1..7a88a2ab99 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -125,7 +125,6 @@ repos: src/aiida/engine/processes/ports.py| src/aiida/manage/configuration/__init__.py| src/aiida/manage/configuration/config.py| - src/aiida/manage/configuration/profile.py| src/aiida/manage/external/rmq/launcher.py| src/aiida/manage/tests/main.py| src/aiida/manage/tests/pytest_fixtures.py| diff --git a/src/aiida/cmdline/commands/cmd_presto.py b/src/aiida/cmdline/commands/cmd_presto.py index 09d7070e7c..eeb98fad75 100644 --- a/src/aiida/cmdline/commands/cmd_presto.py +++ b/src/aiida/cmdline/commands/cmd_presto.py @@ -67,7 +67,7 @@ def detect_postgres_config( """ import secrets - from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER + from aiida.manage.configuration.settings import AiiDAConfigDir from aiida.manage.external.postgres import Postgres dbinfo = { @@ -92,13 +92,15 @@ def detect_postgres_config( except Exception as exception: raise ConnectionError(f'Unable to automatically create the PostgreSQL user and database: {exception}') + aiida_config_folder = AiiDAConfigDir.get() + return { 'database_hostname': postgres_hostname, 'database_port': postgres_port, 'database_name': database_name, 'database_username': database_username, 'database_password': database_password, - 'repository_uri': f'file://{AIIDA_CONFIG_FOLDER / "repository" / profile_name}', + 'repository_uri': f'file://{aiida_config_folder / "repository" / profile_name}', } diff --git a/src/aiida/cmdline/commands/cmd_profile.py b/src/aiida/cmdline/commands/cmd_profile.py index 3dd21b56bf..7cb0e018ae 100644 --- a/src/aiida/cmdline/commands/cmd_profile.py +++ b/src/aiida/cmdline/commands/cmd_profile.py @@ -169,9 +169,9 @@ def profile_list(): # This can happen for a fresh install and the `verdi setup` has not yet been run. In this case it is still nice # to be able to see the configuration directory, for instance for those who have set `AIIDA_PATH`. This way # they can at least verify that it is correctly set. - from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER + from aiida.manage.configuration.settings import AiiDAConfigDir - echo.echo_report(f'configuration folder: {AIIDA_CONFIG_FOLDER}') + echo.echo_report(f'configuration folder: {AiiDAConfigDir.get()}') echo.echo_critical(str(exception)) else: echo.echo_report(f'configuration folder: {config.dirpath}') diff --git a/src/aiida/cmdline/commands/cmd_status.py b/src/aiida/cmdline/commands/cmd_status.py index f3c32327dc..85ef292fa7 100644 --- a/src/aiida/cmdline/commands/cmd_status.py +++ b/src/aiida/cmdline/commands/cmd_status.py @@ -61,13 +61,14 @@ def verdi_status(print_traceback, no_rmq): from aiida.common.docs import URL_NO_BROKER from aiida.common.exceptions import ConfigurationError from aiida.engine.daemon.client import DaemonException, DaemonNotRunningException - from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER + from aiida.manage.configuration.settings import AiiDAConfigDir from aiida.manage.manager import get_manager exit_code = ExitCode.SUCCESS + configure_directory = AiiDAConfigDir.get() print_status(ServiceStatus.UP, 'version', f'AiiDA v{__version__}') - print_status(ServiceStatus.UP, 'config', AIIDA_CONFIG_FOLDER) + print_status(ServiceStatus.UP, 'config', configure_directory) manager = get_manager() diff --git a/src/aiida/cmdline/params/options/commands/setup.py b/src/aiida/cmdline/params/options/commands/setup.py index 930aa97018..49cfc1e121 100644 --- a/src/aiida/cmdline/params/options/commands/setup.py +++ b/src/aiida/cmdline/params/options/commands/setup.py @@ -66,11 +66,12 @@ def get_repository_uri_default(ctx): """ import os - from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER + from aiida.manage.configuration.settings import AiiDAConfigDir validate_profile_parameter(ctx) + configure_directory = AiiDAConfigDir.get() - return os.path.join(AIIDA_CONFIG_FOLDER, 'repository', ctx.params['profile'].name) + return os.path.join(configure_directory, 'repository', ctx.params['profile'].name) def get_quicksetup_repository_uri(ctx, param, value): diff --git a/src/aiida/engine/daemon/client.py b/src/aiida/engine/daemon/client.py index ef250802f7..4e47e7ed60 100644 --- a/src/aiida/engine/daemon/client.py +++ b/src/aiida/engine/daemon/client.py @@ -94,10 +94,10 @@ def __init__(self, profile: Profile): from aiida.common.docs import URL_NO_BROKER type_check(profile, Profile) - config = get_config() + self._config = get_config() self._profile = profile self._socket_directory: str | None = None - self._daemon_timeout: int = config.get_option('daemon.timeout', scope=profile.name) + self._daemon_timeout: int = self._config.get_option('daemon.timeout', scope=profile.name) if self._profile.process_control_backend is None: raise ConfigurationError( @@ -156,31 +156,31 @@ def virtualenv(self) -> str | None: @property def circus_log_file(self) -> str: - return self.profile.filepaths['circus']['log'] + return self._config.filepaths(self.profile)['circus']['log'] @property def circus_pid_file(self) -> str: - return self.profile.filepaths['circus']['pid'] + return self._config.filepaths(self.profile)['circus']['pid'] @property def circus_port_file(self) -> str: - return self.profile.filepaths['circus']['port'] + return self._config.filepaths(self.profile)['circus']['port'] @property def circus_socket_file(self) -> str: - return self.profile.filepaths['circus']['socket']['file'] + return self._config.filepaths(self.profile)['circus']['socket']['file'] @property def circus_socket_endpoints(self) -> dict[str, str]: - return self.profile.filepaths['circus']['socket'] + return self._config.filepaths(self.profile)['circus']['socket'] @property def daemon_log_file(self) -> str: - return self.profile.filepaths['daemon']['log'] + return self._config.filepaths(self.profile)['daemon']['log'] @property def daemon_pid_file(self) -> str: - return self.profile.filepaths['daemon']['pid'] + return self._config.filepaths(self.profile)['daemon']['pid'] def get_circus_port(self) -> int: """Retrieve the port for the circus controller, which should be written to the circus port file. diff --git a/src/aiida/manage/configuration/__init__.py b/src/aiida/manage/configuration/__init__.py index 7227281507..097fcfc012 100644 --- a/src/aiida/manage/configuration/__init__.py +++ b/src/aiida/manage/configuration/__init__.py @@ -65,10 +65,10 @@ def get_config_path(): - """Returns path to .aiida configuration directory.""" - from .settings import AIIDA_CONFIG_FOLDER, DEFAULT_CONFIG_FILE_NAME + """Returns path to aiida configuration file.""" + from .settings import DEFAULT_CONFIG_FILE_NAME, AiiDAConfigDir - return os.path.join(AIIDA_CONFIG_FOLDER, DEFAULT_CONFIG_FILE_NAME) + return os.path.join(AiiDAConfigDir.get(), DEFAULT_CONFIG_FILE_NAME) def load_config(create=False) -> 'Config': diff --git a/src/aiida/manage/configuration/config.py b/src/aiida/manage/configuration/config.py index 4b1f032271..fff83f4330 100644 --- a/src/aiida/manage/configuration/config.py +++ b/src/aiida/manage/configuration/config.py @@ -21,6 +21,7 @@ import json import os import uuid +from pathlib import Path from typing import Any, Dict, List, Optional, Tuple from pydantic import ( @@ -780,3 +781,32 @@ def _atomic_write(self, filepath=None): handle.flush() os.rename(handle.name, self.filepath) + + def filepaths(self, profile: Profile): + """Return the filepaths used by a profile. + + :return: a dictionary of filepaths + """ + from aiida.manage.configuration.settings import AiiDAConfigPathResolver + + _config_path_resolver: AiiDAConfigPathResolver = AiiDAConfigPathResolver(Path(self.dirpath)) + daemon_dir = _config_path_resolver.daemon_dir + daemon_log_dir = _config_path_resolver.daemon_log_dir + + return { + 'circus': { + 'log': str(daemon_log_dir / f'circus-{profile.name}.log'), + 'pid': str(daemon_dir / f'circus-{profile.name}.pid'), + 'port': str(daemon_dir / f'circus-{profile.name}.port'), + 'socket': { + 'file': str(daemon_dir / f'circus-{profile.name}.sockets'), + 'controller': 'circus.c.sock', + 'pubsub': 'circus.p.sock', + 'stats': 'circus.s.sock', + }, + }, + 'daemon': { + 'log': str(daemon_log_dir / f'aiida-{profile.name}.log'), + 'pid': str(daemon_dir / f'aiida-{profile.name}.pid'), + }, + } diff --git a/src/aiida/manage/configuration/profile.py b/src/aiida/manage/configuration/profile.py index acaca2e892..93a3c5c911 100644 --- a/src/aiida/manage/configuration/profile.py +++ b/src/aiida/manage/configuration/profile.py @@ -56,7 +56,7 @@ def __init__(self, name: str, config: Mapping[str, Any], validate=True): ) self._name = name - self._attributes: Dict[str, Any] = deepcopy(config) + self._attributes: Dict[str, Any] = deepcopy(config) # type: ignore[arg-type] # Create a default UUID if not specified if self._attributes.get(self.KEY_UUID, None) is None: @@ -235,22 +235,28 @@ def filepaths(self): :return: a dictionary of filepaths """ - from .settings import DAEMON_DIR, DAEMON_LOG_DIR + from aiida.common.warnings import warn_deprecation + from aiida.manage.configuration.settings import AiiDAConfigPathResolver + + warn_deprecation('This method has been deprecated, use `filepaths` method from `Config` obj instead', version=3) + + daemon_dir = AiiDAConfigPathResolver().daemon_dir + daemon_log_dir = AiiDAConfigPathResolver().daemon_log_dir return { 'circus': { - 'log': str(DAEMON_LOG_DIR / f'circus-{self.name}.log'), - 'pid': str(DAEMON_DIR / f'circus-{self.name}.pid'), - 'port': str(DAEMON_DIR / f'circus-{self.name}.port'), + 'log': str(daemon_log_dir / f'circus-{self.name}.log'), + 'pid': str(daemon_dir / f'circus-{self.name}.pid'), + 'port': str(daemon_dir / f'circus-{self.name}.port'), 'socket': { - 'file': str(DAEMON_DIR / f'circus-{self.name}.sockets'), + 'file': str(daemon_dir / f'circus-{self.name}.sockets'), 'controller': 'circus.c.sock', 'pubsub': 'circus.p.sock', 'stats': 'circus.s.sock', }, }, 'daemon': { - 'log': str(DAEMON_LOG_DIR / f'aiida-{self.name}.log'), - 'pid': str(DAEMON_DIR / f'aiida-{self.name}.pid'), + 'log': str(daemon_log_dir / f'aiida-{self.name}.log'), + 'pid': str(daemon_dir / f'aiida-{self.name}.pid'), }, } diff --git a/src/aiida/manage/configuration/settings.py b/src/aiida/manage/configuration/settings.py index 168abb8879..f47a2dd66e 100644 --- a/src/aiida/manage/configuration/settings.py +++ b/src/aiida/manage/configuration/settings.py @@ -13,6 +13,7 @@ import os import pathlib import warnings +from typing import final DEFAULT_UMASK = 0o0077 DEFAULT_AIIDA_PATH_VARIABLE = 'AIIDA_PATH' @@ -25,38 +26,86 @@ DEFAULT_DAEMON_LOG_DIR_NAME = 'log' DEFAULT_ACCESS_CONTROL_DIR_NAME = 'access' -# Assign defaults which may be overriden in set_configuration_directory() below -AIIDA_CONFIG_FOLDER: pathlib.Path = pathlib.Path(DEFAULT_AIIDA_PATH).expanduser() / DEFAULT_CONFIG_DIR_NAME -DAEMON_DIR: pathlib.Path = AIIDA_CONFIG_FOLDER / DEFAULT_DAEMON_DIR_NAME -DAEMON_LOG_DIR: pathlib.Path = DAEMON_DIR / DEFAULT_DAEMON_LOG_DIR_NAME -ACCESS_CONTROL_DIR: pathlib.Path = AIIDA_CONFIG_FOLDER / DEFAULT_ACCESS_CONTROL_DIR_NAME +__all__ = ('AiiDAConfigPathResolver', 'AiiDAConfigDir') -def create_instance_directories() -> None: +@final +class AiiDAConfigDir: + """Singleton for setting and getting the path to configuration directory.""" + + _glb_aiida_config_folder: pathlib.Path = pathlib.Path(DEFAULT_AIIDA_PATH).expanduser() / DEFAULT_CONFIG_DIR_NAME + + @classmethod + def get(cls): + """Return the path of the configuration directory.""" + return cls._glb_aiida_config_folder + + @classmethod + def set(cls, aiida_config_folder: pathlib.Path | None = None) -> None: + """Set the configuration directory, related global variables and create instance directories. + + The location of the configuration directory is defined by ``aiida_config_folder`` or if not defined, + the path that is returned by ``_get_configuration_directory_from_envvar``. + Or if the environment_variable not set, use the default. + If the directory does not exist yet, it is created, together with all its subdirectories. + """ + _default_dirpath_config = pathlib.Path(DEFAULT_AIIDA_PATH).expanduser() / DEFAULT_CONFIG_DIR_NAME + + aiida_config_folder = aiida_config_folder or _get_configuration_directory_from_envvar() + cls._glb_aiida_config_folder = aiida_config_folder or _default_dirpath_config + + _create_instance_directories(cls._glb_aiida_config_folder) + + +@final +class AiiDAConfigPathResolver: + """For resolving configuration directory, daemon dir, daemon log dir and access control dir. + The locations are all trivially derived from the config directory. + """ + + def __init__(self, config_folder: pathlib.Path | None = None) -> None: + self._aiida_path = config_folder or AiiDAConfigDir.get() + + @property + def aiida_path(self) -> pathlib.Path: + return self._aiida_path + + @property + def daemon_dir(self) -> pathlib.Path: + return self._aiida_path / DEFAULT_DAEMON_DIR_NAME + + @property + def daemon_log_dir(self) -> pathlib.Path: + return self._aiida_path / DEFAULT_DAEMON_DIR_NAME / DEFAULT_DAEMON_LOG_DIR_NAME + + @property + def access_control_dir(self) -> pathlib.Path: + return self._aiida_path / DEFAULT_ACCESS_CONTROL_DIR_NAME + + +def _create_instance_directories(aiida_config_folder: pathlib.Path | None) -> None: """Create the base directories required for a new AiiDA instance. - This will create the base AiiDA directory defined by the AIIDA_CONFIG_FOLDER variable, unless it already exists. - Subsequently, it will create the daemon directory within it and the daemon log directory. + This will create the base AiiDA directory in ``aiida_config_folder``. + If it not provided, the directory returned from ``AiiDAConfigDir.get()`` will be the default config folder, + unless it already exists. Subsequently, it will create the daemon directory within it and the daemon log directory. """ from aiida.common import ConfigurationError - directory_base = AIIDA_CONFIG_FOLDER.expanduser() - directory_daemon = directory_base / DAEMON_DIR - directory_daemon_log = directory_base / DAEMON_LOG_DIR - directory_access = directory_base / ACCESS_CONTROL_DIR + path_resolver = AiiDAConfigPathResolver(aiida_config_folder) list_of_paths = [ - directory_base, - directory_daemon, - directory_daemon_log, - directory_access, + path_resolver.aiida_path, + path_resolver.daemon_dir, + path_resolver.daemon_log_dir, + path_resolver.access_control_dir, ] umask = os.umask(DEFAULT_UMASK) try: for path in list_of_paths: - if path is directory_base and not path.exists(): + if path is path_resolver.aiida_path and not path.exists(): warnings.warn(f'Creating AiiDA configuration folder `{path}`.') try: @@ -64,31 +113,10 @@ def create_instance_directories() -> None: except OSError as exc: raise ConfigurationError(f'could not create the `{path}` configuration directory: {exc}') from exc finally: - os.umask(umask) + _ = os.umask(umask) -def get_configuration_directory(): - """Return the path of the configuration directory. - - The location of the configuration directory is defined following these heuristics in order: - - * If the ``AIIDA_PATH`` variable is set, all the paths will be checked to see if they contain a - configuration folder. The first one to be encountered will be set as ``AIIDA_CONFIG_FOLDER``. If none of them - contain one, the last path defined in the environment variable considered is used. - * If an existing directory is still not found, the ``DEFAULT_AIIDA_PATH`` is used. - - :returns: The path of the configuration directory. - """ - dirpath_config = get_configuration_directory_from_envvar() - - # If no existing configuration directory is found, fall back to the default - if dirpath_config is None: - dirpath_config = pathlib.Path(DEFAULT_AIIDA_PATH).expanduser() / DEFAULT_CONFIG_DIR_NAME - - return dirpath_config - - -def get_configuration_directory_from_envvar() -> pathlib.Path | None: +def _get_configuration_directory_from_envvar() -> pathlib.Path | None: """Return the path of a config directory from the ``AIIDA_PATH`` environment variable. The environment variable should be a colon separated string of filepaths that either point directly to a config @@ -99,10 +127,13 @@ def get_configuration_directory_from_envvar() -> pathlib.Path | None: """ environment_variable = os.environ.get(DEFAULT_AIIDA_PATH_VARIABLE) + default_dirpath_config = pathlib.Path(DEFAULT_AIIDA_PATH).expanduser() / DEFAULT_CONFIG_DIR_NAME + if environment_variable is None: return None # Loop over all the paths in the ``AIIDA_PATH`` variable to see if any of them contain a configuration folder + dirpath_config = None for base_dir_path in [path for path in environment_variable.split(':') if path]: dirpath_config = pathlib.Path(base_dir_path).expanduser() @@ -115,28 +146,8 @@ def get_configuration_directory_from_envvar() -> pathlib.Path | None: if dirpath_config.is_dir(): break - return dirpath_config - - -def set_configuration_directory(aiida_config_folder: pathlib.Path | None = None) -> None: - """Set the configuration directory, related global variables and create instance directories. - - The location of the configuration directory is defined by ``aiida_config_folder`` or if not defined, the path that - is returned by ``get_configuration_directory``. If the directory does not exist yet, it is created, together with - all its subdirectories. - """ - global AIIDA_CONFIG_FOLDER # noqa: PLW0603 - global DAEMON_DIR # noqa: PLW0603 - global DAEMON_LOG_DIR # noqa: PLW0603 - global ACCESS_CONTROL_DIR # noqa: PLW0603 - - AIIDA_CONFIG_FOLDER = aiida_config_folder or get_configuration_directory() - DAEMON_DIR = AIIDA_CONFIG_FOLDER / DEFAULT_DAEMON_DIR_NAME - DAEMON_LOG_DIR = DAEMON_DIR / DEFAULT_DAEMON_LOG_DIR_NAME - ACCESS_CONTROL_DIR = AIIDA_CONFIG_FOLDER / DEFAULT_ACCESS_CONTROL_DIR_NAME - - create_instance_directories() + return dirpath_config or default_dirpath_config # Initialize the configuration directory settings -set_configuration_directory() +AiiDAConfigDir.set() diff --git a/src/aiida/manage/profile_access.py b/src/aiida/manage/profile_access.py index 5b04481e66..c65364af03 100644 --- a/src/aiida/manage/profile_access.py +++ b/src/aiida/manage/profile_access.py @@ -18,8 +18,10 @@ from aiida.common.exceptions import LockedProfileError, LockingProfileError from aiida.common.lang import type_check from aiida.manage.configuration import Profile +from aiida.manage.configuration.settings import AiiDAConfigPathResolver +@typing.final class ProfileAccessManager: """Class to manage access to a profile. @@ -45,12 +47,10 @@ def __init__(self, profile: Profile): :param profile: the profile whose access to manage. """ - from aiida.manage.configuration.settings import ACCESS_CONTROL_DIR - - type_check(profile, Profile) + _ = type_check(profile, Profile) self.profile = profile self.process = psutil.Process(os.getpid()) - self._dirpath_records = ACCESS_CONTROL_DIR / profile.name + self._dirpath_records = AiiDAConfigPathResolver().access_control_dir / profile.name self._dirpath_records.mkdir(exist_ok=True) def request_access(self) -> None: diff --git a/src/aiida/manage/tests/pytest_fixtures.py b/src/aiida/manage/tests/pytest_fixtures.py index 92856aff66..6c5d04d3bc 100644 --- a/src/aiida/manage/tests/pytest_fixtures.py +++ b/src/aiida/manage/tests/pytest_fixtures.py @@ -162,6 +162,7 @@ def aiida_instance( """ from aiida.manage import configuration from aiida.manage.configuration import settings + from aiida.manage.configuration.settings import AiiDAConfigDir if aiida_test_profile: yield configuration.get_config() @@ -178,8 +179,7 @@ def aiida_instance( dirpath_config = tmp_path_factory.mktemp('config') os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] = str(dirpath_config) - settings.AIIDA_CONFIG_FOLDER = dirpath_config - settings.set_configuration_directory() + AiiDAConfigDir.set(dirpath_config) configuration.CONFIG = configuration.load_config(create=True) try: @@ -191,7 +191,7 @@ def aiida_instance( else: os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] = current_path_variable - settings.AIIDA_CONFIG_FOLDER = current_config_path + AiiDAConfigDir.set(current_config_path) configuration.CONFIG = current_config if current_profile: aiida_manager.load_profile(current_profile.name, allow_switch=True) diff --git a/src/aiida/storage/sqlite_dos/backend.py b/src/aiida/storage/sqlite_dos/backend.py index 7be70f4a1c..2443fee259 100644 --- a/src/aiida/storage/sqlite_dos/backend.py +++ b/src/aiida/storage/sqlite_dos/backend.py @@ -26,7 +26,7 @@ from aiida.common import exceptions from aiida.common.log import AIIDA_LOGGER from aiida.manage.configuration.profile import Profile -from aiida.manage.configuration.settings import AIIDA_CONFIG_FOLDER +from aiida.manage.configuration.settings import AiiDAConfigDir from aiida.orm.implementation import BackendEntity from aiida.storage.log import MIGRATE_LOGGER from aiida.storage.psql_dos.models.settings import DbSetting @@ -203,7 +203,7 @@ class Model(BaseModel, defer_build=True): filepath: str = Field( title='Directory of the backend', description='Filepath of the directory in which to store data for this backend.', - default_factory=lambda: str(AIIDA_CONFIG_FOLDER / 'repository' / f'sqlite_dos_{uuid4().hex}'), + default_factory=lambda: str(AiiDAConfigDir.get() / 'repository' / f'sqlite_dos_{uuid4().hex}'), ) @field_validator('filepath') diff --git a/src/aiida/tools/pytest_fixtures/configuration.py b/src/aiida/tools/pytest_fixtures/configuration.py index ca38db3fb0..ed06072b71 100644 --- a/src/aiida/tools/pytest_fixtures/configuration.py +++ b/src/aiida/tools/pytest_fixtures/configuration.py @@ -10,6 +10,8 @@ import pytest +from aiida.manage.configuration.settings import AiiDAConfigDir + if t.TYPE_CHECKING: from aiida.manage.configuration.config import Config @@ -53,7 +55,7 @@ def factory(dirpath: pathlib.Path): dirpath_config = dirpath / settings.DEFAULT_CONFIG_DIR_NAME os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] = str(dirpath_config) - settings.set_configuration_directory(dirpath_config) + AiiDAConfigDir.set(dirpath_config) config = get_config(create=True) try: @@ -61,7 +63,7 @@ def factory(dirpath: pathlib.Path): finally: if current_config: reset_config() - settings.set_configuration_directory(pathlib.Path(current_config.dirpath)) + AiiDAConfigDir.set(pathlib.Path(current_config.dirpath)) get_config() if current_path_variable is None: diff --git a/tests/conftest.py b/tests/conftest.py index fb6780d966..b50cf90843 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -373,7 +373,7 @@ def empty_config(tmp_path) -> Config: """ from aiida.common.utils import Capturing from aiida.manage import configuration, get_manager - from aiida.manage.configuration import settings + from aiida.manage.configuration.settings import AiiDAConfigDir manager = get_manager() @@ -387,7 +387,7 @@ def empty_config(tmp_path) -> Config: # Set the configuration directory to a temporary directory. This will create the necessary folders for an empty # AiiDA configuration and set relevant global variables in :mod:`aiida.manage.configuration.settings`. - settings.set_configuration_directory(tmp_path) + AiiDAConfigDir.set(tmp_path) # The constructor of `Config` called by `load_config` will print warning messages about migrating it with Capturing(): @@ -405,7 +405,7 @@ def empty_config(tmp_path) -> Config: # like the :class:`aiida.engine.daemon.client.DaemonClient` will not function properly after a test that uses # this fixture because the paths of the daemon files would still point to the path of the temporary config # folder created by this fixture. - settings.set_configuration_directory(pathlib.Path(current_config_path)) + AiiDAConfigDir.set(pathlib.Path(current_config_path)) # Reload the original profile manager.load_profile(current_profile_name) diff --git a/tests/manage/configuration/test_config.py b/tests/manage/configuration/test_config.py index b1decb09a7..7fedb4bf2a 100644 --- a/tests/manage/configuration/test_config.py +++ b/tests/manage/configuration/test_config.py @@ -19,6 +19,7 @@ from aiida.manage.configuration.config import Config from aiida.manage.configuration.migrations import CURRENT_CONFIG_VERSION, OLDEST_COMPATIBLE_CONFIG_VERSION from aiida.manage.configuration.options import get_option +from aiida.manage.configuration.settings import AiiDAConfigDir from aiida.storage.sqlite_temp import SqliteTempBackend @@ -42,7 +43,7 @@ def cache_aiida_path_variable(): # Make sure to reset the global variables set by the following call that are dependent on the environment variable # ``DEFAULT_AIIDA_PATH_VARIABLE``. It may have been changed by a test using this fixture. - settings.set_configuration_directory() + AiiDAConfigDir.set() @pytest.mark.filterwarnings('ignore:Creating AiiDA configuration folder') @@ -65,11 +66,11 @@ def test_environment_variable_not_set(chdir_tmp_path, monkeypatch): del os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] except KeyError: pass - settings.set_configuration_directory() + AiiDAConfigDir.set() config_folder = chdir_tmp_path / settings.DEFAULT_CONFIG_DIR_NAME assert os.path.isdir(config_folder) - assert settings.AIIDA_CONFIG_FOLDER == pathlib.Path(config_folder) + assert AiiDAConfigDir.get() == pathlib.Path(config_folder) @pytest.mark.filterwarnings('ignore:Creating AiiDA configuration folder') @@ -78,12 +79,12 @@ def test_environment_variable_set_single_path_without_config_folder(tmp_path): """If `AIIDA_PATH` is set but does not contain a configuration folder, it should be created.""" # Set the environment variable and call configuration initialization os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] = str(tmp_path) - settings.set_configuration_directory() + AiiDAConfigDir.set() # This should have created the configuration directory in the path config_folder = tmp_path / settings.DEFAULT_CONFIG_DIR_NAME assert config_folder.is_dir() - assert settings.AIIDA_CONFIG_FOLDER == config_folder + assert AiiDAConfigDir.get() == config_folder @pytest.mark.filterwarnings('ignore:Creating AiiDA configuration folder') @@ -94,12 +95,12 @@ def test_environment_variable_set_single_path_with_config_folder(tmp_path): # Set the environment variable and call configuration initialization os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] = str(tmp_path) - settings.set_configuration_directory() + AiiDAConfigDir.set() # This should have created the configuration directory in the path config_folder = tmp_path / settings.DEFAULT_CONFIG_DIR_NAME assert config_folder.is_dir() - assert settings.AIIDA_CONFIG_FOLDER == config_folder + assert AiiDAConfigDir.get() == config_folder @pytest.mark.filterwarnings('ignore:Creating AiiDA configuration folder') @@ -114,12 +115,12 @@ def test_environment_variable_path_including_config_folder(tmp_path): """ # Set the environment variable with a path that include base folder name and call config initialization os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] = str(tmp_path / settings.DEFAULT_CONFIG_DIR_NAME) - settings.set_configuration_directory() + AiiDAConfigDir.set() # This should have created the configuration directory in the pathpath config_folder = tmp_path / settings.DEFAULT_CONFIG_DIR_NAME assert config_folder.is_dir() - assert settings.AIIDA_CONFIG_FOLDER == config_folder + assert AiiDAConfigDir.get() == config_folder @pytest.mark.filterwarnings('ignore:Creating AiiDA configuration folder') @@ -137,12 +138,12 @@ def test_environment_variable_set_multiple_path(tmp_path): # Set the environment variable to contain three paths and call configuration initialization env_variable = f'{directory_a}:{directory_b}:{directory_c}' os.environ[settings.DEFAULT_AIIDA_PATH_VARIABLE] = env_variable - settings.set_configuration_directory() + AiiDAConfigDir.set() # This should have created the configuration directory in the last path config_folder = directory_c / settings.DEFAULT_CONFIG_DIR_NAME assert os.path.isdir(config_folder) - assert settings.AIIDA_CONFIG_FOLDER == config_folder + assert AiiDAConfigDir.get() == config_folder def compare_config_in_memory_and_on_disk(config, filepath): @@ -152,9 +153,7 @@ def compare_config_in_memory_and_on_disk(config, filepath): :param filepath: absolute filepath to a configuration file :raises AssertionError: if content of `config` is not equal to that of file on disk """ - from aiida.manage.configuration.settings import DEFAULT_CONFIG_INDENT_SIZE - - in_memory = json.dumps(config.dictionary, indent=DEFAULT_CONFIG_INDENT_SIZE) + in_memory = json.dumps(config.dictionary, indent=settings.DEFAULT_CONFIG_INDENT_SIZE) # Read the content stored on disk with open(filepath, 'r', encoding='utf8') as handle: diff --git a/tests/manage/test_caching_config.py b/tests/manage/test_caching_config.py index 50a2e4ac48..932a32873c 100644 --- a/tests/manage/test_caching_config.py +++ b/tests/manage/test_caching_config.py @@ -45,11 +45,12 @@ def test_merge_deprecated_yaml(tmp_path): """ from aiida.common.warnings import AiidaDeprecationWarning from aiida.manage import configuration, get_manager - from aiida.manage.configuration import get_config_option, load_profile, settings + from aiida.manage.configuration import get_config_option, load_profile + from aiida.manage.configuration.settings import AiiDAConfigDir # Store the current configuration instance and config directory path current_config = configuration.CONFIG - current_config_path = current_config.dirpath + current_config_path = pathlib.Path(current_config.dirpath) current_profile_name = configuration.get_profile().name try: @@ -57,7 +58,7 @@ def test_merge_deprecated_yaml(tmp_path): configuration.CONFIG = None # Create a temporary folder, set it as the current config directory path - settings.AIIDA_CONFIG_FOLDER = str(tmp_path) + AiiDAConfigDir.set(pathlib.Path(tmp_path)) config_dictionary = json.loads( pathlib.Path(__file__) .parent.joinpath('configuration/migrations/test_samples/reference/6.json') @@ -86,7 +87,7 @@ def test_merge_deprecated_yaml(tmp_path): # Reset the config folder path and the config instance. Note this will always be executed after the yield no # matter what happened in the test that used this fixture. get_manager().unload_profile() - settings.AIIDA_CONFIG_FOLDER = current_config_path + AiiDAConfigDir.set(current_config_path) configuration.CONFIG = current_config load_profile(current_profile_name)