fix(dev): run mpremote from PATH or python -m

Made-with: Cursor
This commit is contained in:
pi
2026-04-05 16:41:23 +12:00
parent fb53f900fb
commit dc19877132

27
dev.py
View File

@@ -1,10 +1,20 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import shutil
import subprocess import subprocess
import serial import serial
import sys import sys
from pathlib import Path from pathlib import Path
def mpremote_base():
"""mpremote on PATH, or same interpreter as this script (e.g. pipenv venv)."""
exe = shutil.which("mpremote")
if exe:
return [exe]
return [sys.executable, "-m", "mpremote"]
print(sys.argv) print(sys.argv)
# Extract port (first arg if it's not a command) # Extract port (first arg if it's not a command)
@@ -19,17 +29,20 @@ for cmd in sys.argv[1:]:
match cmd: match cmd:
case "src": case "src":
if port: if port:
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", ".", ":" ], cwd="src") subprocess.call(
[*mpremote_base(), "connect", port, "fs", "cp", "-r", ".", ":"],
cwd="src",
)
else: else:
print("Error: Port required for 'src' command") print("Error: Port required for 'src' command")
case "lib": case "lib":
if port: if port:
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", "lib", ":" ]) subprocess.call([*mpremote_base(), "connect", port, "fs", "cp", "-r", "lib", ":"])
else: else:
print("Error: Port required for 'lib' command") print("Error: Port required for 'lib' command")
case "ls": case "ls":
if port: if port:
subprocess.call(["mpremote", "connect", port, "fs", "ls", ":" ]) subprocess.call([*mpremote_base(), "connect", port, "fs", "ls", ":"])
else: else:
print("Error: Port required for 'ls' command") print("Error: Port required for 'ls' command")
case "reset": case "reset":
@@ -49,7 +62,7 @@ for cmd in sys.argv[1:]:
print("Error: Port required for 'follow' command") print("Error: Port required for 'follow' command")
case "db": case "db":
if port: if port:
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", "db", ":" ]) subprocess.call([*mpremote_base(), "connect", port, "fs", "cp", "-r", "db", ":"])
else: else:
print("Error: Port required for 'db' command") print("Error: Port required for 'db' command")
case "test": case "test":
@@ -64,7 +77,7 @@ for cmd in sys.argv[1:]:
for test_file in test_files: for test_file in test_files:
print(f"Running {test_file}") print(f"Running {test_file}")
code = subprocess.call( code = subprocess.call(
["mpremote", "connect", port, "run", test_file] [*mpremote_base(), "connect", port, "run", test_file]
) )
if code != 0: if code != 0:
failed.append((test_file, code)) failed.append((test_file, code))
@@ -73,6 +86,8 @@ for cmd in sys.argv[1:]:
for test_file, code in failed: for test_file, code in failed:
print(f" {test_file} (exit {code})") print(f" {test_file} (exit {code})")
else: else:
subprocess.call(["mpremote", "connect", port, "run", "test/all.py"]) subprocess.call(
[*mpremote_base(), "connect", port, "run", "test/all.py"]
)
else: else:
print("Error: Port required for 'test' command") print("Error: Port required for 'test' command")