Sync conflicted items

This commit is contained in:
Ivan Kravets
2024-02-16 21:19:39 +02:00
parent d684233315
commit 18413f54f6
6 changed files with 80 additions and 66 deletions

2
platformio/check/tools/base.py Normal file → Executable file
View File

@ -57,7 +57,7 @@ class CheckToolBase: # pylint: disable=too-many-instance-attributes
] ]
def _load_cpp_data(self): def _load_cpp_data(self):
data = load_build_metadata(None, self.envname) data = load_build_metadata(self.project_dir, self.envname)
if not data: if not data:
return return
self.cc_flags = data.get("cc_flags", []) self.cc_flags = data.get("cc_flags", [])

View File

@ -149,7 +149,7 @@ class DebugConfigBase: # pylint: disable=too-many-instance-attributes
def _load_build_data(self): def _load_build_data(self):
data = load_build_metadata( 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: if not data:
raise DebugInvalidOptionsError("Could not load a build configuration") raise DebugInvalidOptionsError("Could not load a build configuration")

2
platformio/project/commands/metadata.py Normal file → Executable file
View File

@ -42,7 +42,7 @@ def project_metadata_cmd(project_dir, environments, json_output, json_output_pat
config = ProjectConfig.get_instance() config = ProjectConfig.get_instance()
config.validate(environments) config.validate(environments)
environments = list(environments or config.envs()) 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: if not json_output:
install_project_dependencies( install_project_dependencies(

View File

@ -12,22 +12,29 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import hashlib
import os import os
import re import re
import subprocess import subprocess
from hashlib import sha1
from click.testing import CliRunner from click.testing import CliRunner
from platformio import __version__, exception, fs from platformio import __version__, exception, fs
from platformio.compat import IS_MACOS, IS_WINDOWS, hashlib_encode_data from platformio.compat import IS_MACOS, IS_WINDOWS, hashlib_encode_data
from platformio.project.config import ProjectConfig from platformio.project.config import ProjectConfig
from platformio.project.options import ProjectOptions
def get_project_dir(): def get_project_dir():
return os.getcwd() 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): def is_platformio_project(project_dir=None):
if not project_dir: if not project_dir:
project_dir = get_project_dir() project_dir = get_project_dir()
@ -92,7 +99,7 @@ def get_default_projects_dir():
def compute_project_checksum(config): def compute_project_checksum(config):
# rebuild when PIO Core version changes # rebuild when PIO Core version changes
checksum = sha1(hashlib_encode_data(__version__)) checksum = hashlib.sha1(hashlib_encode_data(__version__))
# configuration file state # configuration file state
config_data = config.to_json() config_data = config.to_json()
@ -131,27 +138,27 @@ def compute_project_checksum(config):
return checksum.hexdigest() return checksum.hexdigest()
def load_build_metadata(project_dir, env_or_envs, cache=False, build_type=None): def get_build_type(config, env, run_targets=None):
assert env_or_envs types = []
env_names = env_or_envs run_targets = run_targets or []
if not isinstance(env_names, list): env_build_type = config.get(f"env:{env}", "build_type")
env_names = [env_names] 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 {} def load_build_metadata(project_dir, env_or_envs, cache=False, force_targets=None):
# incompatible build-type data assert env_or_envs
for env_name in list(result.keys()): envs = env_or_envs
if build_type is None: if not isinstance(envs, list):
build_type = ProjectConfig.get_instance().get( envs = [envs]
f"env:{env_name}", "build_type" with fs.cd(project_dir or os.getcwd()):
) result = _get_cached_build_metadata(envs, force_targets) if cache else {}
if result[env_name].get("build_type", "") != build_type: missed_envs = set(envs) - set(result.keys())
del result[env_name] if missed_envs:
missed_env_names = set(env_names) - set(result.keys()) result.update(_load_build_metadata(missed_envs, force_targets))
if missed_env_names:
result.update(
_load_build_metadata(project_dir, missed_env_names, build_type)
)
if not isinstance(env_or_envs, list) and env_or_envs in result: if not isinstance(env_or_envs, list) and env_or_envs in result:
return result[env_or_envs] 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 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 # pylint: disable=import-outside-toplevel
from platformio import app from platformio import app
from platformio.run.cli import cli as cmd_run from platformio.run.cli import cli as cmd_run
args = ["--project-dir", project_dir, "--target", "__idedata"] args = ["--target", "__metadata"]
if build_type == "debug": for target in force_targets or []:
args.extend(["--target", "__debug"]) args.extend(["--target", target])
# if build_type == "test": for env in envs:
# args.extend(["--target", "__test"]) args.extend(["-e", env])
for name in env_names:
args.extend(["-e", name])
app.set_session_var("pause_telemetry", True) app.set_session_var("pause_telemetry", True)
result = CliRunner().invoke(cmd_run, args) result = CliRunner().invoke(cmd_run, args)
app.set_session_var("pause_telemetry", False) 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 result.exception, exception.ReturnErrorCode
): ):
raise result.exception 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) raise exception.UserSideException(result.output)
return _get_cached_build_metadata(env_names) return _get_cached_build_metadata(envs, force_targets)
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

