diff --git a/HISTORY.rst b/HISTORY.rst index 13a15253..36b638be 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -6,6 +6,11 @@ Release Notes PlatformIO Core 4.0 ------------------- +4.1.1 (2019-??-??) +~~~~~~~~~~~~~~~~~~ + +* Handle project configuration (monitor, test, and upload options) for PIO Remote commands (`issue #2591 `_) + 4.1.0 (2019-11-07) ~~~~~~~~~~~~~~~~~~ diff --git a/docs b/docs index fc2ae00c..5210aa16 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit fc2ae00cbb369d1e81f1caf392782c2f30756707 +Subproject commit 5210aa16d729bfdc654d2062e109f211e0493fad diff --git a/platformio/commands/device.py b/platformio/commands/device.py index 45f94ae7..67c7f05a 100644 --- a/platformio/commands/device.py +++ b/platformio/commands/device.py @@ -172,18 +172,11 @@ def device_list( # pylint: disable=too-many-branches help="Load configuration from `platformio.ini` and specified environment", ) def device_monitor(**kwargs): # pylint: disable=too-many-branches - env_options = {} + project_options = {} try: with fs.cd(kwargs["project_dir"]): - env_options = get_project_options(kwargs["environment"]) - for k in ("port", "speed", "rts", "dtr"): - k2 = "monitor_%s" % k - if k == "speed": - k = "baud" - if kwargs[k] is None and k2 in env_options: - kwargs[k] = env_options[k2] - if k != "port": - kwargs[k] = int(kwargs[k]) + project_options = get_project_options(kwargs["environment"]) + kwargs = apply_project_monitor_options(kwargs, project_options) except exception.NotPlatformIOProject: pass @@ -191,29 +184,19 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches ports = util.get_serial_ports(filter_hwid=True) if len(ports) == 1: kwargs["port"] = ports[0]["port"] - - sys.argv = ["monitor"] + env_options.get("monitor_flags", []) - for k, v in kwargs.items(): - if k in ("port", "baud", "rts", "dtr", "environment", "project_dir"): - continue - k = "--" + k.replace("_", "-") - if k in env_options.get("monitor_flags", []): - continue - if isinstance(v, bool): - if v: - sys.argv.append(k) - elif isinstance(v, tuple): - for i in v: - sys.argv.extend([k, i]) - else: - sys.argv.extend([k, str(v)]) - - if kwargs["port"] and (set(["*", "?", "[", "]"]) & set(kwargs["port"])): + elif kwargs["port"] and (set(["*", "?", "[", "]"]) & set(kwargs["port"])): for item in util.get_serial_ports(): if fnmatch(item["port"], kwargs["port"]): kwargs["port"] = item["port"] break + # override system argv with patched options + sys.argv = ["monitor"] + options_to_argv( + kwargs, + project_options, + ignore=("port", "baud", "rts", "dtr", "environment", "project_dir"), + ) + try: miniterm.main( default_port=kwargs["port"], @@ -225,6 +208,37 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches raise exception.MinitermException(e) +def apply_project_monitor_options(cli_options, project_options): + for k in ("port", "speed", "rts", "dtr"): + k2 = "monitor_%s" % k + if k == "speed": + k = "baud" + if cli_options[k] is None and k2 in project_options: + cli_options[k] = project_options[k2] + if k != "port": + cli_options[k] = int(cli_options[k]) + return cli_options + + +def options_to_argv(cli_options, project_options, ignore=None): + result = project_options.get("monitor_flags", []) + for k, v in cli_options.items(): + if v is None or (ignore and k in ignore): + continue + k = "--" + k.replace("_", "-") + if k in project_options.get("monitor_flags", []): + continue + if isinstance(v, bool): + if v: + result.append(k) + elif isinstance(v, tuple): + for i in v: + result.extend([k, i]) + else: + result.extend([k, str(v)]) + return result + + def get_project_options(environment=None): config = ProjectConfig.get_instance() config.validate(envs=[environment] if environment else None) diff --git a/platformio/commands/remote.py b/platformio/commands/remote.py index a0707c1f..f4b17ade 100644 --- a/platformio/commands/remote.py +++ b/platformio/commands/remote.py @@ -12,17 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os 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 platformio import exception, fs -from platformio.commands.device import device_monitor as cmd_device_monitor +from platformio.commands import device from platformio.managers.core import pioplus_call # pylint: disable=unused-argument @@ -83,7 +82,7 @@ def remote_update(only_check, dry_run): @click.option( "-d", "--project-dir", - default=getcwd, + default=os.getcwd, type=click.Path( exists=True, file_okay=True, dir_okay=True, writable=True, resolve_path=True ), @@ -104,7 +103,7 @@ def remote_run(**kwargs): @click.option( "-d", "--project-dir", - default=getcwd, + default=os.getcwd, type=click.Path( exists=True, file_okay=False, dir_okay=True, writable=True, resolve_path=True ), @@ -130,9 +129,7 @@ def device_list(json_output): @remote_device.command("monitor", short_help="Monitor remote device") @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("--baud", "-b", type=int, help="Set baud rate, default=9600") @click.option( "--parity", default="N", @@ -183,25 +180,49 @@ def device_list(json_output): is_flag=True, help="Diagnostics: suppress non-error messages, default=Off", ) +@click.option( + "-d", + "--project-dir", + default=os.getcwd, + type=click.Path(exists=True, file_okay=False, dir_okay=True, resolve_path=True), +) +@click.option( + "-e", + "--environment", + help="Load configuration from `platformio.ini` and specified environment", +) @click.pass_context def device_monitor(ctx, **kwargs): + project_options = {} + try: + with fs.cd(kwargs["project_dir"]): + project_options = device.get_project_options(kwargs["environment"]) + kwargs = device.apply_project_monitor_options(kwargs, project_options) + except exception.NotPlatformIOProject: + pass + + kwargs["baud"] = kwargs["baud"] or 9600 + def _tx_target(sock_dir): + pioplus_argv = ["remote", "device", "monitor"] + pioplus_argv.extend(device.options_to_argv(kwargs, project_options)) + pioplus_argv.extend(["--sock", sock_dir]) try: - pioplus_call(sys.argv[1:] + ["--sock", sock_dir]) + pioplus_call(pioplus_argv) except exception.ReturnErrorCode: pass sock_dir = mkdtemp(suffix="pioplus") - sock_file = join(sock_dir, "sock") + sock_file = os.path.join(sock_dir, "sock") try: t = threading.Thread(target=_tx_target, args=(sock_dir,)) t.start() - while t.is_alive() and not isfile(sock_file): + while t.is_alive() and not os.path.isfile(sock_file): sleep(0.1) if not t.is_alive(): return kwargs["port"] = fs.get_file_contents(sock_file) - ctx.invoke(cmd_device_monitor, **kwargs) + ctx.invoke(device.device_monitor, **kwargs) t.join(2) finally: fs.rmtree(sock_dir) diff --git a/platformio/managers/core.py b/platformio/managers/core.py index 8e66aa19..1f8142b1 100644 --- a/platformio/managers/core.py +++ b/platformio/managers/core.py @@ -26,7 +26,7 @@ from platformio.project.config import ProjectConfig CORE_PACKAGES = { "contrib-piohome": "~3.0.0", "contrib-pysite": "~2.%d%d.0" % (sys.version_info[0], sys.version_info[1]), - "tool-pioplus": "^2.5.8", + "tool-pioplus": "^2.6.0", "tool-unity": "~1.20403.0", "tool-scons": "~2.20501.7" if PY2 else "~3.30101.0", "tool-cppcheck": "~1.189.0",