Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-close REPL workaround, indent changes #198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion scripts/microupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import time
import sys
import os
import psutils
from contextlib import suppress
from typing import List, Iterable, TypeVar, Sequence, Set

Expand All @@ -46,11 +47,13 @@ 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)
Expand Down Expand Up @@ -134,6 +137,28 @@ 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:])