37
platformio/project/integration/generator.py Normal file → Executable file
View File

@ -27,7 +27,7 @@ from platformio.project.helpers import load_build_metadata
class ProjectGenerator: class ProjectGenerator:
def __init__(self, config, env_name, ide, boards=None): def __init__(self, config, env_name, ide, boards=None):
self.config = config self.config = config
self.project_dir = os.getcwd() self.project_dir = os.path.dirname(config.path)
self.forced_env_name = env_name self.forced_env_name = env_name
self.env_name = str(env_name or self.get_best_envname(boards)) self.env_name = str(env_name or self.get_best_envname(boards))
self.ide = str(ide) self.ide = str(ide)
@ -103,18 +103,20 @@ class ProjectGenerator:
# default env configuration # default env configuration
tpl_vars.update(self.config.items(env=self.env_name, as_dict=True)) tpl_vars.update(self.config.items(env=self.env_name, as_dict=True))
# build data # build data
tpl_vars.update(load_build_metadata(None, self.env_name) or {}) tpl_vars.update(load_build_metadata(self.project_dir, self.env_name) or {})
tpl_vars.update(
{ with fs.cd(self.project_dir):
"src_files": self.get_src_files(), tpl_vars.update(
"project_src_dir": self.config.get("platformio", "src_dir"), {
"project_lib_dir": self.config.get("platformio", "lib_dir"), "src_files": self.get_src_files(),
"project_test_dir": self.config.get("platformio", "test_dir"), "project_src_dir": self.config.get("platformio", "src_dir"),
"project_libdeps_dir": os.path.join( "project_lib_dir": self.config.get("platformio", "lib_dir"),
self.config.get("platformio", "libdeps_dir"), self.env_name "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(): for key, value in tpl_vars.items():
if key.endswith(("_path", "_dir")): if key.endswith(("_path", "_dir")):
@ -130,9 +132,12 @@ class ProjectGenerator:
def get_src_files(self): def get_src_files(self):
result = [] result = []
for root, _, files in os.walk(self.config.get("platformio", "src_dir")): with fs.cd(self.project_dir):
for f in files: for root, _, files in os.walk(self.config.get("platformio", "src_dir")):
result.append(os.path.relpath(os.path.join(os.path.abspath(root), f))) for f in files:
result.append(
os.path.relpath(os.path.join(os.path.abspath(root), f))
)
return result return result
def get_tpls(self): def get_tpls(self):

10
platformio/test/runners/readers/native.py Normal file → Executable file
View File

@ -83,11 +83,15 @@ class NativeTestOutputReader:
# if user changed PROGNAME # if user changed PROGNAME
if not os.path.exists(cmd[0]): if not os.path.exists(cmd[0]):
build_data = load_build_metadata( build_data = load_build_metadata(
None, os.getcwd(),
self.test_runner.test_suite.env_name, self.test_runner.test_suite.env_name,
cache=True, cache=True,
debug=not self.test_runner.options.without_debugging, force_targets=["__test"]
test=True, + (
["__debug"]
if not self.test_runner.options.without_debugging
else []
),
) )
if build_data: if build_data:
cmd[0] = build_data["prog_path"] cmd[0] = build_data["prog_path"]