Skip to content

Commit

Permalink
Improved support for the renamed configuration options // Resolve #4270
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankravets committed May 17, 2022
1 parent 3776233 commit b764a22
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 55 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ PlatformIO Core 6
6.0.1 (2022-05-??)
~~~~~~~~~~~~~~~~~~

* Improved support for the renamed configuration options (`issue #4270 <https://github.com/platformio/platformio-core/issues/4270>`_)
* Fixed an issue when calling built-in `pio device monitor <https://docs.platformio.org/en/latest/core/userguide/device/cmd_monitor.html#filters>`__ filter

6.0.0 (2022-05-16)
Expand Down
10 changes: 6 additions & 4 deletions platformio/builder/tools/pioproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(_):
Expand Down
89 changes: 42 additions & 47 deletions platformio/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -109,23 +111,6 @@ def read(self, path, parse_extra=True):
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:
Expand All @@ -143,19 +128,20 @@ def _maintain_renaimed_options(self):
"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(
Expand Down Expand Up @@ -237,16 +223,7 @@ def set(self, section, option, value):
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
Expand All @@ -259,19 +236,39 @@ def _resolve_renamed_option(self, section, old_name):
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 = (
Expand Down Expand Up @@ -342,9 +339,7 @@ def get(self, section, option, default=MISSING):
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

Expand Down
23 changes: 19 additions & 4 deletions tests/project/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@
debug_server =
${platformio.packages_dir}/tool-openocd/openocd
--help
src_filter = -<*>
+<a>
+<b>
[env:extra_2]
build_flags = -Og
src_filter = ${custom.src_filter} +<c>
"""

DEFAULT_CORE_DIR = os.path.join(fs.expanduser("~"), ".platformio")
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"]


Expand Down Expand Up @@ -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") == [
Expand All @@ -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+<a>\n+<b>"),
]
assert config.items(env="base") == [
("build_flags", ["-D DEBUG=1"]),
Expand All @@ -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"),
]
Expand All @@ -396,6 +410,7 @@ def test_items(config):
"--help",
],
),
("src_filter", ["-<*>", "+<a>", "+<b> +<c>"]),
("monitor_speed", 9600),
("custom_monitor_speed", "115200"),
("lib_deps", ["Lib1", "Lib2"]),
Expand Down Expand Up @@ -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"]),
],
),
(
Expand Down

0 comments on commit b764a22

Please sign in to comment.