From 18413f54f690dedd390c31264c47a793b084b5c6 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Fri, 16 Feb 2024 21:19:39 +0200 Subject: [PATCH] Sync conflicted items --- platformio/check/tools/base.py | 2 +- platformio/debug/config/base.py | 2 +- platformio/project/commands/metadata.py | 2 +- platformio/project/helpers.py | 93 +++++++++++---------- platformio/project/integration/generator.py | 37 ++++---- platformio/test/runners/readers/native.py | 10 ++- 6 files changed, 80 insertions(+), 66 deletions(-) mode change 100644 => 100755 platformio/check/tools/base.py mode change 100644 => 100755 platformio/project/commands/metadata.py mode change 100644 => 100755 platformio/project/integration/generator.py mode change 100644 => 100755 platformio/test/runners/readers/native.py diff --git a/platformio/check/tools/base.py b/platformio/check/tools/base.py old mode 100644 new mode 100755 index e8c19013..ccf331f4 --- a/platformio/check/tools/base.py +++ b/platformio/check/tools/base.py @@ -57,7 +57,7 @@ class CheckToolBase: # pylint: disable=too-many-instance-attributes ] def _load_cpp_data(self): - data = load_build_metadata(None, self.envname) + data = load_build_metadata(self.project_dir, self.envname) if not data: return self.cc_flags = data.get("cc_flags", []) diff --git a/platformio/debug/config/base.py b/platformio/debug/config/base.py index 1caae23f..fff362a0 100644 --- a/platformio/debug/config/base.py +++ b/platformio/debug/config/base.py @@ -149,7 +149,7 @@ class DebugConfigBase: # pylint: disable=too-many-instance-attributes def _load_build_data(self): data = load_build_metadata( - os.getcwd(), self.env_name, cache=True, build_type="debug" + os.getcwd(), self.env_name, cache=True, force_targets=["__debug"] ) if not data: raise DebugInvalidOptionsError("Could not load a build configuration") diff --git a/platformio/project/commands/metadata.py b/platformio/project/commands/metadata.py old mode 100644 new mode 100755 index 8aa750e6..ee9f5b19 --- a/platformio/project/commands/metadata.py +++ b/platformio/project/commands/metadata.py @@ -42,7 +42,7 @@ def project_metadata_cmd(project_dir, environments, json_output, json_output_pat config = ProjectConfig.get_instance() config.validate(environments) environments = list(environments or config.envs()) - build_metadata = load_build_metadata(None, environments) + build_metadata = load_build_metadata(project_dir, environments) if not json_output: install_project_dependencies( diff --git a/platformio/project/helpers.py b/platformio/project/helpers.py index 4d153492..bbb10261 100644 --- a/platformio/project/helpers.py +++ b/platformio/project/helpers.py @@ -12,22 +12,29 @@ # See the License for the specific language governing permissions and # limitations under the License. +import hashlib import os import re import subprocess -from hashlib import sha1 from click.testing import CliRunner from platformio import __version__, exception, fs from platformio.compat import IS_MACOS, IS_WINDOWS, hashlib_encode_data from platformio.project.config import ProjectConfig +from platformio.project.options import ProjectOptions def get_project_dir(): return os.getcwd() +def get_project_id(project_dir=None): + return hashlib.sha1( + hashlib_encode_data(project_dir or get_project_dir()) + ).hexdigest() + + def is_platformio_project(project_dir=None): if not project_dir: project_dir = get_project_dir() @@ -92,7 +99,7 @@ def get_default_projects_dir(): def compute_project_checksum(config): # rebuild when PIO Core version changes - checksum = sha1(hashlib_encode_data(__version__)) + checksum = hashlib.sha1(hashlib_encode_data(__version__)) # configuration file state config_data = config.to_json() @@ -131,27 +138,27 @@ def compute_project_checksum(config): return checksum.hexdigest() -def load_build_metadata(project_dir, env_or_envs, cache=False, build_type=None): - assert env_or_envs - env_names = env_or_envs - if not isinstance(env_names, list): - env_names = [env_names] +def get_build_type(config, env, run_targets=None): + types = [] + run_targets = run_targets or [] + env_build_type = config.get(f"env:{env}", "build_type") + if set(["__debug", "__memusage"]) & set(run_targets) or env_build_type == "debug": + types.append("debug") + if "__test" in run_targets or env_build_type == "test": + types.append("test") + return ", ".join(types or [ProjectOptions["env.build_type"].default]) - with fs.cd(project_dir): - result = _get_cached_build_metadata(env_names) if cache else {} - # incompatible build-type data - for env_name in list(result.keys()): - if build_type is None: - build_type = ProjectConfig.get_instance().get( - f"env:{env_name}", "build_type" - ) - if result[env_name].get("build_type", "") != build_type: - del result[env_name] - missed_env_names = set(env_names) - set(result.keys()) - if missed_env_names: - result.update( - _load_build_metadata(project_dir, missed_env_names, build_type) - ) + +def load_build_metadata(project_dir, env_or_envs, cache=False, force_targets=None): + assert env_or_envs + envs = env_or_envs + if not isinstance(envs, list): + envs = [envs] + with fs.cd(project_dir or os.getcwd()): + result = _get_cached_build_metadata(envs, force_targets) if cache else {} + missed_envs = set(envs) - set(result.keys()) + if missed_envs: + result.update(_load_build_metadata(missed_envs, force_targets)) if not isinstance(env_or_envs, list) and env_or_envs in result: return result[env_or_envs] @@ -162,18 +169,28 @@ def load_build_metadata(project_dir, env_or_envs, cache=False, build_type=None): load_project_ide_data = load_build_metadata -def _load_build_metadata(project_dir, env_names, build_type=None): +def _get_cached_build_metadata(envs, force_targets=None): + config = ProjectConfig.get_instance(os.path.join(os.getcwd(), "platformio.ini")) + build_dir = config.get("platformio", "build_dir") + result = {} + for env in envs: + build_type = get_build_type(config, env, force_targets) + json_path = os.path.join(build_dir, env, build_type, "metadata.json") + if os.path.isfile(json_path): + result[env] = fs.load_json(json_path) + return result + + +def _load_build_metadata(envs, force_targets=None): # pylint: disable=import-outside-toplevel from platformio import app from platformio.run.cli import cli as cmd_run - args = ["--project-dir", project_dir, "--target", "__idedata"] - if build_type == "debug": - args.extend(["--target", "__debug"]) - # if build_type == "test": - # args.extend(["--target", "__test"]) - for name in env_names: - args.extend(["-e", name]) + args = ["--target", "__metadata"] + for target in force_targets or []: + args.extend(["--target", target]) + for env in envs: + args.extend(["-e", env]) app.set_session_var("pause_telemetry", True) result = CliRunner().invoke(cmd_run, args) app.set_session_var("pause_telemetry", False) @@ -181,18 +198,6 @@ def _load_build_metadata(project_dir, env_names, build_type=None): result.exception, exception.ReturnErrorCode ): raise result.exception - if '"includes":' not in result.output: + if "Metadata has been saved to the following location" not in result.output: raise exception.UserSideException(result.output) - return _get_cached_build_metadata(env_names) - - -def _get_cached_build_metadata(env_names): - build_dir = ProjectConfig.get_instance().get("platformio", "build_dir") - result = {} - for env_name in env_names: - if not os.path.isfile(os.path.join(build_dir, env_name, "idedata.json")): - continue - result[env_name] = fs.load_json( - os.path.join(build_dir, env_name, "idedata.json") - ) - return result + return _get_cached_build_metadata(envs, force_targets) diff --git a/platformio/project/integration/generator.py b/platformio/project/integration/generator.py old mode 100644 new mode 100755 index b9397567..3cb677f1 --- a/platformio/project/integration/generator.py +++ b/platformio/project/integration/generator.py @@ -27,7 +27,7 @@ from platformio.project.helpers import load_build_metadata class ProjectGenerator: def __init__(self, config, env_name, ide, boards=None): self.config = config - self.project_dir = os.getcwd() + self.project_dir = os.path.dirname(config.path) self.forced_env_name = env_name self.env_name = str(env_name or self.get_best_envname(boards)) self.ide = str(ide) @@ -103,18 +103,20 @@ class ProjectGenerator: # default env configuration tpl_vars.update(self.config.items(env=self.env_name, as_dict=True)) # build data - tpl_vars.update(load_build_metadata(None, self.env_name) or {}) - tpl_vars.update( - { - "src_files": self.get_src_files(), - "project_src_dir": self.config.get("platformio", "src_dir"), - "project_lib_dir": self.config.get("platformio", "lib_dir"), - "project_test_dir": self.config.get("platformio", "test_dir"), - "project_libdeps_dir": os.path.join( - self.config.get("platformio", "libdeps_dir"), self.env_name - ), - } - ) + tpl_vars.update(load_build_metadata(self.project_dir, self.env_name) or {}) + + with fs.cd(self.project_dir): + tpl_vars.update( + { + "src_files": self.get_src_files(), + "project_src_dir": self.config.get("platformio", "src_dir"), + "project_lib_dir": self.config.get("platformio", "lib_dir"), + "project_test_dir": self.config.get("platformio", "test_dir"), + "project_libdeps_dir": os.path.join( + self.config.get("platformio", "libdeps_dir"), self.env_name + ), + } + ) for key, value in tpl_vars.items(): if key.endswith(("_path", "_dir")): @@ -130,9 +132,12 @@ class ProjectGenerator: def get_src_files(self): result = [] - for root, _, files in os.walk(self.config.get("platformio", "src_dir")): - for f in files: - result.append(os.path.relpath(os.path.join(os.path.abspath(root), f))) + with fs.cd(self.project_dir): + for root, _, files in os.walk(self.config.get("platformio", "src_dir")): + for f in files: + result.append( + os.path.relpath(os.path.join(os.path.abspath(root), f)) + ) return result def get_tpls(self): diff --git a/platformio/test/runners/readers/native.py b/platformio/test/runners/readers/native.py old mode 100644 new mode 100755 index ac339697..c8adcff1 --- a/platformio/test/runners/readers/native.py +++ b/platformio/test/runners/readers/native.py @@ -83,11 +83,15 @@ class NativeTestOutputReader: # if user changed PROGNAME if not os.path.exists(cmd[0]): build_data = load_build_metadata( - None, + os.getcwd(), self.test_runner.test_suite.env_name, cache=True, - debug=not self.test_runner.options.without_debugging, - test=True, + force_targets=["__test"] + + ( + ["__debug"] + if not self.test_runner.options.without_debugging + else [] + ), ) if build_data: cmd[0] = build_data["prog_path"]