mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
Switch to the new ProjectConfig API
This commit is contained in:
@ -16,13 +16,14 @@
|
|||||||
# pylint: disable=too-many-locals, too-many-branches
|
# pylint: disable=too-many-locals, too-many-branches
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from os.path import isfile
|
from os.path import isfile, join
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from platformio import exception, util
|
from platformio import exception, util
|
||||||
from platformio.commands.debug import helpers
|
from platformio.commands.debug import helpers
|
||||||
from platformio.managers.core import inject_contrib_pysite
|
from platformio.managers.core import inject_contrib_pysite
|
||||||
|
from platformio.project.config import ProjectConfig
|
||||||
|
|
||||||
|
|
||||||
@click.command(
|
@click.command(
|
||||||
@ -69,10 +70,12 @@ def cli(ctx, project_dir, project_conf, environment, verbose, interface,
|
|||||||
project_dir = os.getenv("CWD")
|
project_dir = os.getenv("CWD")
|
||||||
|
|
||||||
with util.cd(project_dir):
|
with util.cd(project_dir):
|
||||||
env_name = helpers.check_env_name(project_conf or project_dir,
|
config = ProjectConfig.get_instance(
|
||||||
environment)
|
project_conf or join(project_dir, "platformio.ini"))
|
||||||
env_options = helpers.get_env_options(project_conf or project_dir,
|
config.validate(envs=[environment] if environment else None)
|
||||||
env_name)
|
|
||||||
|
env_name = environment or helpers.get_default_debug_env(config)
|
||||||
|
env_options = config.items(env=env_name, as_dict=True)
|
||||||
if not set(env_options.keys()) >= set(["platform", "board"]):
|
if not set(env_options.keys()) >= set(["platform", "board"]):
|
||||||
raise exception.ProjectEnvsNotAvailable()
|
raise exception.ProjectEnvsNotAvailable()
|
||||||
debug_options = helpers.validate_debug_options(ctx, env_options)
|
debug_options = helpers.validate_debug_options(ctx, env_options)
|
||||||
|
@ -46,29 +46,9 @@ def escape_path(path):
|
|||||||
return path.replace("\\", "/")
|
return path.replace("\\", "/")
|
||||||
|
|
||||||
|
|
||||||
def check_env_name(project_conf, environment):
|
def get_default_debug_env(config):
|
||||||
config = util.load_project_config(project_conf)
|
default_envs = config.default_envs()
|
||||||
envs = []
|
return default_envs[0] if default_envs else config.envs()[0]
|
||||||
for section in config.sections():
|
|
||||||
if section.startswith("env:"):
|
|
||||||
envs.append(section[4:])
|
|
||||||
if not envs:
|
|
||||||
raise exception.ProjectEnvsNotAvailable()
|
|
||||||
if not environment and config.has_option("platformio", "env_default"):
|
|
||||||
environment = config.get("platformio", "env_default").split(", ")[0]
|
|
||||||
if environment:
|
|
||||||
if environment in envs:
|
|
||||||
return environment
|
|
||||||
raise exception.UnknownEnvNames(environment, envs)
|
|
||||||
return envs[0]
|
|
||||||
|
|
||||||
|
|
||||||
def get_env_options(project_conf, environment):
|
|
||||||
config = util.load_project_config(project_conf)
|
|
||||||
options = {}
|
|
||||||
for k, v in config.items("env:%s" % environment):
|
|
||||||
options[k] = v
|
|
||||||
return options
|
|
||||||
|
|
||||||
|
|
||||||
def validate_debug_options(cmd_ctx, env_options):
|
def validate_debug_options(cmd_ctx, env_options):
|
||||||
|
@ -28,11 +28,7 @@ from platformio.commands.home.rpc.handlers.app import AppRPC
|
|||||||
from platformio.commands.home.rpc.handlers.piocore import PIOCoreRPC
|
from platformio.commands.home.rpc.handlers.piocore import PIOCoreRPC
|
||||||
from platformio.ide.projectgenerator import ProjectGenerator
|
from platformio.ide.projectgenerator import ProjectGenerator
|
||||||
from platformio.managers.platform import PlatformManager
|
from platformio.managers.platform import PlatformManager
|
||||||
|
from platformio.project.config import ProjectConfig
|
||||||
try:
|
|
||||||
from configparser import Error as ConfigParserError
|
|
||||||
except ImportError:
|
|
||||||
from ConfigParser import Error as ConfigParserError
|
|
||||||
|
|
||||||
|
|
||||||
class ProjectRPC(object):
|
class ProjectRPC(object):
|
||||||
@ -42,7 +38,8 @@ class ProjectRPC(object):
|
|||||||
|
|
||||||
def _get_project_data(project_dir):
|
def _get_project_data(project_dir):
|
||||||
data = {"boards": [], "libExtraDirs": []}
|
data = {"boards": [], "libExtraDirs": []}
|
||||||
config = util.load_project_config(project_dir)
|
config = ProjectConfig(join(project_dir, "platformio.ini"))
|
||||||
|
config.validate(validate_options=False)
|
||||||
|
|
||||||
if config.has_section("platformio") and \
|
if config.has_section("platformio") and \
|
||||||
config.has_option("platformio", "lib_extra_dirs"):
|
config.has_option("platformio", "lib_extra_dirs"):
|
||||||
@ -87,10 +84,8 @@ class ProjectRPC(object):
|
|||||||
boards = []
|
boards = []
|
||||||
try:
|
try:
|
||||||
data = _get_project_data(project_dir)
|
data = _get_project_data(project_dir)
|
||||||
except exception.NotPlatformIOProject:
|
except exception.PlatformIOProjectException:
|
||||||
continue
|
continue
|
||||||
except ConfigParserError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
for board_id in data.get("boards", []):
|
for board_id in data.get("boards", []):
|
||||||
name = board_id
|
name = board_id
|
||||||
@ -235,13 +230,13 @@ class ProjectRPC(object):
|
|||||||
for project_dir, _, __ in os.walk(examples_dir):
|
for project_dir, _, __ in os.walk(examples_dir):
|
||||||
project_description = None
|
project_description = None
|
||||||
try:
|
try:
|
||||||
config = util.load_project_config(project_dir)
|
config = ProjectConfig(join(project_dir, "platformio.ini"))
|
||||||
|
config.validate(validate_options=False)
|
||||||
if config.has_section("platformio") and \
|
if config.has_section("platformio") and \
|
||||||
config.has_option("platformio", "description"):
|
config.has_option("platformio", "description"):
|
||||||
project_description = config.get(
|
project_description = config.get(
|
||||||
"platformio", "description")
|
"platformio", "description")
|
||||||
except (exception.NotPlatformIOProject,
|
except exception.PlatformIOProjectException:
|
||||||
exception.InvalidProjectConf):
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
path_tokens = project_dir.split(sep)
|
path_tokens = project_dir.split(sep)
|
||||||
|
@ -22,9 +22,10 @@ from time import time
|
|||||||
import click
|
import click
|
||||||
|
|
||||||
from platformio import exception, util
|
from platformio import exception, util
|
||||||
from platformio.commands.run import check_project_envs, print_header
|
from platformio.commands.run import print_header
|
||||||
from platformio.commands.test.embedded import EmbeddedTestProcessor
|
from platformio.commands.test.embedded import EmbeddedTestProcessor
|
||||||
from platformio.commands.test.native import NativeTestProcessor
|
from platformio.commands.test.native import NativeTestProcessor
|
||||||
|
from platformio.project.config import ProjectConfig
|
||||||
|
|
||||||
|
|
||||||
@click.command("test", short_help="Unit Testing")
|
@click.command("test", short_help="Unit Testing")
|
||||||
@ -87,39 +88,32 @@ def cli( # pylint: disable=redefined-builtin
|
|||||||
if not isdir(test_dir):
|
if not isdir(test_dir):
|
||||||
raise exception.TestDirNotExists(test_dir)
|
raise exception.TestDirNotExists(test_dir)
|
||||||
test_names = get_test_names(test_dir)
|
test_names = get_test_names(test_dir)
|
||||||
projectconf = util.load_project_config(project_conf)
|
|
||||||
env_default = None
|
config = ProjectConfig.get_instance(
|
||||||
if projectconf.has_option("platformio", "env_default"):
|
project_conf or join(project_dir, "platformio.ini"))
|
||||||
env_default = util.parse_conf_multi_values(
|
config.validate(envs=environment)
|
||||||
projectconf.get("platformio", "env_default"))
|
|
||||||
assert check_project_envs(projectconf, environment or env_default)
|
|
||||||
|
|
||||||
click.echo("Verbose mode can be enabled via `-v, --verbose` option")
|
click.echo("Verbose mode can be enabled via `-v, --verbose` option")
|
||||||
click.echo("Collected %d items" % len(test_names))
|
click.echo("Collected %d items" % len(test_names))
|
||||||
|
|
||||||
start_time = time()
|
|
||||||
results = []
|
results = []
|
||||||
|
start_time = time()
|
||||||
|
default_envs = config.default_envs()
|
||||||
for testname in test_names:
|
for testname in test_names:
|
||||||
for section in projectconf.sections():
|
for envname in config.envs():
|
||||||
if not section.startswith("env:"):
|
section = "env:%s" % envname
|
||||||
continue
|
|
||||||
|
|
||||||
# filter and ignore patterns
|
# filter and ignore patterns
|
||||||
patterns = dict(filter=list(filter), ignore=list(ignore))
|
patterns = dict(filter=list(filter), ignore=list(ignore))
|
||||||
for key in patterns:
|
for key in patterns:
|
||||||
if projectconf.has_option(section, "test_%s" % key):
|
if config.has_option(section, "test_%s" % key):
|
||||||
patterns[key].extend([
|
patterns[key].extend(
|
||||||
p.strip()
|
config.getlist(section, "test_%s" % key))
|
||||||
for p in projectconf.get(section, "test_%s" %
|
|
||||||
key).split(", ")
|
|
||||||
if p.strip()
|
|
||||||
])
|
|
||||||
|
|
||||||
envname = section[4:]
|
|
||||||
skip_conditions = [
|
skip_conditions = [
|
||||||
environment and envname not in environment,
|
environment and envname not in environment,
|
||||||
not environment and env_default
|
not environment and default_envs
|
||||||
and envname not in env_default,
|
and envname not in default_envs,
|
||||||
testname != "*" and patterns['filter'] and
|
testname != "*" and patterns['filter'] and
|
||||||
not any([fnmatch(testname, p)
|
not any([fnmatch(testname, p)
|
||||||
for p in patterns['filter']]),
|
for p in patterns['filter']]),
|
||||||
@ -131,13 +125,13 @@ def cli( # pylint: disable=redefined-builtin
|
|||||||
results.append((None, testname, envname))
|
results.append((None, testname, envname))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
cls = (NativeTestProcessor if projectconf.get(
|
cls = (NativeTestProcessor
|
||||||
section, "platform") == "native" else
|
if config.get(section, "platform") == "native" else
|
||||||
EmbeddedTestProcessor)
|
EmbeddedTestProcessor)
|
||||||
tp = cls(
|
tp = cls(
|
||||||
ctx, testname, envname,
|
ctx, testname, envname,
|
||||||
dict(
|
dict(
|
||||||
project_config=projectconf,
|
project_config=config,
|
||||||
project_dir=project_dir,
|
project_dir=project_dir,
|
||||||
upload_port=upload_port,
|
upload_port=upload_port,
|
||||||
test_port=test_port,
|
test_port=test_port,
|
||||||
|
@ -85,10 +85,8 @@ class TestProcessorBase(object):
|
|||||||
self.test_name = testname
|
self.test_name = testname
|
||||||
self.options = options
|
self.options = options
|
||||||
self.env_name = envname
|
self.env_name = envname
|
||||||
self.env_options = {
|
self.env_options = options['project_config'].items(
|
||||||
k: v
|
env=envname, as_dict=True)
|
||||||
for k, v in options['project_config'].items("env:" + envname)
|
|
||||||
}
|
|
||||||
self._run_failed = False
|
self._run_failed = False
|
||||||
self._outputcpp_generated = False
|
self._outputcpp_generated = False
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user