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
|
2014-06-07 13:34:31 +03:00
|
|
|
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-07 13:34:31 +03:00
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
config = get_project_config()
|
2014-06-12 21:17:45 +03:00
|
|
|
|
|
|
|
if not config.sections():
|
2014-12-04 23:17:45 +02:00
|
|
|
raise exception.ProjectEnvsNotAvailable()
|
2014-06-12 21:17:45 +03:00
|
|
|
|
2014-07-27 22:48:27 +03:00
|
|
|
unknown = set(environment) - set([s[4:] for s in config.sections()])
|
2014-07-27 22:39:41 +03:00
|
|
|
if unknown:
|
2014-12-03 20:16:50 +02:00
|
|
|
raise exception.UnknownEnvNames(", ".join(unknown))
|
2014-07-27 22:39:41 +03:00
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
for section in config.sections():
|
2014-10-04 18:53:59 +03:00
|
|
|
# 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
|
|
|
|
2014-06-07 13:34:31 +03:00
|
|
|
envname = section[4:]
|
2014-06-03 21:27:36 +03:00
|
|
|
if environment and envname not in environment:
|
2014-07-27 22:39:41 +03:00
|
|
|
# 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"))
|
2014-07-27 22:39:41 +03:00
|
|
|
|
2014-12-04 23:17:45 +02:00
|
|
|
options = {}
|
2014-07-27 22:39:41 +03:00
|
|
|
for k, v in config.items(section):
|
2014-12-04 23:17:45 +02:00
|
|
|
options[k] = v
|
|
|
|
process_environment(ctx, envname, options, target, upload_port)
|
|
|
|
|
|
|
|
|
|
|
|
def process_environment(ctx, name, options, targets, upload_port):
|
|
|
|
variables = ["PIOENV=" + name]
|
|
|
|
if upload_port:
|
|
|
|
variables.append("UPLOAD_PORT=%s" % upload_port)
|
|
|
|
for k, v in options.items():
|
|
|
|
k = k.upper()
|
|
|
|
if k == "TARGETS" or (k == "UPLOAD_PORT" and upload_port):
|
|
|
|
continue
|
|
|
|
variables.append("%s=%s" % (k.upper(), v))
|
|
|
|
|
|
|
|
envtargets = []
|
|
|
|
if targets:
|
|
|
|
envtargets = [t for t in targets]
|
|
|
|
elif "targets" in options:
|
|
|
|
envtargets = options['targets'].split()
|
|
|
|
|
|
|
|
if "platform" not in options:
|
|
|
|
raise exception.UndefinedEnvPlatform(name)
|
|
|
|
platform = options['platform']
|
|
|
|
|
|
|
|
telemetry.on_run_environment(options, 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])
|
|
|
|
|
|
|
|
p = PlatformFactory.newPlatform(platform)
|
|
|
|
result = p.run(variables, envtargets)
|
|
|
|
click.secho(result['out'], fg="green")
|
|
|
|
click.secho(result['err'],
|
|
|
|
fg="red" if "Error" in result['err'] else "yellow")
|