2014-05-18 23:38:59 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
from os import listdir
|
|
|
|
from os.path import join
|
2014-06-07 13:34:31 +03:00
|
|
|
from sys import exit as sys_exit
|
|
|
|
from traceback import format_exc
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
import click
|
2015-03-18 23:02:04 +02:00
|
|
|
import requests
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2015-02-19 22:02:50 +02:00
|
|
|
from platformio import __version__, exception, maintenance
|
2014-11-29 22:55:32 +02:00
|
|
|
from platformio.util import get_source_dir
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
class PlatformioCLI(click.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-11-29 22:55:32 +02:00
|
|
|
mod = None
|
2014-06-07 13:34:31 +03:00
|
|
|
try:
|
|
|
|
mod = __import__("platformio.commands." + name,
|
|
|
|
None, None, ["cli"])
|
|
|
|
except ImportError:
|
2015-04-24 15:48:32 +01:00
|
|
|
try:
|
|
|
|
return self._handle_obsolate_command(name)
|
|
|
|
except AttributeError:
|
|
|
|
raise exception.UnknownCLICommand(name)
|
2014-06-03 21:27:36 +03:00
|
|
|
return mod.cli
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2015-05-06 18:07:17 +01:00
|
|
|
@staticmethod
|
|
|
|
def _handle_obsolate_command(name):
|
2015-04-24 15:48:32 +01:00
|
|
|
if name in ("install", "list", "search", "show", "uninstall"):
|
|
|
|
click.secho(
|
2015-06-28 20:16:43 +03:00
|
|
|
"Warning! `platformio %s` command is deprecated and will be "
|
2015-04-24 15:51:08 +01:00
|
|
|
"removed in the next release! Please use "
|
|
|
|
"`platformio platforms %s` instead." % (name, name),
|
2015-06-28 19:20:31 +03:00
|
|
|
fg="yellow"
|
2015-04-24 15:48:32 +01:00
|
|
|
)
|
|
|
|
from platformio.commands import platforms
|
|
|
|
return getattr(platforms, "platforms_" + name)
|
|
|
|
raise AttributeError()
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
@click.command(cls=PlatformioCLI)
|
|
|
|
@click.version_option(__version__, prog_name="PlatformIO")
|
2015-04-16 17:04:45 +01:00
|
|
|
@click.option("--force", "-f", is_flag=True,
|
|
|
|
help="Force to accept any confirmation prompts")
|
2014-11-29 22:55:32 +02:00
|
|
|
@click.pass_context
|
2015-04-16 17:04:45 +01:00
|
|
|
def cli(ctx, force):
|
|
|
|
maintenance.on_platformio_start(ctx, force)
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
@cli.resultcallback()
|
|
|
|
@click.pass_context
|
2015-04-16 17:04:45 +01:00
|
|
|
def process_result(ctx, result, force): # pylint: disable=W0613
|
2014-11-29 22:55:32 +02:00
|
|
|
maintenance.on_platformio_end(ctx, result)
|
2014-08-03 18:40:20 +03:00
|
|
|
|
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
def main():
|
2014-06-07 13:34:31 +03:00
|
|
|
try:
|
2015-03-18 23:02:04 +02:00
|
|
|
# https://urllib3.readthedocs.org
|
|
|
|
# /en/latest/security.html#insecureplatformwarning
|
|
|
|
requests.packages.urllib3.disable_warnings()
|
|
|
|
|
2015-04-16 17:04:45 +01:00
|
|
|
cli(None, None)
|
2014-06-07 13:34:31 +03:00
|
|
|
except Exception as e: # pylint: disable=W0703
|
2015-02-19 22:02:50 +02:00
|
|
|
if not isinstance(e, exception.ReturnErrorCode):
|
|
|
|
maintenance.on_platformio_exception(e)
|
2015-02-20 22:02:38 +02:00
|
|
|
error_str = "Error: "
|
2015-02-19 22:02:50 +02:00
|
|
|
if isinstance(e, exception.PlatformioException):
|
2015-02-20 22:02:38 +02:00
|
|
|
error_str += str(e)
|
2015-02-19 22:02:50 +02:00
|
|
|
else:
|
2015-02-20 22:02:38 +02:00
|
|
|
error_str += format_exc()
|
|
|
|
click.secho(error_str, fg="red", err=True)
|
2015-07-30 18:17:57 +03:00
|
|
|
return 1
|
|
|
|
return 0
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-06-07 13:34:31 +03:00
|
|
|
sys_exit(main())
|