forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mg9.py
190 lines (161 loc) · 5.07 KB
/
mg9.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import gscommon as gs
import gsshell
import sublime
import threading
import os
import gsq
import time
import hashlib
import base64
import Queue
DOMAIN = 'MarGo9'
# customization of, or user-owned gocode and margo will no longer be supported
# so we'll hardcode the relevant paths and refer to them directly instead of relying on PATH
MARGO9_SRC = gs.dist_path('margo9')
GOCODE_SRC = gs.dist_path('something_borrowed/gocode')
MARGO9_BIN = gs.home_path('bin', 'gosublime.margo9.exe')
GOCODE_BIN = gs.home_path('bin', 'gosublime.gocode.exe')
def _sb(s):
bdir = gs.home_path('bin')
if s.startswith(bdir):
s = '~bin%s' % (s[len(bdir):])
return s
def _tp(s):
return (_sb(s), ('ok' if os.path.exists(s) else 'missing'))
def _so(out, err, start, end):
out = out.strip()
err = err.strip()
ok = not out and not err
if ok:
out = 'ok %0.3fs' % (end - start)
else:
out = '%s\n%s' % (out, err)
return (out.strip(), ok)
def _run(cmd, cwd=None, shell=False):
nv = {
'GOBIN': '',
'GOPATH': gs.dist_path('something_borrowed'),
}
return gsshell.run(cmd, shell=shell, cwd=cwd, env=nv)
def maybe_install():
if not os.path.exists(MARGO9_BIN) or not os.path.exists(GOCODE_BIN):
install('', True)
def install(aso_tokens, force_install):
init_start = time.time()
try:
os.makedirs(gs.home_path('bin'))
except:
pass
if not force_install and aso_tokens == _gen_tokens():
m_out = 'no'
g_out = 'no'
else:
start = time.time()
m_out, err, _ = _run(['go', 'build', '-o', MARGO9_BIN], cwd=MARGO9_SRC)
m_out, m_ok = _so(m_out, err, start, time.time())
# on windows the file cannot be replaced if it's running so close gocode first.
# in theory, mg9 has the same issue but since it's attached to a st2 instance,
# the only way to close it is to close st2 (which we're presumably already doing)
start = time.time()
if os.path.exists(GOCODE_BIN):
_run([GOCODE_BIN, 'close'])
g_out, err, _ = _run(['go', 'build', '-o', GOCODE_BIN], cwd=GOCODE_SRC)
g_out, g_ok = _so(g_out, err, start, time.time())
if m_ok and g_ok:
def f():
gs.aso().set('mg9_install_tokens', _gen_tokens())
gs.save_aso()
sublime.set_timeout(f, 0)
a = (
'GoSublime init (%0.3fs)' % (time.time() - init_start),
'| install margo9: %s' % m_out,
'| install gocode: %s' % g_out,
'| ~bin: %s' % gs.home_path('bin'),
'| margo9: %s (%s)' % _tp(MARGO9_BIN),
'| gocode: %s (%s)' % _tp(GOCODE_BIN),
)
gs.println(*a)
def _fasthash(fn):
try:
with open(fn) as f:
chunk = f.read(1024*8)
st = os.stat(fn)
return '%s:%d' % (hashlib.sha1(chunk).hexdigest(), st.st_size)
except Exception:
pass
return ''
def _read(fn):
s = ''
try:
with open(fn) as f:
s = f.read()
except Exception:
pass
return s
def _token(head, bin):
head = _read(gs.dist_path(head))
token = '%s %s' % (head.strip(), _fasthash(bin))
return token
def _gen_tokens():
return '%s\n%s' % (_token('margo9.head', MARGO9_BIN), _token('something_borrowed/gocode.head', GOCODE_BIN))
def do_init():
aso_tokens = gs.aso().get('mg9_install_tokens', '')
f = lambda: install(aso_tokens, False)
gsq.do('GoSublime', f, msg='Installing MarGo9 and Gocode', set_status=True)
def _gocode(args, env={}, input=None):
home = gs.home_path()
# gocode should store its settings here
nv = {
'XDG_CONFIG_HOME': home,
}
nv.update(env)
# until mg9 is in active use we'll fallback to existing gocode
bin = GOCODE_BIN if os.path.exists(GOCODE_BIN) else 'gocode'
cmd = gs.lst(bin, args)
return gsshell.run(cmd, input=input, env=nv, cwd=home)
def gocode(args, env={}, input=None):
last_propose = gs.attr('gocode.last_propose_builtins', False)
propose = gs.setting('complete_builtins', False)
if last_propose != propose:
gs.set_attr('gocode.last_propose_builtins', propose)
_gocode(['set', 'propose-builtins', 'true' if propose else 'false'])
last_gopath = gs.attr('gocode.last_gopath')
gopath = gs.getenv('GOPATH')
if gopath and gopath != last_gopath:
out, _, _ = gsshell.run(cmd=['go', 'env', 'GOOS', 'GOARCH'])
vars = out.strip().split()
if len(vars) == 2:
gs.set_attr('gocode.last_gopath', gopath)
libpath = []
osarch = '_'.join(vars)
for p in gopath.split(os.pathsep):
if p:
libpath.append(os.path.join(p, 'pkg', osarch))
_gocode(['set', 'lib-path', os.pathsep.join(libpath)])
return _gocode(args, env=env, input=input)
def do(method, arg, shell=False):
maybe_install()
header, _ = gs.json_encode({'method': method, 'token': 'mg9.call'})
body, _ = gs.json_encode(arg)
s = '%s %s' % (header, body)
s = 'base64:%s' % base64.b64encode(s)
out, err, _ = gsshell.run([MARGO9_BIN, '-do', s], stderr=None, shell=shell)
res = {'error': err}
if out:
try:
for ln in out.split('\n'):
ln = ln.strip()
if ln:
r, err = gs.json_decode(ln, {})
if err:
res = {'error': 'Invalid response %s' % err}
else:
if r.get('token') == 'mg9.call':
res = r.get('data') or {}
if gs.is_a({}, res) and r.get('error'):
r['error'] = res['error']
return res
res = {'error': 'Unexpected response %s' % r}
except Exception:
res = {'error': gs.traceback()}
return res