2014-06-03 21:27:36 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2015-02-15 00:27:28 +02:00
|
|
|
from datetime import datetime
|
2015-02-14 16:34:00 +02:00
|
|
|
from os.path import getmtime, isdir, join
|
2015-02-14 00:12:16 +02:00
|
|
|
from shutil import rmtree
|
2015-02-15 00:27:28 +02:00
|
|
|
from time import time
|
2015-02-14 00:12:16 +02:00
|
|
|
|
2014-12-03 20:16:50 +02:00
|
|
|
import click
|
2014-06-03 21:27:36 +03:00
|
|
|
|
2015-02-14 00:12:16 +02:00
|
|
|
from platformio import app, exception, telemetry, util
|
2015-05-01 13:49:18 +01:00
|
|
|
from platformio.commands.lib import lib_install as cmd_lib_install
|
2015-04-17 12:37:03 +01:00
|
|
|
from platformio.commands.platforms import \
|
|
|
|
platforms_install as cmd_platforms_install
|
2015-05-01 13:49:18 +01:00
|
|
|
from platformio.libmanager import LibraryManager
|
2014-07-30 22:40:11 +03:00
|
|
|
from platformio.platforms.base import PlatformFactory
|
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
|
|
|
|
2015-02-14 00:12:16 +02:00
|
|
|
config = util.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
|
|
|
|
2015-02-14 00:12:16 +02:00
|
|
|
# remove ".pioenvs" if project config is modified
|
2015-02-14 16:34:00 +02:00
|
|
|
_pioenvs_dir = util.get_pioenvs_dir()
|
|
|
|
if (isdir(_pioenvs_dir) and
|
|
|
|
getmtime(join(util.get_project_dir(), "platformio.ini")) >
|
|
|
|
getmtime(_pioenvs_dir)):
|
|
|
|
rmtree(_pioenvs_dir)
|
2015-02-14 00:12:16 +02:00
|
|
|
|
2015-02-19 22:02:50 +02:00
|
|
|
found_error = False
|
2015-02-16 11:08:07 +02:00
|
|
|
_first_done = False
|
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-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
|
2015-02-15 00:27:28 +02:00
|
|
|
|
2015-02-16 11:08:07 +02:00
|
|
|
if _first_done:
|
2015-02-15 23:53:15 +02:00
|
|
|
click.echo()
|
2015-02-16 11:08:07 +02:00
|
|
|
|
2015-02-19 22:02:50 +02:00
|
|
|
if not process_environment(ctx, envname, options, target, upload_port):
|
|
|
|
found_error = True
|
2015-02-16 11:08:07 +02:00
|
|
|
_first_done = True
|
2015-02-15 23:53:15 +02:00
|
|
|
|
2015-02-19 22:02:50 +02:00
|
|
|
if found_error:
|
|
|
|
raise exception.ReturnErrorCode()
|
|
|
|
|
2014-12-04 23:17:45 +02:00
|
|
|
|
|
|
|
def process_environment(ctx, name, options, targets, upload_port):
|
2015-02-15 00:27:28 +02:00
|
|
|
terminal_width, _ = click.get_terminal_size()
|
|
|
|
start_time = time()
|
|
|
|
|
|
|
|
click.echo("[%s] Processing %s (%s)" % (
|
|
|
|
datetime.now().strftime("%c"),
|
|
|
|
click.style(name, fg="cyan", bold=True),
|
|
|
|
", ".join(["%s: %s" % (k, v) for k, v in options.iteritems()])
|
|
|
|
))
|
|
|
|
click.secho("-" * terminal_width, bold=True)
|
|
|
|
|
|
|
|
result = _run_environment(ctx, name, options, targets, upload_port)
|
|
|
|
|
2015-02-19 22:02:50 +02:00
|
|
|
is_error = result['returncode'] != 0
|
2015-02-15 00:27:28 +02:00
|
|
|
summary_text = " Took %.2f seconds " % (time() - start_time)
|
|
|
|
half_line = "=" * ((terminal_width - len(summary_text) - 10) / 2)
|
|
|
|
click.echo("%s [%s]%s%s" % (
|
|
|
|
half_line,
|
|
|
|
(click.style(" ERROR ", fg="red", bold=True)
|
|
|
|
if is_error else click.style("SUCCESS", fg="green", bold=True)),
|
|
|
|
summary_text,
|
|
|
|
half_line
|
|
|
|
), err=is_error)
|
|
|
|
|
2015-02-19 22:02:50 +02:00
|
|
|
return not is_error
|
|
|
|
|
2015-02-15 00:27:28 +02:00
|
|
|
|
|
|
|
def _run_environment(ctx, name, options, targets, upload_port):
|
2014-12-04 23:17:45 +02:00
|
|
|
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)
|
|
|
|
|
2015-05-01 13:49:18 +01:00
|
|
|
# install platform and libs dependencies
|
|
|
|
_autoinstall_env_platform(ctx, platform)
|
|
|
|
if "install_libs" in options:
|
|
|
|
_autoinstall_env_libs(ctx, options['install_libs'])
|
|
|
|
|
|
|
|
p = PlatformFactory.newPlatform(platform)
|
|
|
|
return p.run(variables, envtargets)
|
|
|
|
|
|
|
|
|
|
|
|
def _autoinstall_env_platform(ctx, platform):
|
2014-12-29 15:10:31 +02:00
|
|
|
installed_platforms = PlatformFactory.get_platforms(
|
|
|
|
installed=True).keys()
|
|
|
|
if (platform not in installed_platforms and (
|
|
|
|
not app.get_setting("enable_prompts") or
|
2014-12-04 23:17:45 +02:00
|
|
|
click.confirm("The platform '%s' has not been installed yet. "
|
2014-12-29 15:10:31 +02:00
|
|
|
"Would you like to install it now?" % platform))):
|
2015-04-17 12:37:03 +01:00
|
|
|
ctx.invoke(cmd_platforms_install, platforms=[platform])
|
2014-12-04 23:17:45 +02:00
|
|
|
|
2015-05-01 13:49:18 +01:00
|
|
|
|
|
|
|
def _autoinstall_env_libs(ctx, libids_list):
|
|
|
|
require_libs = [int(l.strip()) for l in libids_list.split(",")]
|
|
|
|
installed_libs = [
|
|
|
|
l['id'] for l in LibraryManager().get_installed().values()
|
|
|
|
]
|
|
|
|
|
|
|
|
not_intalled_libs = set(require_libs) - set(installed_libs)
|
|
|
|
if not require_libs or not not_intalled_libs:
|
|
|
|
return
|
|
|
|
|
|
|
|
if (not app.get_setting("enable_prompts") or
|
|
|
|
click.confirm(
|
|
|
|
"The libraries with IDs '%s' have not been installed yet. "
|
|
|
|
"Would you like to install them now?" %
|
|
|
|
", ".join([str(i) for i in not_intalled_libs])
|
|
|
|
)):
|
|
|
|
ctx.invoke(cmd_lib_install, libid=not_intalled_libs)
|