-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKillableCmd.py
More file actions
63 lines (43 loc) · 1.5 KB
/
KillableCmd.py
File metadata and controls
63 lines (43 loc) · 1.5 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
import os
import subprocess
import threading
import sublime, sublime_plugin
from sys import platform as _platform
class KillableCmd(threading.Thread):
def __init__(self, cmd, timeout, shell):
threading.Thread.__init__(self)
self.cmd = cmd
self.shell = shell
self.timeout = timeout
def run(self):
startupinfo = None
if sublime.platform() == "windows":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
out, err = "", ""
try:
self.p = subprocess.Popen(self.cmd,
startupinfo=startupinfo,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=self.shell,
env=os.environ.copy())
out, err = map(lambda x: x.decode("ascii").replace("\r", ""), self.p.communicate())
except Exception as tryException:
if _platform == "darwin":
self.returnValue = """Unfortunately, EvalPrinter currently has some issues with OS X.
Subscribe to the following issue to get notified about the fix: https://github.com/philippotto/Sublime-EvalPrinter/issues/1"""
return
print("An error occured while opening a subprocess", tryException)
if err or self.p.returncode != 0:
self.returnValue = out + err
return
self.returnValue = out
def Run(self):
self.start()
self.join(self.timeout)
if self.is_alive():
self.p.terminate()
self.join()
return "The execution took longer than " + str(self.timeout) + " second(s). Aborting..."
return self.returnValue