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-08-03 18:40:20 +03:00
|
|
|
|
2015-12-30 20:12:52 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
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")
|
|
|
|
|
2016-01-23 21:45:08 +02:00
|
|
|
to_develop = False
|
|
|
|
try:
|
|
|
|
from pkg_resources import parse_version
|
|
|
|
to_develop = parse_version(last) < parse_version(__version__)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2015-09-02 13:42:01 +03:00
|
|
|
cmds = (
|
2016-01-23 21:45:08 +02:00
|
|
|
["pip", "install", "--upgrade",
|
|
|
|
"https://github.com/platformio/platformio/archive/develop.zip"
|
|
|
|
if to_develop else "platformio"],
|
2015-09-02 13:42:01 +03:00
|
|
|
["platformio", "--version"]
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd = None
|
|
|
|
r = None
|
|
|
|
try:
|
|
|
|
for cmd in cmds:
|
2016-01-13 22:22:55 +02:00
|
|
|
cmd = [os.path.normpath(sys.executable), "-m"] + cmd
|
2015-09-02 13:42:01 +03:00
|
|
|
r = None
|
|
|
|
r = util.exec_command(cmd)
|
2015-10-02 16:14:04 +01:00
|
|
|
|
|
|
|
# try pip with disabled cache
|
2016-01-13 22:22:55 +02:00
|
|
|
if r['returncode'] != 0 and cmd[2] == "pip":
|
2015-12-30 20:12:52 +02:00
|
|
|
cmd.insert(3, "--no-cache-dir")
|
|
|
|
r = util.exec_command(cmd)
|
2015-10-02 16:14:04 +01:00
|
|
|
|
2015-09-02 13:42:01 +03:00
|
|
|
assert r['returncode'] == 0
|
2016-01-23 21:45:08 +02:00
|
|
|
assert "version" in r['out']
|
|
|
|
actual_version = r['out'].strip().split("version", 1)[1].strip()
|
2015-09-02 13:42:01 +03:00
|
|
|
click.secho(
|
2016-01-23 21:45:08 +02:00
|
|
|
"PlatformIO has been successfully upgraded to %s" %
|
|
|
|
actual_version, fg="green")
|
2015-09-02 13:42:01 +03:00
|
|
|
click.echo("Release notes: ", nl=False)
|
|
|
|
click.secho("http://docs.platformio.org/en/latest/history.html",
|
|
|
|
fg="cyan")
|
2015-11-26 22:02:59 +02:00
|
|
|
except Exception as e: # pylint: disable=W0703
|
2015-09-02 13:42:01 +03:00
|
|
|
if not r:
|
2015-11-26 22:02:59 +02:00
|
|
|
raise exception.UpgradeError(
|
2015-09-02 13:42:01 +03:00
|
|
|
"\n".join([str(cmd), str(e)]))
|
2015-12-07 22:44:31 +02:00
|
|
|
permission_errors = (
|
|
|
|
"permission denied",
|
|
|
|
"not permitted"
|
|
|
|
)
|
|
|
|
if (any([m in r['err'].lower() for m in permission_errors]) and
|
2015-09-02 13:42:01 +03:00
|
|
|
"windows" not in util.get_systype()):
|
|
|
|
click.secho("""
|
|
|
|
-----------------
|
|
|
|
Permission denied
|
|
|
|
-----------------
|
|
|
|
You need the `sudo` permission to install Python packages. Try
|
|
|
|
|
2015-12-07 22:44:31 +02:00
|
|
|
> sudo pip install -U platformio
|
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:
|
2015-11-26 22:02:59 +02:00
|
|
|
raise exception.UpgradeError(
|
2015-09-02 13:42:01 +03:00
|
|
|
"\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()
|