From 7a98267e816ce2722c36cf126417f0ab526628ae Mon Sep 17 00:00:00 2001 From: Micah Beasley Date: Sat, 2 Jul 2022 05:16:56 -0500 Subject: [PATCH 1/2] Add auto-close REPL workaround adds requirement: psutils This is intended to be a quality of life improvement while we hope for #139 to come to fruition; to be honest, since it has been a year and 3 months since there has been any updates to the WIP: enhancement issue I don't have a lot of faith this will be completed anytime soon. Because of that I figured out how to check the system's running processes for the MicroPython REPL and close/kill it so the flash process (or script) will continue successfully. I tried all day to implement restarting the REPL when the flash was complete, but I can't figure out a way to launch the MicroPython REPL the way it was originally (as a terminal window in PyCharm). I am a junior programmer so feedback is appreciated! --- scripts/microupload.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/scripts/microupload.py b/scripts/microupload.py index 56bcc5d5..47a4b3b5 100644 --- a/scripts/microupload.py +++ b/scripts/microupload.py @@ -27,6 +27,7 @@ import time import sys import os +import psutils from contextlib import suppress from typing import List, Iterable, TypeVar, Sequence, Set @@ -46,17 +47,19 @@ def main(args: List[str]) -> None: opts = docopt(__doc__, argv=args) verbose = opts['--verbose'] root = opts['PATH'] - + chdir = opts['--chdir'] if chdir: os.chdir(chdir) + check_for_repl() + port = opts['PORT'] print('Connecting to {}'.format(port), file=sys.stderr) board = Pyboard(port) files = Files(board) rel_root = os.path.relpath(root, os.getcwd()) - + wait_for_board() if os.path.isdir(root): @@ -134,6 +137,29 @@ def progress(msg: str, xs: Sequence[T]) -> Iterable[T]: sys.stderr.write('\n') sys.stderr.flush() +def check_for_repl(): + print("Checking for MicroPython REPL processes...", file=sys.stderr) + targets = [ + proc for proc in psutil.process_uter(attrs=['name', 'username','cmdline']) + if 'python' in proc.info['name'] + and "microrepl.py" in str(proc.cmdline()) + ] + if len(targets) != 0: + print("Oops, it looks like the MicroPython REPL is still running, asking it to close now...", file=sys.stderr) + for p in targets: + p.terminate() + _, alive = psutil.wait_procs(targets, timeout=300) + + if len(alive) >= 1: + print("Well, it looks like the MicroPython REPL is still running, trying to kill it now...", file=sys.stderr) + for p in targets: + p.kill() + _, notdead = psutil.wait_procs(targets, timeout=300) + + if len(notdead) >= 1: + print("Unable to close the MicroPython REPL automatically...", file=sys.stderr) + print("Please close it manually and try again", file=sys.stderr) + sys.exit() if __name__ == '__main__': main(sys.argv[1:]) From e592d9c355422cda87ea0daff1085ee27f244e42 Mon Sep 17 00:00:00 2001 From: Micah Beasley Date: Sat, 2 Jul 2022 05:28:44 -0500 Subject: [PATCH 2/2] Correct indent, trailing spaces Corrected indent issue from replicating code from other machine, removed trailing spaces and unnecessary white space characters. --- scripts/microupload.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/scripts/microupload.py b/scripts/microupload.py index 47a4b3b5..1a700caa 100644 --- a/scripts/microupload.py +++ b/scripts/microupload.py @@ -52,14 +52,14 @@ def main(args: List[str]) -> None: if chdir: os.chdir(chdir) - check_for_repl() - + check_for_repl() + port = opts['PORT'] print('Connecting to {}'.format(port), file=sys.stderr) board = Pyboard(port) files = Files(board) rel_root = os.path.relpath(root, os.getcwd()) - + wait_for_board() if os.path.isdir(root): @@ -149,17 +149,16 @@ def check_for_repl(): for p in targets: p.terminate() _, alive = psutil.wait_procs(targets, timeout=300) - - if len(alive) >= 1: - print("Well, it looks like the MicroPython REPL is still running, trying to kill it now...", file=sys.stderr) - for p in targets: - p.kill() - _, notdead = psutil.wait_procs(targets, timeout=300) - - if len(notdead) >= 1: - print("Unable to close the MicroPython REPL automatically...", file=sys.stderr) - print("Please close it manually and try again", file=sys.stderr) - sys.exit() + if len(alive) >= 1: + print("Well, it looks like the MicroPython REPL is still running, trying to kill it now...", file=sys.stderr) + for p in targets: + p.kill() + _, notdead = psutil.wait_procs(targets, timeout=300) + if len(notdead) >= 1: + print("Unable to close the MicroPython REPL automatically...", file=sys.stderr) + print("Please close it manually and try again", file=sys.stderr) + sys.exit() + if __name__ == '__main__': main(sys.argv[1:])