2014-05-18 23:38:59 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2014-08-04 11:09:47 +03:00
|
|
|
from os import listdir, makedirs
|
|
|
|
from os.path import getmtime, isdir, isfile, join
|
2014-06-07 13:34:31 +03:00
|
|
|
from sys import exit as sys_exit
|
2014-08-03 18:40:20 +03:00
|
|
|
from time import time
|
2014-06-07 13:34:31 +03:00
|
|
|
from traceback import format_exc
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-08-03 18:40:20 +03:00
|
|
|
from click import command, MultiCommand, secho, version_option
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
from platformio import __version__
|
2014-08-03 18:40:20 +03:00
|
|
|
from platformio.commands.upgrade import get_latest_version
|
2014-06-07 13:34:31 +03:00
|
|
|
from platformio.exception import PlatformioException, UnknownCLICommand
|
2014-08-03 18:40:20 +03:00
|
|
|
from platformio.util import get_home_dir, get_source_dir
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
2014-08-13 11:55:25 +03:00
|
|
|
class PlatformioCLI(MultiCommand): # pylint: disable=R0904
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
def list_commands(self, ctx):
|
|
|
|
cmds = []
|
|
|
|
for filename in listdir(join(get_source_dir(), "commands")):
|
|
|
|
if filename.startswith("__init__"):
|
|
|
|
continue
|
|
|
|
if filename.endswith(".py"):
|
|
|
|
cmds.append(filename[:-3])
|
|
|
|
cmds.sort()
|
|
|
|
return cmds
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
def get_command(self, ctx, name):
|
2014-06-07 13:34:31 +03:00
|
|
|
try:
|
|
|
|
mod = __import__("platformio.commands." + name,
|
|
|
|
None, None, ["cli"])
|
|
|
|
except ImportError:
|
|
|
|
raise UnknownCLICommand(name)
|
2014-06-03 21:27:36 +03:00
|
|
|
return mod.cli
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
@command(cls=PlatformioCLI)
|
2014-06-23 18:04:07 +03:00
|
|
|
@version_option(__version__, prog_name="PlatformIO")
|
2014-06-03 21:27:36 +03:00
|
|
|
def cli():
|
2014-05-18 23:38:59 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-08-03 18:40:20 +03:00
|
|
|
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
|
2014-08-04 11:09:47 +03:00
|
|
|
if not isdir(get_home_dir()):
|
|
|
|
makedirs(get_home_dir())
|
2014-08-03 18:40:20 +03:00
|
|
|
with open(checkfile, "w") as f:
|
|
|
|
f.write(str(time()))
|
|
|
|
return get_latest_version() != __version__
|
|
|
|
|
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
def main():
|
2014-06-07 13:34:31 +03:00
|
|
|
try:
|
2014-08-03 18:40:20 +03:00
|
|
|
if autocheck_latest_version():
|
|
|
|
secho("\nThere is a new version of PlatformIO available.\n"
|
|
|
|
"Please upgrade it via `platformio upgrade` command.\n",
|
|
|
|
fg="yellow")
|
|
|
|
|
2014-06-07 13:34:31 +03:00
|
|
|
cli()
|
|
|
|
except Exception as e: # pylint: disable=W0703
|
|
|
|
if isinstance(e, PlatformioException):
|
2014-07-27 22:39:41 +03:00
|
|
|
sys_exit("Error: " + str(e))
|
2014-06-07 13:34:31 +03:00
|
|
|
else:
|
|
|
|
print format_exc()
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-06-07 13:34:31 +03:00
|
|
|
sys_exit(main())
|