Check development version automatically

This commit is contained in:
Ivan Kravets
2016-08-01 14:25:11 +03:00
parent e5b76687a8
commit c728b91914
3 changed files with 55 additions and 20 deletions

View File

@ -1,4 +1,4 @@
# Copyright 2014-2016 Ivan Kravets <me@ikravets.com> # Copyright 2014-present Ivan Kravets <me@ikravets.com>
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -13,19 +13,20 @@
# limitations under the License. # limitations under the License.
import os import os
import re
import sys import sys
import click import click
import requests import requests
from platformio import __version__, exception, util from platformio import VERSION, __version__, exception, util
@click.command("upgrade", @click.command("upgrade",
short_help="Upgrade PlatformIO to the latest version") short_help="Upgrade PlatformIO to the latest version")
def cli(): def cli():
last = get_latest_version() latest = get_latest_version()
if __version__ == last: if __version__ == latest:
return click.secho( return click.secho(
"You're up-to-date!\nPlatformIO %s is currently the " "You're up-to-date!\nPlatformIO %s is currently the "
"newest version available." % __version__, fg="green" "newest version available." % __version__, fg="green"
@ -34,13 +35,7 @@ def cli():
click.secho("Please wait while upgrading PlatformIO ...", click.secho("Please wait while upgrading PlatformIO ...",
fg="yellow") fg="yellow")
to_develop = False to_develop = not all([c.isdigit() for c in latest if c != "."])
try:
from pkg_resources import parse_version
to_develop = parse_version(last) < parse_version(__version__)
except ImportError:
pass
cmds = ( cmds = (
["pip", "install", "--upgrade", ["pip", "install", "--upgrade",
"https://github.com/platformio/platformio/archive/develop.zip" "https://github.com/platformio/platformio/archive/develop.zip"
@ -100,10 +95,43 @@ WARNING! Don't use `sudo` for the rest PlatformIO commands.
def get_latest_version(): def get_latest_version():
try: try:
pkgdata = requests.get( if not isinstance(VERSION[2], int):
"https://pypi.python.org/pypi/platformio/json", try:
headers=util.get_request_defheaders() return get_develop_latest_version()
).json() except: # pylint: disable=bare-except
return pkgdata['info']['version'] pass
return get_pypi_latest_version()
except: except:
raise exception.GetLatestVersionError() 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']

View File

@ -67,8 +67,10 @@ def on_platformio_exception(e):
class Upgrader(object): class Upgrader(object):
def __init__(self, from_version, to_version): def __init__(self, from_version, to_version):
self.from_version = semantic_version.Version.coerce(from_version) self.from_version = semantic_version.Version.coerce(
self.to_version = semantic_version.Version.coerce(to_version) util.pepver_to_semver(from_version))
self.to_version = semantic_version.Version.coerce(
util.pepver_to_semver(to_version))
self._upgraders = [ self._upgraders = [
(semantic_version.Version("3.0.0"), self._upgrade_to_3_0_0) (semantic_version.Version("3.0.0"), self._upgrade_to_3_0_0)
@ -180,8 +182,9 @@ def check_platformio_upgrade():
app.set_state_item("last_check", last_check) app.set_state_item("last_check", last_check)
latest_version = get_latest_version() latest_version = get_latest_version()
if (semantic_version.Version.coerce(latest_version) <= if semantic_version.Version.coerce(util.pepver_to_semver(
semantic_version.Version.coerce(__version__)): latest_version)) <= semantic_version.Version.coerce(
util.pepver_to_semver(__version__)):
return return
terminal_width, _ = click.get_terminal_size() terminal_width, _ = click.get_terminal_size()

View File

@ -461,3 +461,7 @@ def where_is_program(program, envpath=None):
return join(bin_dir, "%s.exe" % program) return join(bin_dir, "%s.exe" % program)
return program return program
def pepver_to_semver(pepver):
return re.sub(r"(\.\d+)\.?(dev|a|b|rc|post)", r"\1-\2", pepver, 1)