Files
platformio-core/platformio/commands/run.py

74 lines
2.7 KiB
Python
Raw Normal View History

2014-06-03 21:27:36 +03:00
# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.
2014-12-03 20:16:50 +02:00
import click
2014-06-03 21:27:36 +03:00
2014-12-03 20:16:50 +02:00
from platformio import exception, telemetry
from platformio.commands.install import cli as cmd_install
2014-07-30 22:40:11 +03:00
from platformio.platforms.base import PlatformFactory
from platformio.util import get_project_config
2014-06-03 21:27:36 +03:00
2014-12-03 20:16:50 +02:00
@click.command("run", short_help="Process project environments")
@click.option("--environment", "-e", multiple=True, metavar="<environment>")
@click.option("--target", "-t", multiple=True, metavar="<target>")
@click.option("--upload-port", metavar="<upload port>")
@click.pass_context
def cli(ctx, environment, target, upload_port):
2014-06-03 21:27:36 +03:00
config = get_project_config()
if not config.sections():
2014-12-03 20:16:50 +02:00
raise exception.ProjectEnvsNotAvaialable()
2014-07-27 22:48:27 +03:00
unknown = set(environment) - set([s[4:] for s in config.sections()])
if unknown:
2014-12-03 20:16:50 +02:00
raise exception.UnknownEnvNames(", ".join(unknown))
2014-06-03 21:27:36 +03:00
for section in config.sections():
# skip main configuration section
if section == "platformio":
continue
elif section[:4] != "env:":
2014-12-03 20:16:50 +02:00
raise exception.InvalidEnvName(section)
2014-06-03 21:27:36 +03:00
envname = section[4:]
2014-06-03 21:27:36 +03:00
if environment and envname not in environment:
# echo("Skipped %s environment" % style(envname, fg="yellow"))
2014-06-03 21:27:36 +03:00
continue
2014-12-03 20:16:50 +02:00
click.echo("Processing %s environment:" %
click.style(envname, fg="cyan"))
variables = ["PIOENV=" + envname]
if upload_port:
variables.append("UPLOAD_PORT=%s" % upload_port)
for k, v in config.items(section):
k = k.upper()
if k == "TARGETS" or (k == "UPLOAD_PORT" and upload_port):
continue
variables.append("%s=%s" % (k.upper(), v))
2014-06-03 21:27:36 +03:00
envtargets = []
if target:
envtargets = [t for t in target]
elif config.has_option(section, "targets"):
envtargets = config.get(section, "targets").split()
if not config.has_option(section, "platform"):
2014-12-03 20:16:50 +02:00
raise exception.UndefinedEnvPlatform(envname)
platform = config.get(section, "platform")
2014-11-30 17:00:30 +02:00
telemetry.on_run_environment(config.items(section), envtargets)
if (platform not in PlatformFactory.get_platforms(installed=True) and
click.confirm("The platform '%s' has not been installed yet. "
"Would you like to install it now?" % platform)):
ctx.invoke(cmd_install, platforms=[platform])
2014-12-03 20:16:50 +02:00
p = PlatformFactory.newPlatform(platform)
result = p.run(variables, envtargets)
2014-12-03 20:16:50 +02:00
click.secho(result['out'], fg="green")
click.secho(result['err'],
fg="red" if "Error" in result['err'] else "yellow")