-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommand.py
More file actions
229 lines (179 loc) · 6.36 KB
/
command.py
File metadata and controls
229 lines (179 loc) · 6.36 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# This file is part of the uPiot project, https://github.com/gepd/upiot/
#
# MIT License
#
# Copyright (c) 2017 GEPD
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import time
import threading
import sublime
from re import findall
from sys import platform
from subprocess import Popen, PIPE
from functools import partial
from collections import deque
from ..tools import message, paths
from ..tools.thread_progress import ThreadProgress
_COMMAND_QUEUE = deque()
_BUSY = False
class AsyncProcess(object):
def __init__(self, cmd, listener):
self.listener = listener
self.killed = False
self.start_time = time.time()
self.proc = Popen(
cmd,
stdout=PIPE,
stderr=PIPE,
stdin=PIPE,
shell=True)
if(self.proc.stdout):
th = threading.Thread(target=self.read_stdout)
th.start()
ThreadProgress(th, '', '')
if(self.proc.stderr):
threading.Thread(target=self.read_stderr).start()
def kill(self):
"""Kill process
kill the encapsulated subprocess.Popen
"""
if(not self.killed):
self.killed = True
if(platform == 'win32'):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen("taskkill /PID " + str(self.proc.pid),
startupinfo=startupinfo)
else:
self.proc.terminate()
self.listener = None
def poll(self):
return self.proc.poll() is None
def exit_code(self):
"""
return the exit code
"""
return self.proc.poll()
def read_stdout(self):
"""Read stdout output
Reads the stdout outputs when the data is available
and send it to the listener to be printed
"""
while True:
data = os.read(self.proc.stdout.fileno(), 2 ** 15)
if(len(data) > 0):
if(self.listener):
self.listener.on_data(data)
else:
self.proc.stdout.close()
if(self.listener):
self.listener.on_finished(self)
break
def read_stderr(self):
"""Read stderr output
Reads the stderr outputs when the data is available
and send it to the listener to be printed
"""
while True:
data = os.read(self.proc.stderr.fileno(), 2 ** 15)
if len(data) > 0:
if self.listener:
self.listener.on_data(data)
else:
self.proc.stderr.close()
break
class Command:
txt = None
def run(self, cmd, port=None, working_dir="", kill=False, word_wrap=True):
self.window = sublime.active_window()
global _COMMAND_QUEUE
global _BUSY
# kill the process
if(kill):
if(self.proc):
self.proc.kill()
self.proc = None
return
if(_BUSY):
_COMMAND_QUEUE.append(cmd)
return
self.txt = message.open(port)
self.encoding = 'utf-8'
self.quiet = False
self.proc = None
cmd = prepare_command(cmd)
if not self.quiet:
if cmd:
cmd_string = findall(r'\"(.+?)\"', cmd)
cmd_string = os.path.normpath(cmd_string[0])
cmd_string = os.path.basename(cmd_string)
lindex = cmd.rfind('"')
cmd_string += cmd[lindex:].replace('"', '')
self.txt.print("\n\nRunning {}\n\n" .format(cmd_string))
if working_dir != "":
os.chdir(working_dir)
try:
_BUSY = True
self.proc = AsyncProcess(cmd, self)
except Exception as e:
pass
def on_data(self, data):
try:
characters = data.decode(self.encoding)
except:
characters = "[Decode error - output not " + self.encoding + "]\n"
# Normalize newlines, Sublime Text always uses a single \n separator
# in memory.
characters = characters.replace('\r\n', '\n').replace('\r', '\n')
self.txt.print(characters)
def finish(self, proc):
elapsed = time.time() - proc.start_time
exit_code = proc.exit_code()
if(exit_code == 0 or exit_code is None):
txt = "\n[Finished in {0:.1f}s]".format(elapsed)
self.txt.print(txt)
else:
txt = "\n[Finished in {0:.1f}s with exit code {1}]\n".format(
elapsed, exit_code)
self.txt.print(txt)
if(proc != self.proc):
return
if(exit_code == 0):
sublime.status_message("Build finished")
else:
sublime.status_message("Build finished with errors")
# run next command in the deque
run_next()
def on_finished(self, proc):
sublime.set_timeout(partial(self.finish, proc), 0)
def run_next():
global _COMMAND_QUEUE
global _BUSY
_BUSY = False
if(len(_COMMAND_QUEUE)):
Command().run(_COMMAND_QUEUE.popleft())
def prepare_command(options):
esptool = paths.esptool_file()
cmd = ['python', '"' + esptool + '"']
cmd.extend(options)
cmd.append("2>&1")
cmd = " ".join(cmd)
return cmd