From b764a2220fff96b6e779b45d820739c4c1e7cf4e Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 17 May 2022 13:33:25 +0300 Subject: [PATCH] Improved support for the renamed configuration options // Resolve #4270 --- HISTORY.rst | 1 + platformio/builder/tools/pioproject.py | 10 +-- platformio/project/config.py | 89 ++++++++++++-------------- tests/project/test_config.py | 23 +++++-- 4 files changed, 68 insertions(+), 55 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 52599ced..bb35be2c 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -14,6 +14,7 @@ PlatformIO Core 6 6.0.1 (2022-05-??) ~~~~~~~~~~~~~~~~~~ +* Improved support for the renamed configuration options (`issue #4270 `_) * Fixed an issue when calling built-in `pio device monitor `__ filter 6.0.0 (2022-05-16) diff --git a/platformio/builder/tools/pioproject.py b/platformio/builder/tools/pioproject.py index 425d6758..bf59d918 100644 --- a/platformio/builder/tools/pioproject.py +++ b/platformio/builder/tools/pioproject.py @@ -15,7 +15,7 @@ from __future__ import absolute_import from platformio.compat import MISSING -from platformio.project.config import ProjectConfig, ProjectOptions +from platformio.project.config import ProjectConfig def GetProjectConfig(env): @@ -31,15 +31,17 @@ def GetProjectOption(env, option, default=MISSING): def LoadProjectOptions(env): - for option, value in env.GetProjectOptions(): - option_meta = ProjectOptions.get("env." + option) + config = env.GetProjectConfig() + section = "env:" + env["PIOENV"] + for option in config.options(section): + option_meta = config.find_option_meta(section, option) if ( not option_meta or not option_meta.buildenvvar or option_meta.buildenvvar in env ): continue - env[option_meta.buildenvvar] = value + env[option_meta.buildenvvar] = config.get(section, option) def exists(_): diff --git a/platformio/project/config.py b/platformio/project/config.py index 9b7f43cf..0a4c6ed1 100644 --- a/platformio/project/config.py +++ b/platformio/project/config.py @@ -43,6 +43,8 @@ class ProjectConfigBase(object): INLINE_COMMENT_RE = re.compile(r"\s+;.*$") VARTPL_RE = re.compile(r"\$\{([^\.\}\()]+)\.([^\}]+)\}") + CUSTOM_OPTION_PREFIXES = ("custom_", "board_") + expand_interpolations = True warnings = [] @@ -109,23 +111,6 @@ class ProjectConfigBase(object): self.read(item) def _maintain_renaimed_options(self): - # legacy `lib_extra_dirs` in [platformio] - if self._parser.has_section("platformio") and self._parser.has_option( - "platformio", "lib_extra_dirs" - ): - if not self._parser.has_section("env"): - self._parser.add_section("env") - self._parser.set( - "env", - "lib_extra_dirs", - self._parser.get("platformio", "lib_extra_dirs"), - ) - self._parser.remove_option("platformio", "lib_extra_dirs") - self.warnings.append( - "`lib_extra_dirs` configuration option is deprecated in " - "section [platformio]! Please move it to global `env` section" - ) - renamed_options = {} for option in ProjectOptions.values(): if option.oldnames: @@ -143,19 +128,20 @@ class ProjectConfigBase(object): "Please use `%s` instead" % (option, section, renamed_options[option]) ) - # rename on-the-fly - self._parser.set( - section, - renamed_options[option], - self._parser.get(section, option), - ) - self._parser.remove_option(section, option) + # # rename on-the-fly + # self._parser.set( + # section, + # renamed_options[option], + # self._parser.get(section, option), + # ) + # self._parser.remove_option(section, option) continue # unknown unknown_conditions = [ ("%s.%s" % (scope, option)) not in ProjectOptions, - scope != "env" or not option.startswith(("custom_", "board_")), + scope != "env" + or not option.startswith(self.CUSTOM_OPTION_PREFIXES), ] if all(unknown_conditions): self.warnings.append( @@ -237,16 +223,7 @@ class ProjectConfigBase(object): value = "\n" + value self._parser.set(section, option, value) - def getraw(self, section, option, default=MISSING): - try: - return self._getraw(section, option, default) - except configparser.NoOptionError as exc: - renamed_option = self._resolve_renamed_option(section, option) - if renamed_option: - return self._getraw(section, renamed_option, default) - raise exc - - def _resolve_renamed_option(self, section, old_name): + def resolve_renamed_option(self, section, old_name): scope = self.get_section_scope(section) if scope not in ("platformio", "env"): return None @@ -259,19 +236,39 @@ class ProjectConfigBase(object): return option_meta.name return None - def _getraw(self, section, option, default): # pylint: disable=too-many-branches + def find_option_meta(self, section, option): + scope = self.get_section_scope(section) + if scope not in ("platformio", "env"): + return None + option_meta = ProjectOptions.get("%s.%s" % (scope, option)) + if option_meta: + return option_meta + for option_meta in ProjectOptions.values(): + if option_meta.scope == scope and option in (option_meta.oldnames or []): + return option_meta + return None + + def _traverse_for_value(self, section, option, option_meta=None): + for _section, _option in self.walk_options(section): + if _option == option or ( + option_meta + and ( + option_meta.name == _option + or _option in (option_meta.oldnames or []) + ) + ): + return self._parser.get(_section, _option) + return MISSING + + def getraw( + self, section, option, default=MISSING + ): # pylint: disable=too-many-branches if not self.expand_interpolations: return self._parser.get(section, option) - value = MISSING - for sec, opt in self.walk_options(section): - if opt == option: - value = self._parser.get(sec, option) - break + option_meta = self.find_option_meta(section, option) + value = self._traverse_for_value(section, option, option_meta) - option_meta = ProjectOptions.get( - "%s.%s" % (self.get_section_scope(section), option) - ) if not option_meta: if value == MISSING: value = ( @@ -342,9 +339,7 @@ class ProjectConfigBase(object): except configparser.Error as e: raise exception.InvalidProjectConfError(self.path, str(e)) - option_meta = ProjectOptions.get( - "%s.%s" % (self.get_section_scope(section), option) - ) + option_meta = self.find_option_meta(section, option) if not option_meta: return value diff --git a/tests/project/test_config.py b/tests/project/test_config.py index 25b7f545..aa948f71 100644 --- a/tests/project/test_config.py +++ b/tests/project/test_config.py @@ -101,9 +101,13 @@ debug_flags = -D DEBUG=1 debug_server = ${platformio.packages_dir}/tool-openocd/openocd --help +src_filter = -<*> + + + + [env:extra_2] build_flags = -Og +src_filter = ${custom.src_filter} + """ DEFAULT_CORE_DIR = os.path.join(fs.expanduser("~"), ".platformio") @@ -130,7 +134,7 @@ def test_empty_config(): def test_warnings(config): config.validate(["extra_2", "base"], silent=True) - assert len(config.warnings) == 2 + assert len(config.warnings) == 3 assert "lib_install" in config.warnings[1] with pytest.raises(UnknownEnvNamesError): @@ -213,8 +217,9 @@ def test_options(config): def test_has_option(config): assert config.has_option("env:base", "monitor_speed") assert not config.has_option("custom", "monitor_speed") - assert not config.has_option("env:extra_1", "lib_install") + assert config.has_option("env:extra_1", "lib_install") assert config.has_option("env:test_extends", "lib_compat_mode") + assert config.has_option("env:extra_2", "src_filter") def test_sysenv_options(config): @@ -312,6 +317,8 @@ def test_getraw_value(config): assert config.getraw("platformio", "build_dir") == "~/tmp/pio-$PROJECT_HASH" # renamed option + assert config.getraw("env:extra_1", "lib_install") == "574" + assert config.getraw("env:extra_1", "lib_deps") == "574" assert config.getraw("env:base", "debug_load_cmd") == ["load"] @@ -349,6 +356,11 @@ def test_get_value(config): ) assert "$PROJECT_HASH" not in config.get("platformio", "build_dir") + # renamed option + assert config.get("env:extra_1", "lib_install") == ["574"] + assert config.get("env:extra_1", "lib_deps") == ["574"] + assert config.get("env:base", "debug_load_cmd") == ["load"] + def test_items(config): assert config.items("custom") == [ @@ -363,6 +375,7 @@ def test_items(config): "\n%s/tool-openocd/openocd\n--help" % os.path.join(DEFAULT_CORE_DIR, "packages"), ), + ("src_filter", "-<*>\n+\n+"), ] assert config.items(env="base") == [ ("build_flags", ["-D DEBUG=1"]), @@ -379,9 +392,10 @@ def test_items(config): "build_flags", ["-fdata-sections", "-Wl,--gc-sections", "-lc -lm", "-D DEBUG=1"], ), - ("lib_deps", ["574"]), + ("lib_install", ["574"]), ("monitor_speed", 9600), ("custom_monitor_speed", "115200"), + ("lib_deps", ["574"]), ("lib_ignore", ["LibIgnoreCustom"]), ("custom_builtin_option", "release"), ] @@ -396,6 +410,7 @@ def test_items(config): "--help", ], ), + ("src_filter", ["-<*>", "+", "+ +"]), ("monitor_speed", 9600), ("custom_monitor_speed", "115200"), ("lib_deps", ["Lib1", "Lib2"]), @@ -496,10 +511,10 @@ def test_dump(tmpdir_factory): ( "platformio", [ + ("env_default", ["base", "extra_2"]), ("src_dir", "${custom.src_dir}"), ("build_dir", "${custom.build_dir}"), ("extra_configs", ["extra_envs.ini", "extra_debug.ini"]), - ("default_envs", ["base", "extra_2"]), ], ), (