2016-08-03 22:18:51 +03:00
|
|
|
# Copyright 2014-present PlatformIO <contact@platformio.org>
|
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-11-29 22:55:32 +02:00
|
|
|
|
2016-06-08 13:34:49 +03:00
|
|
|
import json
|
|
|
|
import os
|
2016-05-26 19:43:36 +03:00
|
|
|
from os import getenv
|
2016-06-08 13:34:49 +03:00
|
|
|
from os.path import isdir, join
|
2014-11-29 22:55:32 +02:00
|
|
|
from time import time
|
|
|
|
|
|
|
|
import click
|
2016-06-08 13:34:49 +03:00
|
|
|
import semantic_version
|
2014-11-29 22:55:32 +02:00
|
|
|
|
2016-03-07 18:27:33 +02:00
|
|
|
from platformio import __version__, app, exception, telemetry, util
|
2016-08-02 19:10:29 +03:00
|
|
|
from platformio.commands.lib import lib_update as cmd_lib_update
|
2016-05-26 19:43:36 +03:00
|
|
|
from platformio.commands.platform import \
|
|
|
|
platform_install as cmd_platform_install
|
|
|
|
from platformio.commands.platform import platform_update as cmd_platform_update
|
2014-11-29 22:55:32 +02:00
|
|
|
from platformio.commands.upgrade import get_latest_version
|
2016-08-02 19:10:29 +03:00
|
|
|
from platformio.managers.lib import LibraryManager
|
2016-05-26 19:43:36 +03:00
|
|
|
from platformio.managers.platform import PlatformManager
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
|
2016-04-26 00:28:48 +03:00
|
|
|
def in_silence(ctx):
|
|
|
|
ctx_args = ctx.args or []
|
|
|
|
return (ctx_args and
|
|
|
|
(ctx.args[0] == "upgrade" or "--json-output" in ctx_args))
|
|
|
|
|
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
def on_platformio_start(ctx, force, caller):
|
|
|
|
app.set_session_var("command_ctx", ctx)
|
2015-04-16 17:04:45 +01:00
|
|
|
app.set_session_var("force_option", force)
|
2015-09-03 19:04:09 +03:00
|
|
|
app.set_session_var("caller_id", caller)
|
|
|
|
telemetry.on_command()
|
2015-05-25 22:50:46 +03:00
|
|
|
|
2016-04-26 00:28:48 +03:00
|
|
|
if not in_silence(ctx):
|
|
|
|
after_upgrade(ctx)
|
2015-04-10 21:33:20 +03:00
|
|
|
|
2016-03-07 18:27:33 +02:00
|
|
|
|
|
|
|
def on_platformio_end(ctx, result): # pylint: disable=W0613
|
2016-04-26 00:28:48 +03:00
|
|
|
if in_silence(ctx):
|
|
|
|
return
|
|
|
|
|
2015-04-10 21:33:20 +03:00
|
|
|
try:
|
|
|
|
check_platformio_upgrade()
|
|
|
|
check_internal_updates(ctx, "platforms")
|
|
|
|
check_internal_updates(ctx, "libraries")
|
|
|
|
except (exception.GetLatestVersionError, exception.APIRequestError):
|
|
|
|
click.secho("Failed to check for PlatformIO upgrades. "
|
|
|
|
"Please check your Internet connection.", fg="red")
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def on_platformio_exception(e):
|
|
|
|
telemetry.on_exception(e)
|
|
|
|
|
|
|
|
|
|
|
|
class Upgrader(object):
|
|
|
|
|
|
|
|
def __init__(self, from_version, to_version):
|
2016-08-01 14:25:11 +03:00
|
|
|
self.from_version = semantic_version.Version.coerce(
|
|
|
|
util.pepver_to_semver(from_version))
|
|
|
|
self.to_version = semantic_version.Version.coerce(
|
|
|
|
util.pepver_to_semver(to_version))
|
2014-11-29 22:55:32 +02:00
|
|
|
|
2016-05-26 19:43:36 +03:00
|
|
|
self._upgraders = [
|
2016-06-08 13:34:49 +03:00
|
|
|
(semantic_version.Version("3.0.0"), self._upgrade_to_3_0_0)
|
2016-05-26 19:43:36 +03:00
|
|
|
]
|
2015-02-14 16:21:35 +02:00
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
def run(self, ctx):
|
|
|
|
if self.from_version > self.to_version:
|
|
|
|
return True
|
|
|
|
|
|
|
|
result = [True]
|
2015-02-14 16:21:35 +02:00
|
|
|
for item in self._upgraders:
|
2016-05-26 19:43:36 +03:00
|
|
|
if self.from_version >= item[0] or self.to_version < item[0]:
|
2014-11-29 22:55:32 +02:00
|
|
|
continue
|
2015-02-14 16:21:35 +02:00
|
|
|
result.append(item[1](ctx))
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
return all(result)
|
|
|
|
|
2016-05-26 19:43:36 +03:00
|
|
|
def _upgrade_to_3_0_0(self, ctx): # pylint: disable=R0201
|
2016-06-08 13:34:49 +03:00
|
|
|
# convert custom board configuration
|
|
|
|
boards_dir = join(util.get_home_dir(), "boards")
|
|
|
|
if isdir(boards_dir):
|
|
|
|
for item in os.listdir(boards_dir):
|
|
|
|
if not item.endswith(".json"):
|
|
|
|
continue
|
|
|
|
data = util.load_json(join(boards_dir, item))
|
|
|
|
if set(["name", "url", "vendor"]) <= set(data.keys()):
|
|
|
|
continue
|
|
|
|
os.remove(join(boards_dir, item))
|
|
|
|
for key, value in data.items():
|
|
|
|
with open(join(boards_dir, "%s.json" % key),
|
|
|
|
"w") as f:
|
|
|
|
json.dump(value, f, sort_keys=True, indent=2)
|
|
|
|
|
|
|
|
# re-install PlatformIO 2.0 development platforms
|
2016-05-26 19:43:36 +03:00
|
|
|
installed_platforms = app.get_state_item("installed_platforms", [])
|
2015-02-23 21:25:59 +02:00
|
|
|
if installed_platforms:
|
2016-05-26 19:43:36 +03:00
|
|
|
ctx.invoke(cmd_platform_install, platforms=installed_platforms)
|
2016-06-08 13:34:49 +03:00
|
|
|
|
2015-02-03 18:45:07 +02:00
|
|
|
return True
|
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
def after_upgrade(ctx):
|
2015-02-24 12:47:12 +02:00
|
|
|
last_version = app.get_state_item("last_version", "0.0.0")
|
|
|
|
if last_version == __version__:
|
2014-11-29 22:55:32 +02:00
|
|
|
return
|
|
|
|
|
2016-05-26 19:43:36 +03:00
|
|
|
if last_version == "0.0.0":
|
|
|
|
app.set_state_item("last_version", __version__)
|
|
|
|
else:
|
|
|
|
click.secho("Please wait while upgrading PlatformIO ...",
|
|
|
|
fg="yellow")
|
2015-04-23 13:09:41 +01:00
|
|
|
|
2016-05-26 19:43:36 +03:00
|
|
|
u = Upgrader(last_version, __version__)
|
|
|
|
if u.run(ctx):
|
|
|
|
app.set_state_item("last_version", __version__)
|
2016-05-31 23:43:27 +03:00
|
|
|
|
|
|
|
# patch development platforms
|
|
|
|
pm = PlatformManager()
|
|
|
|
for manifest in pm.get_installed():
|
2016-08-01 17:05:48 +03:00
|
|
|
pm.update(manifest['name'], "^" + manifest['version'])
|
2016-05-26 19:43:36 +03:00
|
|
|
|
|
|
|
click.secho("PlatformIO has been successfully upgraded to %s!\n" %
|
|
|
|
__version__, fg="green")
|
|
|
|
|
|
|
|
telemetry.on_event(category="Auto", action="Upgrade",
|
|
|
|
label="%s > %s" % (last_version, __version__))
|
|
|
|
else:
|
|
|
|
raise exception.UpgradeError("Auto upgrading...")
|
|
|
|
click.echo("")
|
|
|
|
|
|
|
|
# PlatformIO banner
|
|
|
|
terminal_width, _ = click.get_terminal_size()
|
2015-04-23 13:09:41 +01:00
|
|
|
click.echo("*" * terminal_width)
|
|
|
|
click.echo("If you like %s, please:" % (
|
2014-11-29 22:55:32 +02:00
|
|
|
click.style("PlatformIO", fg="cyan")
|
|
|
|
))
|
|
|
|
click.echo(
|
|
|
|
"- %s us on Twitter to stay up-to-date "
|
|
|
|
"on the latest project news > %s" %
|
|
|
|
(click.style("follow", fg="cyan"),
|
2014-12-30 14:02:20 +02:00
|
|
|
click.style("https://twitter.com/PlatformIO_Org", fg="cyan"))
|
2014-11-29 22:55:32 +02:00
|
|
|
)
|
2016-01-29 14:38:06 +02:00
|
|
|
click.echo("- %s it on GitHub > %s" % (
|
2015-12-07 22:23:20 +02:00
|
|
|
click.style("star", fg="cyan"),
|
2015-03-25 12:15:17 +02:00
|
|
|
click.style("https://github.com/platformio/platformio", fg="cyan")
|
2014-11-29 22:55:32 +02:00
|
|
|
))
|
2016-01-29 14:38:06 +02:00
|
|
|
if not getenv("PLATFORMIO_IDE"):
|
|
|
|
click.echo("- %s PlatformIO IDE for IoT development > %s" % (
|
|
|
|
click.style("try", fg="cyan"),
|
2016-03-12 21:32:13 +02:00
|
|
|
click.style("http://platformio.org/platformio-ide", fg="cyan")
|
2016-03-07 18:27:33 +02:00
|
|
|
))
|
|
|
|
if not util.is_ci():
|
|
|
|
click.echo("- %s to keep PlatformIO alive! > %s" % (
|
|
|
|
click.style("donate", fg="cyan"),
|
2016-03-12 21:32:13 +02:00
|
|
|
click.style("http://platformio.org/donate", fg="cyan")
|
2016-01-29 14:38:06 +02:00
|
|
|
))
|
|
|
|
|
2015-04-23 13:09:41 +01:00
|
|
|
click.echo("*" * terminal_width)
|
|
|
|
click.echo("")
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_platformio_upgrade():
|
|
|
|
last_check = app.get_state_item("last_check", {})
|
|
|
|
interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24
|
|
|
|
if (time() - interval) < last_check.get("platformio_upgrade", 0):
|
|
|
|
return
|
|
|
|
|
|
|
|
last_check['platformio_upgrade'] = int(time())
|
|
|
|
app.set_state_item("last_check", last_check)
|
|
|
|
|
2015-04-10 21:33:20 +03:00
|
|
|
latest_version = get_latest_version()
|
2016-08-01 14:25:11 +03:00
|
|
|
if semantic_version.Version.coerce(util.pepver_to_semver(
|
|
|
|
latest_version)) <= semantic_version.Version.coerce(
|
|
|
|
util.pepver_to_semver(__version__)):
|
2014-11-29 22:55:32 +02:00
|
|
|
return
|
|
|
|
|
2015-04-23 13:09:41 +01:00
|
|
|
terminal_width, _ = click.get_terminal_size()
|
|
|
|
|
|
|
|
click.echo("")
|
|
|
|
click.echo("*" * terminal_width)
|
2014-11-29 22:55:32 +02:00
|
|
|
click.secho("There is a new version %s of PlatformIO available.\n"
|
2015-10-09 14:12:30 +01:00
|
|
|
"Please upgrade it via `" % latest_version,
|
2014-11-29 22:55:32 +02:00
|
|
|
fg="yellow", nl=False)
|
2016-02-13 17:22:17 +02:00
|
|
|
if getenv("PLATFORMIO_IDE"):
|
|
|
|
click.secho("PlatformIO IDE Menu: Upgrade PlatformIO",
|
|
|
|
fg="cyan", nl=False)
|
|
|
|
click.secho("`.", fg="yellow")
|
2016-07-30 19:46:08 +03:00
|
|
|
elif join("Cellar", "platformio") in util.get_source_dir():
|
|
|
|
click.secho("brew update && brew upgrade", fg="cyan", nl=False)
|
|
|
|
click.secho("` command.", fg="yellow")
|
2016-02-13 17:22:17 +02:00
|
|
|
else:
|
|
|
|
click.secho("platformio upgrade", fg="cyan", nl=False)
|
|
|
|
click.secho("` or `", fg="yellow", nl=False)
|
|
|
|
click.secho("pip install -U platformio", fg="cyan", nl=False)
|
|
|
|
click.secho("` command.", fg="yellow")
|
|
|
|
click.secho("Changes: ", fg="yellow", nl=False)
|
2016-08-03 00:26:48 +03:00
|
|
|
click.secho("http://docs.platformio.org/en/stable/history.html", fg="cyan")
|
2015-04-23 13:09:41 +01:00
|
|
|
click.echo("*" * terminal_width)
|
|
|
|
click.echo("")
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_internal_updates(ctx, what):
|
|
|
|
last_check = app.get_state_item("last_check", {})
|
|
|
|
interval = int(app.get_setting("check_%s_interval" % what)) * 3600 * 24
|
|
|
|
if (time() - interval) < last_check.get(what + "_update", 0):
|
|
|
|
return
|
|
|
|
|
|
|
|
last_check[what + '_update'] = int(time())
|
|
|
|
app.set_state_item("last_check", last_check)
|
|
|
|
|
2016-08-02 19:10:29 +03:00
|
|
|
pm = PlatformManager() if what == "platforms" else LibraryManager()
|
2014-11-29 22:55:32 +02:00
|
|
|
outdated_items = []
|
2016-08-02 19:10:29 +03:00
|
|
|
for manifest in pm.get_installed():
|
|
|
|
if manifest['name'] not in outdated_items and \
|
|
|
|
pm.is_outdated(manifest['name']):
|
|
|
|
outdated_items.append(manifest['name'])
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
if not outdated_items:
|
|
|
|
return
|
|
|
|
|
2015-04-20 17:20:27 +01:00
|
|
|
terminal_width, _ = click.get_terminal_size()
|
|
|
|
|
|
|
|
click.echo("")
|
|
|
|
click.echo("*" * terminal_width)
|
2014-11-29 22:55:32 +02:00
|
|
|
click.secho("There are the new updates for %s (%s)" %
|
|
|
|
(what, ", ".join(outdated_items)), fg="yellow")
|
|
|
|
|
|
|
|
if not app.get_setting("auto_update_" + what):
|
|
|
|
click.secho("Please update them via ", fg="yellow", nl=False)
|
2015-04-17 12:37:03 +01:00
|
|
|
click.secho("`platformio %s update`" %
|
2016-08-02 19:10:29 +03:00
|
|
|
("lib --global" if what == "libraries" else "platform"),
|
|
|
|
fg="cyan", nl=False)
|
|
|
|
click.secho(" command.\n", fg="yellow")
|
|
|
|
click.secho("If you want to manually check for the new versions "
|
|
|
|
"without updating, please use ", fg="yellow", nl=False)
|
|
|
|
click.secho("`platformio %s update --only-check`" %
|
|
|
|
("lib --global" if what == "libraries" else "platform"),
|
2014-11-29 22:55:32 +02:00
|
|
|
fg="cyan", nl=False)
|
2015-04-20 17:20:27 +01:00
|
|
|
click.secho(" command.", fg="yellow")
|
2014-11-29 22:55:32 +02:00
|
|
|
else:
|
|
|
|
click.secho("Please wait while updating %s ..." % what, fg="yellow")
|
|
|
|
if what == "platforms":
|
2016-08-02 19:10:29 +03:00
|
|
|
ctx.invoke(cmd_platform_update, platforms=outdated_items)
|
2014-11-29 22:55:32 +02:00
|
|
|
elif what == "libraries":
|
2016-08-02 19:10:29 +03:00
|
|
|
ctx.obj = pm
|
|
|
|
ctx.invoke(cmd_lib_update, libraries=outdated_items)
|
2014-11-29 22:55:32 +02:00
|
|
|
click.echo()
|
|
|
|
|
2016-08-02 19:10:29 +03:00
|
|
|
telemetry.on_event(
|
|
|
|
category="Auto", action="Update", label=what.title())
|
2015-04-20 17:20:27 +01:00
|
|
|
|
|
|
|
click.echo("*" * terminal_width)
|
|
|
|
click.echo("")
|