-
Notifications
You must be signed in to change notification settings - Fork 5
/
exec_in_window.py
158 lines (130 loc) · 5.6 KB
/
exec_in_window.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
import sublime, sublime_plugin
import os, sys
import thread
import subprocess
import functools
import time
import os
import tempfile
import codecs
execcmd = __import__("exec")
class ExecInWindowCommand(execcmd.sublime_plugin.WindowCommand, execcmd.ProcessListener):
def run(self, cmd = [], file_regex = "", line_regex = "", working_dir = "",
encoding = "utf-8", env = {}, quiet = False, kill = False,
# Catches "path" and "shell"
**kwargs):
if kill:
if self.proc:
self.proc.kill()
self.proc = None
self.append_data(None, "[Cancelled]")
return
# Create temporary file if doesn't exists
#
if self.window.active_view().file_name():
self.file = self.window.active_view().file_name()
else:
self.file = self.create_temp_file()
cmd[cmd.index('')] = self.file
self.output_view = self.window.open_file("Build result")
self.output_view.set_scratch(True)
self.output_view.set_read_only(False)
# Default the to the current files directory if no working directory was given
if (working_dir == "" and self.window.active_view() and self.file):
working_dir = os.path.dirname(self.file)
self.output_view.settings().set("result_file_regex", file_regex)
self.output_view.settings().set("result_line_regex", line_regex)
self.output_view.settings().set("result_base_dir", working_dir)
self.encoding = encoding
self.quiet = quiet
self.proc = None
execcmd.sublime.set_timeout(functools.partial(self.clear_view), 0)
if not self.quiet:
print "Running " + " ".join(cmd)
execcmd.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
try:
# Forward kwargs to AsyncProcess
self.proc = execcmd.AsyncProcess(cmd, merged_env, self, **kwargs)
except err_type as e:
self.append_data(None, str(e) + "\n")
self.append_data(None, "[cmd: " + str(cmd) + "]\n")
self.append_data(None, "[dir: " + str(os.getcwdu()) + "]\n")
if "PATH" in merged_env:
self.append_data(None, "[path: " + str(merged_env["PATH"]) + "]\n")
else:
self.append_data(None, "[path: " + str(os.environ["PATH"]) + "]\n")
if not self.quiet:
self.append_data(None, "[Finished]")
def is_enabled(self, kill = False):
if kill:
return hasattr(self, 'proc') and self.proc and self.proc.poll()
else:
return True
def clear_view(self):
edit = self.output_view.begin_edit()
self.output_view.erase(edit, sublime.Region(0, self.output_view.size()))
self.output_view.end_edit(edit)
def create_temp_file(self):
view = self.window.active_view()
region = execcmd.sublime.Region(0, view.size())
content = view.substr(region)
filename = '%s.tmp' % view.id()
path = os.path.join(tempfile.gettempdir(), filename)
file = open(path, 'w')
file.write(content.encode('utf-8'))
file.close()
return path
def append_data(self, proc, data):
if proc != self.proc:
# a second call to exec has been made before the first one
# finished, ignore it instead of intermingling the output.
if proc:
proc.kill()
return
try:
str = data.decode(self.encoding)
except:
str = "[Decode error - output not " + self.encoding + "]\n"
proc = None
# Normalize newlines, Sublime Text always uses a single \n separator
# in memory.
str = str.replace('\r\n', '\n').replace('\r', '\n')
selection_was_at_end = (len(self.output_view.sel()) == 1
and self.output_view.sel()[0]
== execcmd.sublime.Region(self.output_view.size()))
edit = self.output_view.begin_edit()
self.output_view.insert(edit, self.output_view.size(), str)
if selection_was_at_end:
self.output_view.show(self.output_view.size())
self.output_view.end_edit(edit)
def finish(self, proc):
if not self.quiet:
elapsed = time.time() - proc.start_time
exit_code = proc.exit_code()
if exit_code == 0 or exit_code == None:
self.append_data(proc, ("\n[Finished in %.1fs]\n") % (elapsed))
else:
self.append_data(proc, ("\n[Finished in %.1fs with exit code %d]\n") % (elapsed, exit_code))
if proc != self.proc:
return
errs = self.output_view.find_all_results()
if len(errs) == 0:
execcmd.sublime.status_message("Build finished")
else:
execcmd.sublime.status_message(("Build finished with %d errors") % len(errs))
def on_data(self, proc, data):
execcmd.sublime.set_timeout(functools.partial(self.append_data, proc, data), 0)
def on_finished(self, proc):
execcmd.sublime.set_timeout(functools.partial(self.finish, proc), 0)