forked from platformio/platformio-core
Resolve #8: Implement "upgrade" command and "auto-check" for the latest version
This commit is contained in:
+20
-3
@@ -2,15 +2,17 @@
|
||||
# See LICENSE for details.
|
||||
|
||||
from os import listdir
|
||||
from os.path import join
|
||||
from os.path import getmtime, isfile, join
|
||||
from sys import exit as sys_exit
|
||||
from time import time
|
||||
from traceback import format_exc
|
||||
|
||||
from click import command, MultiCommand, version_option
|
||||
from click import command, MultiCommand, secho, version_option
|
||||
|
||||
from platformio import __version__
|
||||
from platformio.commands.upgrade import get_latest_version
|
||||
from platformio.exception import PlatformioException, UnknownCLICommand
|
||||
from platformio.util import get_source_dir
|
||||
from platformio.util import get_home_dir, get_source_dir
|
||||
|
||||
|
||||
class PlatformioCLI(MultiCommand):
|
||||
@@ -40,8 +42,23 @@ def cli():
|
||||
pass
|
||||
|
||||
|
||||
def autocheck_latest_version():
|
||||
check_interval = 3600 * 24 * 7 # 1 week
|
||||
checkfile = join(get_home_dir(), ".pioupgrade")
|
||||
if isfile(checkfile) and getmtime(checkfile) > (time() - check_interval):
|
||||
return False
|
||||
with open(checkfile, "w") as f:
|
||||
f.write(str(time()))
|
||||
return get_latest_version() != __version__
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
if autocheck_latest_version():
|
||||
secho("\nThere is a new version of PlatformIO available.\n"
|
||||
"Please upgrade it via `platformio upgrade` command.\n",
|
||||
fg="yellow")
|
||||
|
||||
cli()
|
||||
except Exception as e: # pylint: disable=W0703
|
||||
if isinstance(e, PlatformioException):
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
||||
# See LICENSE for details.
|
||||
|
||||
from click import command, secho
|
||||
from requests import get
|
||||
|
||||
from platformio import __version__
|
||||
from platformio.exception import GetLatestVersionError
|
||||
from platformio.util import exec_command
|
||||
|
||||
|
||||
@command("upgrade", short_help="Upgrade PlatformIO to the latest version")
|
||||
def cli():
|
||||
try:
|
||||
last = get_latest_version()
|
||||
except:
|
||||
raise GetLatestVersionError()
|
||||
|
||||
if __version__ == last:
|
||||
return secho("You're up-to-date!\nPlatformIO %s is currently the "
|
||||
"newest version available." % __version__, fg="green")
|
||||
else:
|
||||
result = exec_command(["pip", "install", "--upgrade", "platformio"])
|
||||
secho(result['out'], fg="green")
|
||||
secho(result['err'], fg="red")
|
||||
|
||||
|
||||
def get_latest_version():
|
||||
pkgdata = get("https://pypi.python.org/pypi/platformio/json").json()
|
||||
return pkgdata['info']['version']
|
||||
@@ -100,3 +100,8 @@ class UnknownEnvNames(PlatformioException):
|
||||
class GetSerialPortsError(PlatformioException):
|
||||
|
||||
MESSAGE = "No implementation for your platform ('%s') available"
|
||||
|
||||
|
||||
class GetLatestVersionError(PlatformioException):
|
||||
|
||||
MESSAGE = "Can't retrieve latest PlatformIO version"
|
||||
|
||||
Reference in New Issue
Block a user