2015-11-18 17:16:17 +02:00
|
|
|
# Copyright 2014-2015 Ivan Kravets <me@ikravets.com>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
from os import listdir
|
|
|
|
from os.path import join
|
2015-09-10 20:31:26 +03:00
|
|
|
from platform import system
|
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
|
|
|
|
2015-09-02 23:07:45 +03:00
|
|
|
@click.command(cls=PlatformioCLI,
|
|
|
|
context_settings=dict(help_option_names=["-h", "--help"]))
|
2014-11-29 22:55:32 +02:00
|
|
|
@click.version_option(__version__, prog_name="PlatformIO")
|
2015-04-16 17:04:45 +01:00
|
|
|
@click.option("--force", "-f", is_flag=True,
|
2015-09-03 19:04:09 +03:00
|
|
|
help="Force to accept any confirmation prompts.")
|
|
|
|
@click.option("--caller", "-c", help="Caller ID (service).")
|
2014-11-29 22:55:32 +02:00
|
|
|
@click.pass_context
|
2015-09-03 19:04:09 +03:00
|
|
|
def cli(ctx, force, caller):
|
|
|
|
maintenance.on_platformio_start(ctx, force, caller)
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
2014-11-29 22:55:32 +02:00
|
|
|
@cli.resultcallback()
|
|
|
|
@click.pass_context
|
2015-09-03 19:04:09 +03:00
|
|
|
def process_result(ctx, result, force, caller): # 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-09-10 20:31:26 +03:00
|
|
|
if "cygwin" in system().lower():
|
|
|
|
raise exception.CygwinEnvDetected()
|
|
|
|
|
2015-03-18 23:02:04 +02:00
|
|
|
# https://urllib3.readthedocs.org
|
|
|
|
# /en/latest/security.html#insecureplatformwarning
|
2015-08-21 23:09:56 +03:00
|
|
|
try:
|
|
|
|
requests.packages.urllib3.disable_warnings()
|
|
|
|
except AttributeError:
|
2015-09-04 20:35:56 +03:00
|
|
|
raise exception.PlatformioException(
|
2015-08-21 23:09:56 +03:00
|
|
|
"Invalid installation of Python `requests` package`. See "
|
2015-09-04 20:35:56 +03:00
|
|
|
"< https://github.com/platformio/platformio/issues/252 >")
|
2015-08-21 23:09:56 +03:00
|
|
|
|
2015-09-03 19:04:09 +03:00
|
|
|
cli(None, 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()
|
2015-11-26 22:02:59 +02:00
|
|
|
|
|
|
|
error_str += """
|
|
|
|
============================================================
|
|
|
|
|
|
|
|
An unexpected error occurred. Further steps:
|
|
|
|
|
|
|
|
* Verify that you have the latest version of PlatformIO using
|
|
|
|
`platformio upgrade` command
|
|
|
|
* Report this problem to the developers
|
|
|
|
https://github.com/platformio/platformio/issues
|
|
|
|
|
|
|
|
============================================================
|
|
|
|
"""
|
2015-02-20 22:02:38 +02:00
|
|
|
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())
|