-
Notifications
You must be signed in to change notification settings - Fork 0
/
mg.py
123 lines (98 loc) · 3.9 KB
/
mg.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from argparse import ArgumentParser
from subprocess import Popen, PIPE
from PyFileSystem.fileSystem import get_files, get_directories
from os.path import join
MAIN = "main.py"
def __isMicroPythonDir(item: str):
return item.find(".") == -1
def __is_hidden(item: str):
return item[0] == "."
def windpath_to_unixpath(path: str):
if path.count("\\") <= 0: return path
return path.replace("\\", "/")
def cli():
parser = ArgumentParser(prog="mg",
description="MicroPython ESP32 proyect manager")
parser.add_argument("port", help="specify port where esp32 is connected")
parser.add_argument("--upload", help="upload the entire proyect, must containt a main.py on the root")
parser.add_argument("--tree", help="", action="store_true")
parser.add_argument("--clean", help="", action="store_true")
parser.add_argument("--run", help="run especify file")
return parser.parse_args()
def upload_command(path_to_proyect: str, device_port: str):
root_files = get_files(path_to_proyect)
if not MAIN in root_files:
print("Theres no '", MAIN, "' file on root folder of the proyect")
exit(0)
files_and_folders = root_files + get_directories(path_to_proyect)
for item in files_and_folders:
if __is_hidden(item): continue
p = Popen(f"ampy --port {device_port} put {join(path_to_proyect, item)}", stdout=PIPE, shell=True)
print(f"Uploading {join(path_to_proyect, item)}...")
p.communicate()
def run_from_board_command(file: str, device_port: str):
path = windpath_to_unixpath(path)
path = file.split("/")
if len(path) > 1:
path = "/".join(path[:-1])
else:
path = ""
p = Popen(f"ampy --port {device_port} ls {path}", stdout=PIPE, shell=True)
out_bytes, err = p.communicate()
out = out_bytes.decode("utf-8")
out = out.split("\n")
content = out[:-1] # Remove last element, is an empty line break
exists = False
for f in content:
_file = f.split("/")[-1]
if _file == file.split("/")[-1]:
exists = True
break
if exists:
p = Popen(f"ampy --port {device_port} run {file}", stdout=PIPE, shell=True)
print(f"Executing {file}...")
print(p.communicate())
else:
print(f"File {file} not exists")
def tree_command(path: str, device_port: str, deep: int):
p = Popen(f"ampy --port {device_port} ls {path}", stdout=PIPE, shell=True)
out_bytes, err = p.communicate()
out = out_bytes.decode("utf-8")
out = out.split("\n")
out = out[:-1] # Remove last element, is an empty line break
for item in out:
if deep > 0:
_item = item.split("/")[-1]
else:
_item = item[1:]
# Print \t
for i in range(0,deep):
print(" ", end="")
print(" ", _item)
if __isMicroPythonDir(item):
tree_command(join(path, _item), device_port, deep+1)
def clean_command(device_port: str):
p = Popen(f"ampy --port {device_port} ls", stdout=PIPE, shell=True)
out_bytes, err = p.communicate()
out = out_bytes.decode("utf-8")
out = out.split("\n")
out = out[:-1] # Remove last element, is an empty line break
for item in out:
# remove forward slash at the start of the item
_item = item[1:]
if __isMicroPythonDir(item):
p = Popen(f"ampy --port {device_port} rmdir {_item}", stdout=PIPE, shell=True)
p.communicate()
print(f"Directory {_item} was removed")
else:
p = Popen(f"ampy --port {device_port} rm {_item}", stdout=PIPE, shell=True)
p.communicate()
print(f"File {_item} was removed")
if __name__ == "__main__":
args = cli()
if args.tree:
tree_command("", args.port, 0)
if args.upload:
upload_command(args.upload, args.port)
if args.clean:
clean_command(args.port)