From 5cfca6b1f6362f26263f868ac692f3e249796360 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 26 Sep 2016 22:15:08 +0300 Subject: [PATCH] Initial support for PIO Remote --- platformio/commands/remote.py | 232 ++++++++++++++++++++++++++++++++ platformio/exception.py | 7 - platformio/managers/platform.py | 2 +- platformio/pioplus.py | 21 ++- 4 files changed, 243 insertions(+), 19 deletions(-) create mode 100644 platformio/commands/remote.py diff --git a/platformio/commands/remote.py b/platformio/commands/remote.py new file mode 100644 index 00000000..93fe9b42 --- /dev/null +++ b/platformio/commands/remote.py @@ -0,0 +1,232 @@ +# Copyright 2014-present PlatformIO +# +# 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. + +import sys +import threading +from os import getcwd +from os.path import isfile, join +from tempfile import mkdtemp +from time import sleep + +import click +from serial import VERSION as PYSERIAL_VERSION + +from platformio import util +from platformio.commands.device import device_monitor as cmd_device_monitor +from platformio.pioplus import pioplus_call + +# pylint: disable=unused-argument + + +@click.group("remote", short_help="PIO Remote") +def cli(): + pass + + +@cli.command("agent", short_help="Host agent") +def remote_agent(): + pioplus_call(sys.argv[1:]) + + +@cli.command("run", short_help="Process project environments") +@click.option("-e", "--environment", multiple=True) +@click.option("-t", "--target", multiple=True) +@click.option("--upload-port") +@click.option( + "-d", + "--project-dir", + default=getcwd, + type=click.Path( + exists=True, + file_okay=True, + dir_okay=True, + writable=True, + resolve_path=True)) +@click.option("-s", "--silent", is_flag=True) +@click.option("-v", "--verbose", is_flag=True) +@click.option("-r", "--build-remotely", is_flag=True) +def remote_run(**kwargs): + pioplus_call(sys.argv[1:]) + + +@cli.group("device", short_help="Monitor device or list existing") +def remote_device(): + pass + + +@remote_device.command("list", short_help="List devices") +@click.option("--json-output", is_flag=True) +def device_list(json_output): + pioplus_call(sys.argv[1:]) + + +if int(PYSERIAL_VERSION[0]) == 3: + + @remote_device.command("monitor", short_help="Monitor device (Serial)") + @click.option("--port", "-p", help="Port, a number or a device name") + @click.option( + "--baud", + "-b", + type=int, + default=9600, + help="Set baud rate, default=9600") + @click.option( + "--parity", + default="N", + type=click.Choice(["N", "E", "O", "S", "M"]), + help="Set parity, default=N") + @click.option( + "--rtscts", + is_flag=True, + help="Enable RTS/CTS flow control, default=Off") + @click.option( + "--xonxoff", + is_flag=True, + help="Enable software flow control, default=Off") + @click.option( + "--rts", + default=None, + type=click.Choice(["0", "1"]), + help="Set initial RTS line state") + @click.option( + "--dtr", + default=None, + type=click.Choice(["0", "1"]), + help="Set initial DTR line state") + @click.option( + "--echo", is_flag=True, help="Enable local echo, default=Off") + @click.option( + "--encoding", + default="UTF-8", + help="Set the encoding for the serial port (e.g. hexlify, " + "Latin1, UTF-8), default: UTF-8") + @click.option( + "--filter", "-f", multiple=True, help="Add text transformation") + @click.option( + "--eol", + default="CRLF", + type=click.Choice(["CR", "LF", "CRLF"]), + help="End of line mode, default=CRLF") + @click.option( + "--raw", + is_flag=True, + help="Do not apply any encodings/transformations") + @click.option( + "--exit-char", + type=int, + default=29, + help="ASCII code of special character that is used to exit " + "the application, default=29 (DEC)") + @click.option( + "--menu-char", + type=int, + default=20, + help="ASCII code of special character that is used to " + "control miniterm (menu), default=20 (DEC)") + @click.option( + "--quiet", + is_flag=True, + help="Diagnostics: suppress non-error messages, default=Off") + @click.pass_context + def device_monitor(ctx, **kwargs): + _device_monitor(ctx, **kwargs) +else: + + @remote_device.command("monitor", short_help="Monitor device (Serial)") + @click.option("--port", "-p", help="Port, a number or a device name") + @click.option( + "--baud", + "-b", + type=int, + default=9600, + help="Set baud rate, default=9600") + @click.option( + "--parity", + default="N", + type=click.Choice(["N", "E", "O", "S", "M"]), + help="Set parity, default=N") + @click.option( + "--rtscts", + is_flag=True, + help="Enable RTS/CTS flow control, default=Off") + @click.option( + "--xonxoff", + is_flag=True, + help="Enable software flow control, default=Off") + @click.option( + "--rts", + default=None, + type=click.Choice(["0", "1"]), + help="Set initial RTS line state, default=0") + @click.option( + "--dtr", + default=None, + type=click.Choice(["0", "1"]), + help="Set initial DTR line state, default=0") + @click.option( + "--echo", is_flag=True, help="Enable local echo, default=Off") + @click.option( + "--cr", + is_flag=True, + help="Do not send CR+LF, send CR only, default=Off") + @click.option( + "--lf", + is_flag=True, + help="Do not send CR+LF, send LF only, default=Off") + @click.option( + "--debug", + "-d", + count=True, + help="""Debug received data (escape non-printable chars) + # --debug can be given multiple times: + # 0: just print what is received + # 1: escape non-printable characters, do newlines as unusual + # 2: escape non-printable characters, newlines too + # 3: hex dump everything""") + @click.option( + "--exit-char", + type=int, + default=29, + help="ASCII code of special character that is used to exit " + "the application, default=29 (DEC)") + @click.option( + "--menu-char", + type=int, + default=20, + help="ASCII code of special character that is used to " + "control miniterm (menu), default=20 (DEC)") + @click.option( + "--quiet", + is_flag=True, + help="Diagnostics: suppress non-error messages, default=Off") + @click.pass_context + def device_monitor(ctx, **kwargs): + _device_monitor(ctx, **kwargs) + + +def _device_monitor(ctx, **kwargs): + sock_dir = mkdtemp(suffix="pioplus") + sock_file = join(sock_dir, "sock") + try: + t = threading.Thread( + target=pioplus_call, args=(sys.argv[1:] + ["--sock", sock_dir], )) + t.start() + while t.is_alive() and not isfile(sock_file): + sleep(0.1) + if not t.is_alive(): + return + ctx.invoke(cmd_device_monitor, port=open(sock_file).read()) + t.join(2) + finally: + util.rmtree_(sock_dir) diff --git a/platformio/exception.py b/platformio/exception.py index c3cf76f5..5d5be430 100644 --- a/platformio/exception.py +++ b/platformio/exception.py @@ -200,13 +200,6 @@ class CIBuildEnvsEmpty(PlatformioException): "predefined environments using `--project-conf` option" -class TestDirEmpty(PlatformioException): - - MESSAGE = "Test directory '{0}' is empty. More details about Unit "\ - "Testing:\n http://docs.platformio.org/en/stable/platforms/"\ - "unit_testing.html" - - class UpgradeError(PlatformioException): MESSAGE = """{0} diff --git a/platformio/managers/platform.py b/platformio/managers/platform.py index eecb481c..48329197 100644 --- a/platformio/managers/platform.py +++ b/platformio/managers/platform.py @@ -438,7 +438,7 @@ class PlatformBase(PlatformPackagesMixin, PlatformRunMixin): return names def configure_default_packages(self, variables, targets): - # enbale used frameworks + # enable used frameworks for framework in variables.get("pioframework", "").split(","): if not self.frameworks: continue diff --git a/platformio/pioplus.py b/platformio/pioplus.py index 3fe18eba..4d91550d 100644 --- a/platformio/pioplus.py +++ b/platformio/pioplus.py @@ -20,7 +20,7 @@ from platform import system from platformio import exception, util from platformio.managers.package import PackageManager -PACKAGE_PIOPLUS_NAME = "tool-pioplus" +PACKAGE_DEPS = {"pysite": "pysite-pioplus", "tool": "tool-pioplus"} class PioPlusPackageManager(PackageManager): @@ -32,27 +32,26 @@ class PioPlusPackageManager(PackageManager): "https://dl.platformio.org/packages/manifest.json"]) -def get_pioplusexe_path(): +def pioplus_install(): pm = PioPlusPackageManager() - package_dir = pm.get_package_dir(PACKAGE_PIOPLUS_NAME) - if not package_dir: - pm.install(PACKAGE_PIOPLUS_NAME) - package_dir = pm.get_package_dir(PACKAGE_PIOPLUS_NAME) - assert package_dir - return join(package_dir, "pioplus") + for name in PACKAGE_DEPS.values(): + pm.install(name, silent=True) def pioplus_update(): pm = PioPlusPackageManager() - if pm.get_package_dir(PACKAGE_PIOPLUS_NAME): - pm.update(PACKAGE_PIOPLUS_NAME) + for name in PACKAGE_DEPS.values(): + pm.update(name) def pioplus_call(args, **kwargs): - pioplus_path = get_pioplusexe_path() + pioplus_install() + pm = PioPlusPackageManager() + pioplus_path = join(pm.get_package_dir(PACKAGE_DEPS['tool']), "pioplus") if system() == "Linux": os.environ['LD_LIBRARY_PATH'] = dirname(pioplus_path) os.environ['PYTHONEXEPATH'] = util.get_pythonexe_path() + os.environ['PYTHONPYSITEDIR'] = pm.get_package_dir(PACKAGE_DEPS['pysite']) util.copy_pythonpath_to_osenv() if subprocess.call([pioplus_path] + args, **kwargs) != 0: raise exception.ReturnErrorCode()