2014-08-03 18:40:20 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2014-12-03 20:16:50 +02:00
|
|
|
import click
|
|
|
|
import requests
|
2014-08-03 18:40:20 +03:00
|
|
|
|
2015-09-02 13:42:01 +03:00
|
|
|
from platformio import __version__, exception, util
|
2014-08-03 18:40:20 +03:00
|
|
|
|
|
|
|
|
2014-12-03 20:16:50 +02:00
|
|
|
@click.command("upgrade",
|
|
|
|
short_help="Upgrade PlatformIO to the latest version")
|
2014-08-03 18:40:20 +03:00
|
|
|
def cli():
|
2015-02-13 23:55:08 +02:00
|
|
|
last = get_latest_version()
|
2014-08-03 18:40:20 +03:00
|
|
|
if __version__ == last:
|
2014-12-03 20:16:50 +02:00
|
|
|
return click.secho(
|
|
|
|
"You're up-to-date!\nPlatformIO %s is currently the "
|
|
|
|
"newest version available." % __version__, fg="green"
|
|
|
|
)
|
2014-08-03 18:40:20 +03:00
|
|
|
else:
|
2015-01-06 17:45:07 +02:00
|
|
|
click.secho("Please wait while upgrading PlatformIO ...",
|
|
|
|
fg="yellow")
|
|
|
|
|
2015-09-02 13:42:01 +03:00
|
|
|
cmds = (
|
2015-09-09 01:28:43 +03:00
|
|
|
["pip", "install", "--upgrade", "pip", "setuptools", "virtualenv"],
|
2015-09-02 13:42:01 +03:00
|
|
|
["pip", "install", "--upgrade", "platformio"],
|
|
|
|
["platformio", "--version"]
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd = None
|
|
|
|
r = None
|
|
|
|
try:
|
|
|
|
for cmd in cmds:
|
|
|
|
r = None
|
|
|
|
r = util.exec_command(cmd)
|
|
|
|
assert r['returncode'] == 0
|
|
|
|
assert last in r['out'].strip()
|
|
|
|
click.secho(
|
|
|
|
"PlatformIO has been successfully upgraded to %s" % last,
|
|
|
|
fg="green")
|
|
|
|
click.echo("Release notes: ", nl=False)
|
|
|
|
click.secho("http://docs.platformio.org/en/latest/history.html",
|
|
|
|
fg="cyan")
|
|
|
|
except (OSError, AssertionError) as e:
|
|
|
|
if not r:
|
|
|
|
raise exception.PlatformioException(
|
|
|
|
"\n".join([str(cmd), str(e)]))
|
|
|
|
if ("Permission denied" in r['err'] and
|
|
|
|
"windows" not in util.get_systype()):
|
|
|
|
click.secho("""
|
|
|
|
-----------------
|
|
|
|
Permission denied
|
|
|
|
-----------------
|
|
|
|
You need the `sudo` permission to install Python packages. Try
|
|
|
|
|
|
|
|
> sudo platformio upgrade
|
2015-01-06 17:45:07 +02:00
|
|
|
|
2015-09-02 13:42:01 +03:00
|
|
|
WARNING! Don't use `sudo` for the rest PlatformIO commands.
|
|
|
|
""", fg="yellow", err=True)
|
|
|
|
raise exception.ReturnErrorCode()
|
|
|
|
else:
|
|
|
|
raise exception.PlatformioException(
|
|
|
|
"\n".join([str(cmd), r['out'], r['err']]))
|
2014-08-03 18:40:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_latest_version():
|
2015-02-13 23:55:08 +02:00
|
|
|
try:
|
|
|
|
pkgdata = requests.get(
|
2015-04-29 18:17:14 +01:00
|
|
|
"https://pypi.python.org/pypi/platformio/json",
|
|
|
|
headers=util.get_request_defheaders()
|
|
|
|
).json()
|
2015-02-13 23:55:08 +02:00
|
|
|
return pkgdata['info']['version']
|
|
|
|
except:
|
2015-09-02 13:42:01 +03:00
|
|
|
raise exception.GetLatestVersionError()
|