2017-06-05 16:02:39 +03:00
|
|
|
# Copyright (c) 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
|
|
|
|
2021-05-19 19:43:41 +03:00
|
|
|
import os
|
|
|
|
import shutil
|
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
|
|
|
|
2020-08-23 15:24:31 +03:00
|
|
|
from platformio import __version__, app, exception, fs, telemetry
|
2020-08-22 20:05:14 +03:00
|
|
|
from platformio.cache import cleanup_content_cache
|
2020-08-22 17:48:49 +03:00
|
|
|
from platformio.clients import http
|
2019-05-09 00:51:28 +03:00
|
|
|
from platformio.commands import PlatformioCLI
|
2016-05-26 19:43:36 +03:00
|
|
|
from platformio.commands.platform import platform_update as cmd_platform_update
|
2021-01-24 17:21:22 +02:00
|
|
|
from platformio.commands.system.prune import calculate_unnecessary_system_data
|
2014-11-29 22:55:32 +02:00
|
|
|
from platformio.commands.upgrade import get_latest_version
|
2020-08-13 18:30:04 +03:00
|
|
|
from platformio.package.manager.core import update_core_packages
|
|
|
|
from platformio.package.manager.tool import ToolPackageManager
|
|
|
|
from platformio.package.meta import PackageSpec
|
2020-08-23 15:24:31 +03:00
|
|
|
from platformio.package.version import pepver_to_semver
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
|
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)
|
2017-08-31 23:12:02 +03:00
|
|
|
set_caller(caller)
|
2015-09-03 19:04:09 +03:00
|
|
|
telemetry.on_command()
|
2015-05-25 22:50:46 +03:00
|
|
|
|
2020-12-26 16:10:07 +02:00
|
|
|
if PlatformioCLI.in_silence():
|
|
|
|
return
|
|
|
|
after_upgrade(ctx)
|
|
|
|
|
2016-03-07 18:27:33 +02:00
|
|
|
|
2019-05-09 00:51:28 +03:00
|
|
|
def on_platformio_end(ctx, result): # pylint: disable=unused-argument
|
2019-05-21 13:18:11 +03:00
|
|
|
if PlatformioCLI.in_silence():
|
2016-04-26 00:28:48 +03:00
|
|
|
return
|
|
|
|
|
2015-04-10 21:33:20 +03:00
|
|
|
try:
|
|
|
|
check_platformio_upgrade()
|
2021-01-24 17:21:22 +02:00
|
|
|
check_prune_system()
|
2019-09-23 23:13:48 +03:00
|
|
|
except (
|
2020-08-22 17:48:49 +03:00
|
|
|
http.HTTPClientError,
|
|
|
|
http.InternetIsOffline,
|
2019-09-23 23:13:48 +03:00
|
|
|
exception.GetLatestVersionError,
|
|
|
|
):
|
2016-08-03 23:38:20 +03:00
|
|
|
click.secho(
|
|
|
|
"Failed to check for PlatformIO upgrades. "
|
|
|
|
"Please check your Internet connection.",
|
2019-09-23 23:13:48 +03:00
|
|
|
fg="red",
|
|
|
|
)
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def on_platformio_exception(e):
|
|
|
|
telemetry.on_exception(e)
|
|
|
|
|
|
|
|
|
2017-08-31 23:12:02 +03:00
|
|
|
def set_caller(caller=None):
|
2021-05-19 19:43:41 +03:00
|
|
|
caller = caller or os.getenv("PLATFORMIO_CALLER")
|
2020-11-01 21:05:03 +02:00
|
|
|
if caller:
|
|
|
|
return app.set_session_var("caller_id", caller)
|
2022-01-19 17:16:23 +02:00
|
|
|
if os.getenv("CODESPACES"):
|
|
|
|
caller = "codespaces"
|
|
|
|
elif os.getenv("VSCODE_PID") or os.getenv("VSCODE_NLS_CONFIG"):
|
2020-11-01 21:05:03 +02:00
|
|
|
caller = "vscode"
|
2022-01-19 17:16:23 +02:00
|
|
|
elif os.getenv("GITPOD_WORKSPACE_ID") or os.getenv("GITPOD_WORKSPACE_URL"):
|
2020-11-01 21:05:03 +02:00
|
|
|
caller = "gitpod"
|
2020-11-02 12:27:17 +02:00
|
|
|
return app.set_session_var("caller_id", caller)
|
2017-08-31 23:12:02 +03:00
|
|
|
|
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
class Upgrader(object):
|
|
|
|
def __init__(self, from_version, to_version):
|
2020-08-23 15:24:31 +03:00
|
|
|
self.from_version = pepver_to_semver(from_version)
|
|
|
|
self.to_version = pepver_to_semver(to_version)
|
2014-11-29 22:55:32 +02:00
|
|
|
|
2019-09-23 23:13:48 +03:00
|
|
|
self._upgraders = [
|
2020-08-13 18:30:04 +03:00
|
|
|
(semantic_version.Version("3.5.0-a.2"), self._update_dev_platforms),
|
|
|
|
(semantic_version.Version("4.4.0-a.8"), self._update_pkg_metadata),
|
2019-09-23 23:13:48 +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]
|
2017-05-04 19:06:38 +03:00
|
|
|
for version, callback in self._upgraders:
|
|
|
|
if self.from_version >= version or self.to_version < version:
|
2014-11-29 22:55:32 +02:00
|
|
|
continue
|
2017-05-04 19:06:38 +03:00
|
|
|
result.append(callback(ctx))
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
return all(result)
|
|
|
|
|
2017-05-04 19:06:38 +03:00
|
|
|
@staticmethod
|
|
|
|
def _update_dev_platforms(ctx):
|
|
|
|
ctx.invoke(cmd_platform_update)
|
|
|
|
return True
|
|
|
|
|
2020-08-13 18:30:04 +03:00
|
|
|
@staticmethod
|
|
|
|
def _update_pkg_metadata(_):
|
|
|
|
pm = ToolPackageManager()
|
|
|
|
for pkg in pm.get_installed():
|
|
|
|
if not pkg.metadata or pkg.metadata.spec.external or pkg.metadata.spec.id:
|
|
|
|
continue
|
|
|
|
result = pm.search_registry_packages(PackageSpec(name=pkg.metadata.name))
|
|
|
|
if len(result) != 1:
|
|
|
|
continue
|
|
|
|
result = result[0]
|
|
|
|
pkg.metadata.spec = PackageSpec(
|
2020-09-09 16:27:36 +03:00
|
|
|
id=result["id"],
|
|
|
|
owner=result["owner"]["username"],
|
|
|
|
name=result["name"],
|
2020-08-13 18:30:04 +03:00
|
|
|
)
|
|
|
|
pkg.dump_meta()
|
|
|
|
return True
|
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
|
|
|
|
def after_upgrade(ctx):
|
2021-05-19 19:43:41 +03:00
|
|
|
terminal_width, _ = shutil.get_terminal_size()
|
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__)
|
2020-08-23 15:24:31 +03:00
|
|
|
elif pepver_to_semver(last_version) > pepver_to_semver(__version__):
|
2017-05-27 18:43:56 +03:00
|
|
|
click.secho("*" * terminal_width, fg="yellow")
|
2019-09-23 23:13:48 +03:00
|
|
|
click.secho(
|
|
|
|
"Obsolete PIO Core v%s is used (previous was %s)"
|
|
|
|
% (__version__, last_version),
|
|
|
|
fg="yellow",
|
|
|
|
)
|
|
|
|
click.secho("Please remove multiple PIO Cores from a system:", fg="yellow")
|
2017-05-27 18:43:56 +03:00
|
|
|
click.secho(
|
2018-08-15 19:44:02 +03:00
|
|
|
"https://docs.platformio.org/page/faq.html"
|
2020-09-02 18:13:20 +02:00
|
|
|
"#multiple-platformio-cores-in-a-system",
|
2019-09-23 23:13:48 +03:00
|
|
|
fg="cyan",
|
|
|
|
)
|
2017-05-27 18:43:56 +03:00
|
|
|
click.secho("*" * terminal_width, fg="yellow")
|
|
|
|
return
|
2016-05-26 19:43:36 +03:00
|
|
|
else:
|
2017-03-09 23:24:50 +02:00
|
|
|
click.secho("Please wait while upgrading PlatformIO...", fg="yellow")
|
2017-03-11 23:28:55 +02:00
|
|
|
|
|
|
|
# Update PlatformIO's Core packages
|
2022-03-20 15:40:44 +02:00
|
|
|
cleanup_content_cache("http")
|
|
|
|
update_core_packages()
|
2017-03-11 23:28:55 +02: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__)
|
2019-09-23 23:13:48 +03:00
|
|
|
click.secho(
|
|
|
|
"PlatformIO has been successfully upgraded to %s!\n" % __version__,
|
|
|
|
fg="green",
|
|
|
|
)
|
2019-11-28 16:15:54 +02:00
|
|
|
telemetry.send_event(
|
2019-09-23 23:13:48 +03:00
|
|
|
category="Auto",
|
|
|
|
action="Upgrade",
|
|
|
|
label="%s > %s" % (last_version, __version__),
|
|
|
|
)
|
2016-05-26 19:43:36 +03:00
|
|
|
else:
|
|
|
|
raise exception.UpgradeError("Auto upgrading...")
|
|
|
|
|
|
|
|
# PlatformIO banner
|
2015-04-23 13:09:41 +01:00
|
|
|
click.echo("*" * terminal_width)
|
2019-09-23 23:13:48 +03:00
|
|
|
click.echo("If you like %s, please:" % (click.style("PlatformIO", fg="cyan")))
|
2017-06-05 16:05:05 +03:00
|
|
|
click.echo(
|
2019-09-23 23:13:48 +03:00
|
|
|
"- %s us on Twitter to stay up-to-date "
|
|
|
|
"on the latest project news > %s"
|
|
|
|
% (
|
|
|
|
click.style("follow", fg="cyan"),
|
|
|
|
click.style("https://twitter.com/PlatformIO_Org", fg="cyan"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
click.echo(
|
|
|
|
"- %s it on GitHub > %s"
|
|
|
|
% (
|
|
|
|
click.style("star", fg="cyan"),
|
|
|
|
click.style("https://github.com/platformio/platformio", fg="cyan"),
|
|
|
|
)
|
|
|
|
)
|
2021-05-19 19:43:41 +03:00
|
|
|
if not os.getenv("PLATFORMIO_IDE"):
|
2017-06-05 16:05:05 +03:00
|
|
|
click.echo(
|
2019-11-07 16:49:34 +02:00
|
|
|
"- %s PlatformIO IDE for embedded development > %s"
|
2019-09-23 23:13:48 +03:00
|
|
|
% (
|
|
|
|
click.style("try", fg="cyan"),
|
|
|
|
click.style("https://platformio.org/platformio-ide", 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():
|
|
|
|
interval = int(app.get_setting("check_platformio_interval")) * 3600 * 24
|
2022-02-16 21:53:18 +02:00
|
|
|
check_state = app.get_state_item("last_check", {})
|
|
|
|
last_checked_time = check_state.get("platformio_upgrade", 0)
|
|
|
|
if (time() - interval) < last_checked_time:
|
2014-11-29 22:55:32 +02:00
|
|
|
return
|
|
|
|
|
2022-02-16 21:53:18 +02:00
|
|
|
check_state["platformio_upgrade"] = int(time())
|
|
|
|
app.set_state_item("last_check", check_state)
|
|
|
|
if not last_checked_time:
|
|
|
|
return
|
2014-11-29 22:55:32 +02:00
|
|
|
|
2020-08-22 17:48:49 +03:00
|
|
|
http.ensure_internet_on(raise_exception=True)
|
2017-12-13 18:14:01 +02:00
|
|
|
|
2022-02-16 21:53:18 +02:00
|
|
|
# Update PlatformIO Core packages
|
2022-03-20 15:40:44 +02:00
|
|
|
update_core_packages()
|
2017-12-13 01:31:41 +02:00
|
|
|
|
2015-04-10 21:33:20 +03:00
|
|
|
latest_version = get_latest_version()
|
2020-08-23 15:24:31 +03:00
|
|
|
if pepver_to_semver(latest_version) <= pepver_to_semver(__version__):
|
2014-11-29 22:55:32 +02:00
|
|
|
return
|
|
|
|
|
2021-05-19 19:43:41 +03:00
|
|
|
terminal_width, _ = shutil.get_terminal_size()
|
2015-04-23 13:09:41 +01:00
|
|
|
|
|
|
|
click.echo("")
|
|
|
|
click.echo("*" * terminal_width)
|
2019-09-23 23:13:48 +03:00
|
|
|
click.secho(
|
|
|
|
"There is a new version %s of PlatformIO available.\n"
|
|
|
|
"Please upgrade it via `" % latest_version,
|
|
|
|
fg="yellow",
|
|
|
|
nl=False,
|
|
|
|
)
|
2022-02-16 21:53:18 +02:00
|
|
|
if os.path.join("Cellar", "platformio") in fs.get_source_dir():
|
2016-07-30 19:46:08 +03:00
|
|
|
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)
|
2019-09-23 23:13:48 +03:00
|
|
|
click.secho("https://docs.platformio.org/en/latest/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
|
|
|
|
|
|
|
|
2022-02-16 21:53:18 +02:00
|
|
|
def check_prune_system():
|
|
|
|
interval = 30 * 3600 * 24 # 1 time per month
|
|
|
|
check_state = app.get_state_item("last_check", {})
|
|
|
|
last_checked_time = check_state.get("prune_system", 0)
|
|
|
|
if (time() - interval) < last_checked_time:
|
2014-11-29 22:55:32 +02:00
|
|
|
return
|
|
|
|
|
2022-02-16 21:53:18 +02:00
|
|
|
check_state["prune_system"] = int(time())
|
|
|
|
app.set_state_item("last_check", check_state)
|
2015-04-20 17:20:27 +01:00
|
|
|
|
2022-02-16 21:53:18 +02:00
|
|
|
if not last_checked_time:
|
2021-01-24 17:21:22 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
threshold_mb = int(app.get_setting("check_prune_system_threshold") or 0)
|
|
|
|
if threshold_mb <= 0:
|
|
|
|
return
|
|
|
|
|
2021-03-09 19:26:22 +02:00
|
|
|
unnecessary_size = calculate_unnecessary_system_data()
|
|
|
|
if (unnecessary_size / 1024) < threshold_mb:
|
2021-01-24 17:21:22 +02:00
|
|
|
return
|
|
|
|
|
2021-05-19 19:43:41 +03:00
|
|
|
terminal_width, _ = shutil.get_terminal_size()
|
2021-01-24 17:21:22 +02:00
|
|
|
click.echo()
|
|
|
|
click.echo("*" * terminal_width)
|
|
|
|
click.secho(
|
|
|
|
"We found %s of unnecessary PlatformIO system data (temporary files, "
|
|
|
|
"unnecessary packages, etc.).\nUse `pio system prune --dry-run` to list "
|
|
|
|
"them or `pio system prune` to save disk space."
|
2021-03-09 19:26:22 +02:00
|
|
|
% fs.humanize_file_size(unnecessary_size),
|
2021-01-24 17:21:22 +02:00
|
|
|
fg="yellow",
|
|
|
|
)
|