-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli_manager.py
174 lines (132 loc) · 4.36 KB
/
cli_manager.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
The cli manager file.
"""
import subprocess
import threading
import time
ERROR_TERMS = ["Error opening sketch", "Error during build", "exit status"]
class CliManager:
"""
Controls the flow of commands to the CLI
Output should be continuous with previous outputs seperated with clear headers -
Succes,
Fail,
Command
Change file -> menu
add cli manager button (full screen)
"""
def __init__(self, path):
self.path = path
self.commands = {}
self.outputs = {}
self.running = False
self.enabled = True
self.update_core_index()
self.update_library_index()
def update_core_index(self):
"""
Updates the arduino core packages available and waits for command to finish
"""
with subprocess.Popen(
f"\"{self.path}\" core update-index", stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True) as process:
process.communicate()
def update_library_index(self):
"""
Update the arduino library packages available and waits for command to finish
"""
with subprocess.Popen(
f"\"{self.path}\" lib update-index", stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True) as process:
process.communicate()
def get_output(self):
"""
Removes first output from the queue
"""
try:
key = list(self.outputs.keys())[0]
return key, self.outputs.pop(key)
except IndexError:
return None, None
def get_status(self, key="upload"):
"""
Gets the current status of what tasks are still to be done.
"""
for cmd in self.commands.items():
if cmd[1] == key:
return True
return False
def check_for_upload(self, compile_out:str) -> bool:
"""
Checks whether or not to upload if the compile was a success
or not.
Args:
compile_out (str): the output of the compile
Returns:
bool: whether or not to compile
"""
for item in ERROR_TERMS:
if item in compile_out:
return False
return True
def threaded_call(self):
"""
Puts the command on the thread to be non blocking
"""
prev_output = ""
while self.enabled:
if not self.commands.keys():
time.sleep(0.25)
continue
cmd = list(self.commands.keys()).pop(0)
if "upload" in cmd:
if not self.check_for_upload(prev_output):
self.commands.pop(cmd)
continue
cmd_type = self.commands[cmd]
self.running = True
prev_output = self.get_cmd_output(cmd).decode("UTF-8")
self.outputs[prev_output] = cmd_type
self.commands.pop(cmd)
self.running = False
def communicate(self, cmd, cmd_type="usr"):
"""
Calls the thread to run the command
Args:
cmd (string): the command to run in terminal
cmd_type (string): what type of command
('usr' = user input, 'compile' = compile, 'upload' = upload)
"""
self.commands[cmd] = cmd_type
def get_cmd_output(self, cmd):
"""
Runs the command in the program flow
Args:
cmd (string): the comand to run in terminal
Returns:
string: the output from CLI
"""
with subprocess.Popen(
f"\"{self.path}\" {cmd}", stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True) as process:
output = process.communicate()
return output[0]
def terminate(self):
"""
Ends the threaded function if it is running.
"""
self.enabled = False
if __name__ == "__main__":
PATH = "./Externals/arduino-cli-windows.exe"
cli = CliManager(PATH)
command_runner = threading.Thread(target=cli.threaded_call, args=(),)
command_runner.start()
while True:
command = input(">>> ")
if command.lower() == "exit":
break
cli.communicate(command)
cli.terminate()