Files
platformio-core/platformio/commands/upgrade.py

138 lines
4.5 KiB
Python
Raw Normal View History

# Copyright 2014-present Ivan Kravets <me@ikravets.com>
#
# 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.
import os
import re
import sys
2014-12-03 20:16:50 +02:00
import click
import requests
from platformio import VERSION, __version__, exception, util
2014-12-03 20:16:50 +02:00
@click.command("upgrade",
short_help="Upgrade PlatformIO to the latest version")
def cli():
latest = get_latest_version()
if __version__ == latest:
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"
)
else:
click.secho("Please wait while upgrading PlatformIO ...",
fg="yellow")
to_develop = not all([c.isdigit() for c in latest if c != "."])
2015-09-02 13:42:01 +03:00
cmds = (
["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:
if sys.version_info < (2, 7, 0):
cmd[0] += ".__main__"
cmd = [os.path.normpath(sys.executable), "-m"] + cmd
2015-09-02 13:42:01 +03:00
r = None
r = util.exec_command(cmd)
# try pip with disabled cache
if r['returncode'] != 0 and cmd[2] == "pip":
cmd.insert(3, "--no-cache-dir")
r = util.exec_command(cmd)
2015-09-02 13:42:01 +03:00
assert r['returncode'] == 0
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(
"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)
2016-08-02 21:13:58 +03:00
click.secho("http://docs.platformio.org/en/stable/history.html",
2015-09-02 13:42:01 +03:00
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-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']]))
def get_latest_version():
try:
if not isinstance(VERSION[2], int):
try:
return get_develop_latest_version()
except: # pylint: disable=bare-except
pass
return get_pypi_latest_version()
except:
2015-09-02 13:42:01 +03:00
raise exception.GetLatestVersionError()
def get_develop_latest_version():
version = None
r = requests.get(
"https://raw.githubusercontent.com/platformio/platformio"
"/develop/platformio/__init__.py",
headers=util.get_request_defheaders()
)
r.raise_for_status()
for line in r.text.split("\n"):
line = line.strip()
if not line.startswith("VERSION"):
continue
match = re.match(r"VERSION\s*=\s*\(([^\)]+)\)", line)
if not match:
continue
version = match.group(1)
for c in (" ", "'", '"'):
version = version.replace(c, "")
version = ".".join(version.split(","))
assert version
return version
def get_pypi_latest_version():
r = requests.get(
"https://pypi.python.org/pypi/platformio/json",
headers=util.get_request_defheaders()
)
r.raise_for_status()
return r.json()['info']['version']