diff --git a/platformio/commands/upgrade.py b/platformio/commands/upgrade.py index 44e3b8ea..4f1f9b8e 100644 --- a/platformio/commands/upgrade.py +++ b/platformio/commands/upgrade.py @@ -1,4 +1,4 @@ -# Copyright 2014-2016 Ivan Kravets +# Copyright 2014-present Ivan Kravets # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,19 +13,20 @@ # limitations under the License. import os +import re import sys import click import requests -from platformio import __version__, exception, util +from platformio import VERSION, __version__, exception, util @click.command("upgrade", short_help="Upgrade PlatformIO to the latest version") def cli(): - last = get_latest_version() - if __version__ == last: + latest = get_latest_version() + if __version__ == latest: return click.secho( "You're up-to-date!\nPlatformIO %s is currently the " "newest version available." % __version__, fg="green" @@ -34,13 +35,7 @@ def cli(): click.secho("Please wait while upgrading PlatformIO ...", fg="yellow") - to_develop = False - try: - from pkg_resources import parse_version - to_develop = parse_version(last) < parse_version(__version__) - except ImportError: - pass - + to_develop = not all([c.isdigit() for c in latest if c != "."]) cmds = ( ["pip", "install", "--upgrade", "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(): try: - pkgdata = requests.get( - "https://pypi.python.org/pypi/platformio/json", - headers=util.get_request_defheaders() - ).json() - return pkgdata['info']['version'] + if not isinstance(VERSION[2], int): + try: + return get_develop_latest_version() + except: # pylint: disable=bare-except + pass + return get_pypi_latest_version() except: 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'] diff --git a/platformio/maintenance.py b/platformio/maintenance.py index ad190080..5bc31f2c 100644 --- a/platformio/maintenance.py +++ b/platformio/maintenance.py @@ -67,8 +67,10 @@ def on_platformio_exception(e): class Upgrader(object): def __init__(self, from_version, to_version): - self.from_version = semantic_version.Version.coerce(from_version) - self.to_version = semantic_version.Version.coerce(to_version) + self.from_version = semantic_version.Version.coerce( + util.pepver_to_semver(from_version)) + self.to_version = semantic_version.Version.coerce( + util.pepver_to_semver(to_version)) self._upgraders = [ (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) latest_version = get_latest_version() - if (semantic_version.Version.coerce(latest_version) <= - semantic_version.Version.coerce(__version__)): + if semantic_version.Version.coerce(util.pepver_to_semver( + latest_version)) <= semantic_version.Version.coerce( + util.pepver_to_semver(__version__)): return terminal_width, _ = click.get_terminal_size() diff --git a/platformio/util.py b/platformio/util.py index 2eeaec55..d3ac1980 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -461,3 +461,7 @@ def where_is_program(program, envpath=None): return join(bin_dir, "%s.exe" % program) return program + + +def pepver_to_semver(pepver): + return re.sub(r"(\.\d+)\.?(dev|a|b|rc|post)", r"\1-\2", pepver, 1)