From 635a8c9ce6b6006f504e281a5919865e0def0270 Mon Sep 17 00:00:00 2001 From: horw Date: Mon, 27 Nov 2023 17:59:44 +0800 Subject: [PATCH] fix: move session_args to init file --- idf_build_apps/__init__.py | 5 +++++ idf_build_apps/app.py | 5 +++-- idf_build_apps/main.py | 6 ++---- idf_build_apps/session_args.py | 36 +++------------------------------- idf_build_apps/utils.py | 8 ++++---- tests/conftest.py | 7 +++---- tests/test_finder.py | 9 +++------ 7 files changed, 23 insertions(+), 53 deletions(-) diff --git a/idf_build_apps/__init__.py b/idf_build_apps/__init__.py index b29f67c..465b4bb 100644 --- a/idf_build_apps/__init__.py +++ b/idf_build_apps/__init__.py @@ -12,7 +12,12 @@ __version__ = '2.0.0b3' +from .session_args import ( + SessionArgs, +) + LOGGER = logging.getLogger('idf_build_apps') +SESSION_ARGS = SessionArgs() from .app import ( App, diff --git a/idf_build_apps/app.py b/idf_build_apps/app.py index b6402f8..37adf0d 100644 --- a/idf_build_apps/app.py +++ b/idf_build_apps/app.py @@ -31,6 +31,8 @@ computed_field, ) +import idf_build_apps + from . import ( LOGGER, ) @@ -55,7 +57,6 @@ ) from .session_args import ( SessionArgs, - get_session_args, ) from .utils import ( BaseModel, @@ -194,7 +195,7 @@ def __init__( # create logger and process sdkconfig files self._logger = LOGGER.getChild(str(hash(self))) self._logger.addFilter(_AppBuildStageFilter(app=self)) - self.session_args = get_session_args() + self.session_args = idf_build_apps.SESSION_ARGS self._process_sdkconfig_files() diff --git a/idf_build_apps/main.py b/idf_build_apps/main.py index ad04834..4462bc7 100644 --- a/idf_build_apps/main.py +++ b/idf_build_apps/main.py @@ -15,6 +15,7 @@ from . import ( LOGGER, + SESSION_ARGS, ) from .app import ( App, @@ -46,9 +47,6 @@ FolderRule, Manifest, ) -from .session_args import ( - set_session_args, -) from .utils import ( InvalidCommand, files_matches_patterns, @@ -730,7 +728,7 @@ def main(): apply_config_args(args) validate_args(parser, args) - set_session_args(args) + SESSION_ARGS.set(args) # real call starts here apps = find_apps( args.paths, diff --git a/idf_build_apps/session_args.py b/idf_build_apps/session_args.py index 46f3c6b..32275db 100644 --- a/idf_build_apps/session_args.py +++ b/idf_build_apps/session_args.py @@ -4,7 +4,6 @@ import os import re import typing as t -import warnings from .utils import ( BaseModel, @@ -18,16 +17,12 @@ class SessionArgs(BaseModel): override_sdkconfig_items: t.Dict[str, t.Any] = {} override_sdkconfig_file_path: t.Optional[str] = None - def __new__(cls, *args, **kwargs): - if not hasattr(cls, '_instance'): - cls._instance = super().__new__(cls) - return cls._instance - - def __init__(self, parsed_args, *, workdir=None): + def __init__(self): super().__init__() + + def set(self, parsed_args, *, workdir=None): if workdir: self.workdir = workdir - self._setup_override_sdkconfig(parsed_args) def _setup_override_sdkconfig(self, args): @@ -75,28 +70,3 @@ def _create_override_sdkconfig_merged_file(self, override_sdkconfig_merged_items for key, value in override_sdkconfig_merged_items.items(): f.write(f'{key}={value}\n') return f_path - - -_session_args: t.Optional[SessionArgs] = None - - -def get_session_args() -> t.Optional[SessionArgs]: - global _session_args - if _session_args is None: - warnings.warn('SessionArgs have not been set up yet.') - return _session_args - - -def set_session_args(*args, **kwargs) -> None: - global _session_args - if _session_args is None: - _session_args = SessionArgs(*args, **kwargs) - else: - warnings.warn('SessionArgs have been set up. Use `get_session_args` to retrieve an instance of this class.') - - -def reset_singleton() -> None: - global _session_args - if hasattr(SessionArgs, '_instance'): - _session_args = None - del SessionArgs._instance diff --git a/idf_build_apps/utils.py b/idf_build_apps/utils.py index 4d8b22c..199a8bf 100644 --- a/idf_build_apps/utils.py +++ b/idf_build_apps/utils.py @@ -20,10 +20,6 @@ ) from pydantic import BaseModel as _BaseModel -from . import ( - LOGGER, -) - try: from typing import ( Literal, @@ -161,6 +157,10 @@ def subprocess_run( :param additional_env_dict: additional environment variables :return: return code """ + from . import ( + LOGGER, + ) + LOGGER.debug('==> Running %s', ' '.join(cmd)) subprocess_env = None diff --git a/tests/conftest.py b/tests/conftest.py index f43843a..7f3ff6b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,19 +4,18 @@ import pytest +import idf_build_apps from idf_build_apps import ( App, + session_args, setup_logging, ) -from idf_build_apps.session_args import ( - reset_singleton, -) @pytest.fixture(autouse=True) def clean_cls_attr(): App.MANIFEST = None - reset_singleton() + idf_build_apps.SESSION_ARGS = session_args.SessionArgs() @pytest.fixture(autouse=True) diff --git a/tests/test_finder.py b/tests/test_finder.py index 3a93d21..9a04c09 100644 --- a/tests/test_finder.py +++ b/tests/test_finder.py @@ -402,10 +402,7 @@ def test_with_sdkconfig_defaults_env_var_expansion(self, tmp_path, monkeypatch): assert len(apps) == 0 def test_with_sdkconfig_override(self, tmp_path): - from idf_build_apps.session_args import ( - get_session_args, - set_session_args, - ) + import idf_build_apps create_project('test1', tmp_path) (tmp_path / 'test1' / 'sdkconfig.defaults').write_text( @@ -426,8 +423,8 @@ def test_with_sdkconfig_override(self, tmp_path): args = Args( 'CONFIG_IDF_TARGET=esp32c3,CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=1522,CONFIG_A=10', 'sdkconfig.override1' ) - set_session_args(args, workdir=tmp_path) - session_args = get_session_args() + idf_build_apps.SESSION_ARGS.set(args, workdir=tmp_path) + session_args = idf_build_apps.SESSION_ARGS assert str(tmp_path) in str(session_args.override_sdkconfig_file_path) assert len(session_args.override_sdkconfig_items) == 4