-
Notifications
You must be signed in to change notification settings - Fork 3
/
QuickExec.py
173 lines (137 loc) · 4.81 KB
/
QuickExec.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
import sublime, sublime_plugin
import os, sys
import threading
from threading import Thread
import subprocess
import functools
import time
import gc
def objects_by_id(id_):
for obj in gc.get_objects():
if id(obj) == id_:
return obj
raise Exception("No found")
class ProcessListener(object):
def on_data(self, proc, data):
pass
def on_finished(self, proc, alldata):
pass
# Encapsulates subprocess.Popen, forwarding stdout to a supplied
# ProcessListener (on a separate thread)
class AsyncProcess(object):
# "path" is an option in build systems
# "shell" is an options in build systems
def __init__(self, arg_list, env, listener, path="", shell=False):
self.listener = listener
self.killed = False
self.start_time = time.time()
self.alldata = ''
self.stdout_thread = None
self.stderr_thread = None
# Hide the console window on Windows
startupinfo = None
if os.name == "nt":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Set temporary PATH to locate executable in arg_list
if path:
old_path = os.environ["PATH"]
# The user decides in the build system whether he wants to append $PATH
# or tuck it at the front: "$PATH;C:\\new\\path", "C:\\new\\path;$PATH"
os.environ["PATH"] = os.path.expandvars(path)
proc_env = os.environ.copy()
proc_env.update(env)
for k, v in tuple(proc_env.items()):
proc_env[k] = os.path.expandvars(v)
self.proc = subprocess.Popen(arg_list, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, shell=shell)
if path:
os.environ["PATH"] = old_path
if self.proc.stdout:
self.stdout_thread = Thread(target=self.read_stdout, args=())
self.stdout_thread.start()
if self.proc.stderr:
self.stderr_thread = Thread(target=self.read_stderr, args=())
self.stderr_thread.start()
def kill(self):
if not self.killed:
self.killed = True
self.proc.terminate()
self.listener = None
def poll(self):
return self.proc.poll() == None
def exit_code(self):
return self.proc.poll()
def read_stdout(self):
for row in self.proc.stdout.readlines():
self.alldata += str(row)
self.listener.on_data(self, self.alldata)
self.proc.stdout.close()
# self.stdout_thread = None
# self.stderr_thread = None
self.proc.terminate()
sublime.set_timeout(lambda: self.listener.on_finished(self, self.alldata), 0)
def read_stderr(self):
data = ''
for row in self.proc.stderr.readlines():
data += str(row)
if data != "":
if self.listener:
self.listener.on_data(self, data)
else:
self.proc.stderr.close()
self.proc.terminate()
break
class QuickExecCommand(sublime_plugin.WindowCommand, ProcessListener):
def run(self, cmd = [], file_regex = "", line_regex = "", working_dir = "",
encoding = "utf-8", env = {}, kill = False, listenerid = "",
# Catches "path" and "shell"
**kwargs):
listener = objects_by_id(listenerid)
# TODO: arguments can only be in JSON format, so i need to convert
# function pointer 'listener' to string and then back to function pointer.
if kill:
if self.proc:
self.proc.kill()
self.proc = None
return
# Default the to the current files directory if no working directory was given
if (working_dir == "" and self.window.active_view()
and self.window.active_view().file_name()):
working_dir = os.path.dirname(self.window.active_view().file_name())
self.encoding = encoding
self.proc = None
sublime.status_message("BUILDING")
merged_env = env.copy()
if self.window.active_view():
user_env = self.window.active_view().settings().get('build_env')
if user_env:
merged_env.update(user_env)
# Change to the working dir, rather than spawning the process with it,
# so that emitted working dir relative path names make sense
if working_dir != "":
os.chdir(working_dir)
err_type = OSError
if os.name == "nt":
err_type = WindowsError
if not listener:
print("Oh shit...")
listener = self
try:
# Forward kwargs to AsyncProcess
self.proc = AsyncProcess(cmd, merged_env, listener, **kwargs)
except err_type as e:
pass
def is_enabled(self, kill = False):
if kill:
return hasattr(self, 'proc') and self.proc and self.proc.poll()
else:
return True
def finish(self, proc, alldata):
if proc != self.proc:
return
def on_data(self, proc, data):
pass
#sublime.set_timeout(functools.partial(self.append_data, proc, data), 0)
def on_finished(self, proc, alldata):
sublime.set_timeout(functools.partial(self.finish, proc, alldata), 0)