From 6ec11a4f6abe6b89bbeda139057a471dfb9d37b9 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 22 Nov 2024 11:14:53 +0100 Subject: [PATCH] Move filepaths to config --- src/aiida/__init__.py | 3 --- src/aiida/engine/daemon/client.py | 18 ++++++------- src/aiida/manage/configuration/config.py | 31 ++++++++++++++++++++++ src/aiida/manage/configuration/profile.py | 19 ++++++------- src/aiida/manage/configuration/settings.py | 6 ++++- src/aiida/manage/profile_access.py | 3 ++- 6 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/aiida/__init__.py b/src/aiida/__init__.py index 42c5e8299b..69cdb38bed 100644 --- a/src/aiida/__init__.py +++ b/src/aiida/__init__.py @@ -36,9 +36,6 @@ ) __paper_short__ = 'S. P. Huber et al., Scientific Data 7, 300 (2020).' -# Initialize the configuration directory settings -AiiDAConfigDir.set_configuration_directory() - def get_strict_version(): """Return a distutils StrictVersion instance with the current distribution version 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/config.py b/src/aiida/manage/configuration/config.py index 4b1f032271..beb8b1a697 100644 --- a/src/aiida/manage/configuration/config.py +++ b/src/aiida/manage/configuration/config.py @@ -20,6 +20,7 @@ import io import json import os +from pathlib import Path import uuid from typing import Any, Dict, List, Optional, Tuple @@ -780,3 +781,33 @@ def _atomic_write(self, filepath=None): handle.flush() os.rename(handle.name, self.filepath) + + + def filepaths(self, profile: Profile): + """Return the filepaths used by this 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 64662c8e49..6e1f014d3e 100644 --- a/src/aiida/manage/configuration/profile.py +++ b/src/aiida/manage/configuration/profile.py @@ -18,7 +18,6 @@ from aiida.common import exceptions from aiida.common.lang import type_check -from aiida.manage.configuration.settings import AiiDAConfigPathResolver from .options import parse_option @@ -49,7 +48,7 @@ class Profile: ) def __init__( - self, name: str, config: abc.Mapping[str, Any], config_folder: pathlib.Path | None = None, validate: bool = True + self, name: str, config: abc.Mapping[str, Any], validate: bool = True ): """Load a profile with the profile configuration.""" type_check(config, abc.Mapping) @@ -67,8 +66,6 @@ def __init__( self._attributes[self.KEY_UUID] = uuid4().hex - self._config_path_resolver: AiiDAConfigPathResolver = AiiDAConfigPathResolver(config_folder) - def __repr__(self) -> str: return f'Profile' @@ -88,11 +85,6 @@ def uuid(self) -> str: def uuid(self, value: str) -> None: self._attributes[self.KEY_UUID] = value - @property - def config_path_resolver(self) -> AiiDAConfigPathResolver: - """The config_path_resolver property.""" - return self._config_path_resolver - @property def default_user_email(self) -> str | None: """Return the default user email.""" @@ -245,8 +237,13 @@ def filepaths(self): :return: a dictionary of filepaths """ - daemon_dir = self._config_path_resolver.daemon_dir - daemon_log_dir = self._config_path_resolver.daemon_log_dir + from aiida.common.warnings import warn_deprecation + from aiida.manage.configuration.settings import AiiDAConfigPathResolver + + warn_deprecation('This method has been deprecated', version=3) + + daemon_dir = AiiDAConfigPathResolver().daemon_dir + daemon_log_dir = AiiDAConfigPathResolver().daemon_log_dir return { 'circus': { diff --git a/src/aiida/manage/configuration/settings.py b/src/aiida/manage/configuration/settings.py index 17e289088a..e942e06410 100644 --- a/src/aiida/manage/configuration/settings.py +++ b/src/aiida/manage/configuration/settings.py @@ -64,7 +64,7 @@ class AiiDAConfigPathResolver: The locations are all trivially derived from the config location, """ - def __init__(self, config_folder: pathlib.Path | None) -> None: + def __init__(self, config_folder: pathlib.Path | None = None) -> None: self._aiida_path = config_folder or AiiDAConfigDir.get_configuration_directory() @property @@ -148,3 +148,7 @@ def _get_configuration_directory_from_envvar() -> pathlib.Path | None: break return dirpath_config or default_dirpath_config + + +# Initialize the configuration directory settings +AiiDAConfigDir.set_configuration_directory() diff --git a/src/aiida/manage/profile_access.py b/src/aiida/manage/profile_access.py index 8602539e13..c65364af03 100644 --- a/src/aiida/manage/profile_access.py +++ b/src/aiida/manage/profile_access.py @@ -18,6 +18,7 @@ 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 @@ -49,7 +50,7 @@ def __init__(self, profile: Profile): _ = type_check(profile, Profile) self.profile = profile self.process = psutil.Process(os.getpid()) - self._dirpath_records = profile.config_path_resolver.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: