-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_uploader.py
executable file
·78 lines (68 loc) · 2.39 KB
/
file_uploader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import argparse
import logging
import sys
import os
sys.path.append('./common_lib/libraries')
from micro_python_board import MicroPythonBoard
from board import ComPort
import discovery
def main(upload: str, repl: str = ""):
board = object()
if repl and len(repl) > 3:
board = MicroPythonBoard(repl=ComPort({'device': repl}))
else:
boards = discovery.get_connected_boards()
if len(boards) == 0:
logging.error("No boards found")
exit(1)
choice = 0
if len(boards) > 1:
print("Which board do you want to upload to?")
for i, board in enumerate(boards):
print(f"{i}: {board.id} {board.ports}")
choice = int(input("Enter the number of the board: "))
board = boards[choice]
# check if upload is a file or directory
files = []
if os.path.isfile(upload):
files.append(upload)
elif os.path.isdir(upload):
# get all files in directory and append them to the directory
files.extend(f"{upload}/{f}" for f in os.listdir(upload))
else:
logging.error(f"Error! {upload} is not a file or directory.")
exit(1)
board.open_ports()
board.close_repl_uart()
board.open_raw_repl_uart()
for file in files:
logging.info(f"Uploading {file}")
# strip path from file name
file_name = file.split("/")[-1]
board.upload_py_file(file, file_name)
print("Done")
board.close_raw_repl_uart()
board.close_ports()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="File Uploader",
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Tool to upload files onto a MicroPython board.",
)
parser.add_argument(
"-d", "--debug", action="store_true", help="Enable verbose debug messages"
)
parser.add_argument("-u", "--upload", required=True,
help="upload a file or all files in directory")
parser.add_argument(
"-r", "--repl", help="The REPL com port to use for file upload"
)
logging.basicConfig(
format="%(asctime)s | %(levelname)s | %(message)s", level=logging.INFO
)
args, unknown = parser.parse_known_args()
if args.debug:
logging.info("Debugging mode enabled")
logging.getLogger().setLevel(logging.DEBUG)
main(args.upload, args.repl)