Handle project configuration (monitor, test, and upload options) for PIO Remote commands // Resolve #2591

This commit is contained in:
Ivan Kravets
2019-11-11 22:38:16 +02:00
parent 3c796ca7c8
commit 0328037b49
5 changed files with 82 additions and 42 deletions

View File

@@ -6,6 +6,11 @@ Release Notes
PlatformIO Core 4.0 PlatformIO Core 4.0
------------------- -------------------
4.1.1 (2019-??-??)
~~~~~~~~~~~~~~~~~~
* Handle project configuration (monitor, test, and upload options) for PIO Remote commands (`issue #2591 <https://github.com/platformio/platformio-core/issues/2591>`_)
4.1.0 (2019-11-07) 4.1.0 (2019-11-07)
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

2
docs

Submodule docs updated: fc2ae00cbb...5210aa16d7

View File

@@ -172,18 +172,11 @@ def device_list( # pylint: disable=too-many-branches
help="Load configuration from `platformio.ini` and specified environment", help="Load configuration from `platformio.ini` and specified environment",
) )
def device_monitor(**kwargs): # pylint: disable=too-many-branches def device_monitor(**kwargs): # pylint: disable=too-many-branches
env_options = {} project_options = {}
try: try:
with fs.cd(kwargs["project_dir"]): with fs.cd(kwargs["project_dir"]):
env_options = get_project_options(kwargs["environment"]) project_options = get_project_options(kwargs["environment"])
for k in ("port", "speed", "rts", "dtr"): kwargs = apply_project_monitor_options(kwargs, project_options)
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])
except exception.NotPlatformIOProject: except exception.NotPlatformIOProject:
pass pass
@@ -191,29 +184,19 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches
ports = util.get_serial_ports(filter_hwid=True) ports = util.get_serial_ports(filter_hwid=True)
if len(ports) == 1: if len(ports) == 1:
kwargs["port"] = ports[0]["port"] kwargs["port"] = ports[0]["port"]
elif kwargs["port"] and (set(["*", "?", "[", "]"]) & set(kwargs["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"])):
for item in util.get_serial_ports(): for item in util.get_serial_ports():
if fnmatch(item["port"], kwargs["port"]): if fnmatch(item["port"], kwargs["port"]):
kwargs["port"] = item["port"] kwargs["port"] = item["port"]
break 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: try:
miniterm.main( miniterm.main(
default_port=kwargs["port"], default_port=kwargs["port"],
@@ -225,6 +208,37 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches
raise exception.MinitermException(e) 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): def get_project_options(environment=None):
config = ProjectConfig.get_instance() config = ProjectConfig.get_instance()
config.validate(envs=[environment] if environment else None) config.validate(envs=[environment] if environment else None)

View File

@@ -12,17 +12,16 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import sys import sys
import threading import threading
from os import getcwd
from os.path import isfile, join
from tempfile import mkdtemp from tempfile import mkdtemp
from time import sleep from time import sleep
import click import click
from platformio import exception, fs 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 from platformio.managers.core import pioplus_call
# pylint: disable=unused-argument # pylint: disable=unused-argument
@@ -83,7 +82,7 @@ def remote_update(only_check, dry_run):
@click.option( @click.option(
"-d", "-d",
"--project-dir", "--project-dir",
default=getcwd, default=os.getcwd,
type=click.Path( type=click.Path(
exists=True, file_okay=True, dir_okay=True, writable=True, resolve_path=True exists=True, file_okay=True, dir_okay=True, writable=True, resolve_path=True
), ),
@@ -104,7 +103,7 @@ def remote_run(**kwargs):
@click.option( @click.option(
"-d", "-d",
"--project-dir", "--project-dir",
default=getcwd, default=os.getcwd,
type=click.Path( type=click.Path(
exists=True, file_okay=False, dir_okay=True, writable=True, resolve_path=True 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") @remote_device.command("monitor", short_help="Monitor remote device")
@click.option("--port", "-p", help="Port, a number or a device name") @click.option("--port", "-p", help="Port, a number or a device name")
@click.option( @click.option("--baud", "-b", type=int, help="Set baud rate, default=9600")
"--baud", "-b", type=int, default=9600, help="Set baud rate, default=9600"
)
@click.option( @click.option(
"--parity", "--parity",
default="N", default="N",
@@ -183,25 +180,49 @@ def device_list(json_output):
is_flag=True, is_flag=True,
help="Diagnostics: suppress non-error messages, default=Off", 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 @click.pass_context
def device_monitor(ctx, **kwargs): def device_monitor(ctx, **kwargs):
def _tx_target(sock_dir): project_options = {}
try: try:
pioplus_call(sys.argv[1:] + ["--sock", sock_dir]) 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(pioplus_argv)
except exception.ReturnErrorCode: except exception.ReturnErrorCode:
pass pass
sock_dir = mkdtemp(suffix="pioplus") sock_dir = mkdtemp(suffix="pioplus")
sock_file = join(sock_dir, "sock") sock_file = os.path.join(sock_dir, "sock")
try: try:
t = threading.Thread(target=_tx_target, args=(sock_dir,)) t = threading.Thread(target=_tx_target, args=(sock_dir,))
t.start() 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) sleep(0.1)
if not t.is_alive(): if not t.is_alive():
return return
kwargs["port"] = fs.get_file_contents(sock_file) kwargs["port"] = fs.get_file_contents(sock_file)
ctx.invoke(cmd_device_monitor, **kwargs) ctx.invoke(device.device_monitor, **kwargs)
t.join(2) t.join(2)
finally: finally:
fs.rmtree(sock_dir) fs.rmtree(sock_dir)

View File

@@ -26,7 +26,7 @@ from platformio.project.config import ProjectConfig
CORE_PACKAGES = { CORE_PACKAGES = {
"contrib-piohome": "~3.0.0", "contrib-piohome": "~3.0.0",
"contrib-pysite": "~2.%d%d.0" % (sys.version_info[0], sys.version_info[1]), "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-unity": "~1.20403.0",
"tool-scons": "~2.20501.7" if PY2 else "~3.30101.0", "tool-scons": "~2.20501.7" if PY2 else "~3.30101.0",
"tool-cppcheck": "~1.189.0", "tool-cppcheck": "~1.189.0",