2016-01-01 20:51:48 +02:00
|
|
|
# Copyright 2014-2016 Ivan Kravets <me@ikravets.com>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-06-03 21:27:36 +03:00
|
|
|
|
2015-02-15 00:27:28 +02:00
|
|
|
from datetime import datetime
|
2015-06-04 22:50:13 +03:00
|
|
|
from hashlib import sha1
|
|
|
|
from os import getcwd, makedirs, walk
|
|
|
|
from os.path import getmtime, isdir, isfile, 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
|
|
|
|
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>")
|
2015-09-04 18:28:13 +03:00
|
|
|
@click.option("--project-dir", "-d", default=getcwd,
|
2015-05-06 17:29:58 +01:00
|
|
|
type=click.Path(exists=True, file_okay=False, dir_okay=True,
|
|
|
|
writable=True, resolve_path=True))
|
2015-05-15 22:40:29 +02:00
|
|
|
@click.option("--verbose", "-v", count=True, default=3)
|
2015-07-21 14:53:38 +03:00
|
|
|
@click.option("--disable-auto-clean", is_flag=True)
|
2014-12-03 20:16:50 +02:00
|
|
|
@click.pass_context
|
2015-05-15 22:40:29 +02:00
|
|
|
def cli(ctx, environment, target, upload_port, # pylint: disable=R0913,R0914
|
2015-07-21 14:53:38 +03:00
|
|
|
project_dir, verbose, disable_auto_clean):
|
2015-05-25 23:26:35 +03:00
|
|
|
with util.cd(project_dir):
|
2015-05-06 17:29:58 +01:00
|
|
|
config = util.get_project_config()
|
|
|
|
|
|
|
|
if not config.sections():
|
|
|
|
raise exception.ProjectEnvsNotAvailable()
|
|
|
|
|
2015-10-03 15:38:33 +01:00
|
|
|
known = set([s[4:] for s in config.sections()
|
|
|
|
if s.startswith("env:")])
|
|
|
|
unknown = set(environment) - known
|
2015-05-06 17:29:58 +01:00
|
|
|
if unknown:
|
2015-10-03 15:38:33 +01:00
|
|
|
raise exception.UnknownEnvNames(
|
|
|
|
", ".join(unknown), ", ".join(known))
|
2015-05-06 17:29:58 +01:00
|
|
|
|
2015-06-04 22:50:13 +03:00
|
|
|
# clean obsolete .pioenvs dir
|
2015-07-21 14:53:38 +03:00
|
|
|
if not disable_auto_clean:
|
2015-10-06 17:06:47 +01:00
|
|
|
try:
|
|
|
|
_clean_pioenvs_dir(util.get_pioenvs_dir())
|
2016-01-28 00:56:25 +02:00
|
|
|
except: # pylint: disable=bare-except
|
2016-01-28 00:37:16 +02:00
|
|
|
click.secho(
|
|
|
|
"Can not remove temporary directory `%s`. Please remove "
|
|
|
|
"`.pioenvs` directory from the project manually to avoid "
|
|
|
|
"build issues" % util.get_pioenvs_dir(),
|
|
|
|
fg="yellow"
|
|
|
|
)
|
2015-05-06 17:29:58 +01:00
|
|
|
|
2015-05-06 18:07:17 +01:00
|
|
|
results = []
|
2015-05-06 17:29:58 +01:00
|
|
|
for section in config.sections():
|
2015-05-15 22:40:29 +02:00
|
|
|
# skip main configuration section
|
|
|
|
if section == "platformio":
|
|
|
|
continue
|
2015-05-06 18:07:17 +01:00
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
if not section.startswith("env:"):
|
|
|
|
raise exception.InvalidEnvName(section)
|
2015-05-06 18:07:17 +01:00
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
envname = section[4:]
|
|
|
|
if environment and envname not in environment:
|
|
|
|
# echo("Skipped %s environment" % style(envname, fg="yellow"))
|
|
|
|
continue
|
2015-02-15 00:27:28 +02:00
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
if results:
|
|
|
|
click.echo()
|
2014-12-04 23:17:45 +02:00
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
options = {}
|
|
|
|
for k, v in config.items(section):
|
|
|
|
options[k] = v
|
2014-12-04 23:17:45 +02:00
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
ep = EnvironmentProcessor(
|
|
|
|
ctx, envname, options, target, upload_port, verbose)
|
|
|
|
results.append(ep.process())
|
2015-05-01 13:49:18 +01:00
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
if not all(results):
|
|
|
|
raise exception.ReturnErrorCode()
|
2015-05-01 13:49:18 +01:00
|
|
|
|
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
class EnvironmentProcessor(object):
|
|
|
|
|
2015-06-28 19:20:31 +03:00
|
|
|
RENAMED_OPTIONS = {
|
2015-06-28 20:16:43 +03:00
|
|
|
"INSTALL_LIBS": "LIB_INSTALL",
|
2015-06-28 19:44:35 +03:00
|
|
|
"IGNORE_LIBS": "LIB_IGNORE",
|
2015-06-28 20:16:43 +03:00
|
|
|
"USE_LIBS": "LIB_USE",
|
2015-06-28 20:18:16 +03:00
|
|
|
"LDF_CYCLIC": "LIB_DFCYCLIC",
|
|
|
|
"SRCBUILD_FLAGS": "SRC_BUILD_FLAGS"
|
2015-06-28 19:20:31 +03:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:40:29 +02:00
|
|
|
def __init__(self, cmd_ctx, name, options, # pylint: disable=R0913
|
|
|
|
targets, upload_port, verbose):
|
|
|
|
self.cmd_ctx = cmd_ctx
|
|
|
|
self.name = name
|
2015-06-28 20:16:43 +03:00
|
|
|
self.options = self._validate_options(options)
|
2015-05-15 22:40:29 +02:00
|
|
|
self.targets = targets
|
|
|
|
self.upload_port = upload_port
|
|
|
|
self.verbose_level = int(verbose)
|
|
|
|
|
|
|
|
def process(self):
|
|
|
|
terminal_width, _ = click.get_terminal_size()
|
|
|
|
start_time = time()
|
|
|
|
|
|
|
|
click.echo("[%s] Processing %s (%s)" % (
|
|
|
|
datetime.now().strftime("%c"),
|
|
|
|
click.style(self.name, fg="cyan", bold=True),
|
|
|
|
", ".join(["%s: %s" % (k, v) for k, v in self.options.iteritems()])
|
|
|
|
))
|
|
|
|
click.secho("-" * terminal_width, bold=True)
|
|
|
|
|
|
|
|
result = self._run()
|
|
|
|
|
|
|
|
is_error = result['returncode'] != 0
|
|
|
|
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)
|
|
|
|
|
|
|
|
return not is_error
|
|
|
|
|
2015-06-28 20:16:43 +03:00
|
|
|
def _validate_options(self, options):
|
|
|
|
result = {}
|
|
|
|
for k, v in options.items():
|
|
|
|
_k = k.upper()
|
2015-06-28 19:20:31 +03:00
|
|
|
# process obsolete options
|
2015-06-28 20:16:43 +03:00
|
|
|
if _k in self.RENAMED_OPTIONS:
|
2015-06-28 19:20:31 +03:00
|
|
|
click.secho(
|
2015-06-28 20:16:43 +03:00
|
|
|
"Warning! `%s` option is deprecated and will be "
|
2015-06-28 19:20:31 +03:00
|
|
|
"removed in the next release! Please use "
|
|
|
|
"`%s` instead." % (
|
2015-06-28 20:16:43 +03:00
|
|
|
k, self.RENAMED_OPTIONS[_k].lower()),
|
2015-06-28 19:20:31 +03:00
|
|
|
fg="yellow"
|
|
|
|
)
|
2015-06-28 20:16:43 +03:00
|
|
|
k = self.RENAMED_OPTIONS[_k].lower()
|
|
|
|
result[k] = v
|
|
|
|
return result
|
2015-06-28 19:20:31 +03:00
|
|
|
|
2015-06-28 20:16:43 +03:00
|
|
|
def _get_build_variables(self):
|
|
|
|
variables = ["PIOENV=" + self.name]
|
|
|
|
if self.upload_port:
|
|
|
|
variables.append("UPLOAD_PORT=%s" % self.upload_port)
|
|
|
|
for k, v in self.options.items():
|
|
|
|
k = k.upper()
|
2015-05-15 22:40:29 +02:00
|
|
|
if k == "TARGETS" or (k == "UPLOAD_PORT" and self.upload_port):
|
|
|
|
continue
|
2015-06-28 19:20:31 +03:00
|
|
|
variables.append("%s=%s" % (k, v))
|
2015-05-15 22:40:29 +02:00
|
|
|
return variables
|
|
|
|
|
|
|
|
def _get_build_targets(self):
|
|
|
|
targets = []
|
|
|
|
if self.targets:
|
|
|
|
targets = [t for t in self.targets]
|
|
|
|
elif "targets" in self.options:
|
|
|
|
targets = self.options['targets'].split()
|
|
|
|
return targets
|
|
|
|
|
|
|
|
def _run(self):
|
|
|
|
if "platform" not in self.options:
|
|
|
|
raise exception.UndefinedEnvPlatform(self.name)
|
|
|
|
|
|
|
|
platform = self.options['platform']
|
|
|
|
build_vars = self._get_build_variables()
|
|
|
|
build_targets = self._get_build_targets()
|
|
|
|
|
|
|
|
telemetry.on_run_environment(self.options, build_targets)
|
|
|
|
|
2015-12-15 15:58:52 +02:00
|
|
|
# install dependent libraries
|
2015-06-28 20:16:43 +03:00
|
|
|
if "lib_install" in self.options:
|
|
|
|
_autoinstall_libs(self.cmd_ctx, self.options['lib_install'])
|
2015-05-15 22:40:29 +02:00
|
|
|
|
|
|
|
p = PlatformFactory.newPlatform(platform)
|
|
|
|
return p.run(build_vars, build_targets, self.verbose_level)
|
|
|
|
|
|
|
|
|
|
|
|
def _autoinstall_libs(ctx, libids_list):
|
2015-05-01 13:49:18 +01:00
|
|
|
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?" %
|
2016-01-01 21:15:23 +02:00
|
|
|
", ".join([str(i) for i in not_intalled_libs]))):
|
2015-05-01 13:49:18 +01:00
|
|
|
ctx.invoke(cmd_lib_install, libid=not_intalled_libs)
|
2015-06-04 22:50:13 +03:00
|
|
|
|
|
|
|
|
2015-10-06 17:06:47 +01:00
|
|
|
def _clean_pioenvs_dir(pioenvs_dir):
|
2015-06-04 23:15:28 +03:00
|
|
|
structhash_file = join(pioenvs_dir, "structure.hash")
|
|
|
|
proj_hash = calculate_project_hash()
|
|
|
|
|
|
|
|
# if project's config is modified
|
2015-06-05 19:12:57 +03:00
|
|
|
if (isdir(pioenvs_dir) and
|
|
|
|
getmtime(join(util.get_project_dir(), "platformio.ini")) >
|
|
|
|
getmtime(pioenvs_dir)):
|
2015-06-05 19:30:49 +03:00
|
|
|
rmtree(pioenvs_dir)
|
2015-06-04 23:15:28 +03:00
|
|
|
|
|
|
|
# check project structure
|
2015-06-05 19:12:57 +03:00
|
|
|
if isdir(pioenvs_dir) and isfile(structhash_file):
|
|
|
|
with open(structhash_file) as f:
|
|
|
|
if f.read() == proj_hash:
|
|
|
|
return
|
|
|
|
rmtree(pioenvs_dir)
|
2015-06-04 23:15:28 +03:00
|
|
|
|
|
|
|
if not isdir(pioenvs_dir):
|
|
|
|
makedirs(pioenvs_dir)
|
|
|
|
|
|
|
|
with open(structhash_file, "w") as f:
|
|
|
|
f.write(proj_hash)
|
2015-06-04 22:50:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def calculate_project_hash():
|
|
|
|
structure = []
|
|
|
|
for d in (util.get_projectsrc_dir(), util.get_projectlib_dir()):
|
|
|
|
if not isdir(d):
|
|
|
|
continue
|
|
|
|
for root, _, files in walk(d):
|
|
|
|
for f in files:
|
|
|
|
path = join(root, f)
|
2016-04-27 00:00:40 +03:00
|
|
|
if not any([s in path for s in (".git", ".svn", ".pioenvs")]):
|
2015-06-04 22:50:13 +03:00
|
|
|
structure.append(path)
|
|
|
|
return sha1(",".join(sorted(structure))).hexdigest() if structure else ""
|