forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mg9.py
449 lines (380 loc) · 10.4 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import atexit
import base64
import gscommon as gs
import gsq
import gsshell
import hashlib
import json
import margo
import os
import Queue
import re
import sublime
import sublime_plugin
import threading
import time
import uuid
DOMAIN = 'MarGo9'
REQUEST_PREFIX = '%s.rqst.' % DOMAIN
PROC_ATTR_NAME = 'mg9.proc'
CHANGES_SPLIT_PAT = re.compile(r'^##', re.MULTILINE)
CHANGES_MATCH_PAT = re.compile(r'^\s*(r[\d.]+[-]\d+)\s+(.+?)\s*$', re.DOTALL)
CHANGELOG_FN = gs.dist_path("CHANGELOG.md")
CHANGES = []
with open(CHANGELOG_FN) as f:
s = f.read()
for m in CHANGES_SPLIT_PAT.split(s):
m = CHANGES_MATCH_PAT.match(m)
if m:
CHANGES.append((m.group(1), m.group(2)))
CHANGES.sort(reverse=True)
REV = CHANGES[0][0]
# customization of, or user-owned margo will no longer be supported
# so we'll hardcode the relevant paths and refer to them directly instead of relying on PATH
MARGO0_SRC = gs.dist_path('something_borrowed/margo0')
MARGO9_SRC = gs.dist_path('margo9')
MARGO0_EXE = 'gosublime.%s.margo0.exe' % REV
MARGO9_EXE = 'gosublime.%s.margo9.exe' % REV
MARGO0_BIN = gs.home_path('bin', MARGO0_EXE)
MARGO9_BIN = gs.home_path('bin', MARGO9_EXE)
if not gs.checked(DOMAIN, '_vars'):
_send_q = Queue.Queue()
_recv_q = Queue.Queue()
class Request(object):
def __init__(self, f, method='', token=''):
self.f = f
self.tm = time.time()
self.method = method
if token:
self.token = token
else:
self.token = 'mg9.autoken.%s' % uuid.uuid4()
class GsSanityCheckCommand(sublime_plugin.WindowCommand):
def run(self):
s = 'GoSublime Sanity Check\n\n%s' % '\n'.join(['%7s: %s' % ln for ln in _sanity_check()])
gs.show_output(DOMAIN, s, print_output=False)
def _sanity_check(env={}):
if not env:
env = gs.env()
return [
('version', REV),
('~bin', '%s' % gs.home_path('bin')),
('margo0', '%s (%s)' % _tp(MARGO0_BIN)),
('margo9', '%s (%s)' % _tp(MARGO9_BIN)),
('GOROOT', '%s' % env.get('GOROOT', '(not set)')),
('GOPATH', '%s' % env.get('GOPATH', '(not set)')),
('GOBIN', '%s (should usually be (not set))' % env.get('GOBIN', '(not set)')),
]
def _check_changes():
def cb():
aso = gs.aso()
old_rev = aso.get('changelog.rev', '')
if REV > old_rev:
aso.set('changelog.rev', REV)
gs.save_aso()
gs.focus(CHANGELOG_FN)
sublime.set_timeout(cb, 0)
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 _bins_exist():
return os.path.exists(MARGO9_BIN) and os.path.exists(MARGO0_BIN)
def maybe_install():
if not _bins_exist():
install('', True)
def install(aso_tokens, force_install):
k = 'mg9.install.%s' % REV
if gs.attr(k, False):
gs.error(DOMAIN, 'Installation aborted. Install command already called for GoSublime %s.' % REV)
return
gs.set_attr(k, True)
init_start = time.time()
try:
os.makedirs(gs.home_path('bin'))
except:
pass
if not force_install and _bins_exist() and aso_tokens == _gen_tokens():
m0_out = 'no'
m_out = 'no'
else:
gs.notify('GoSublime', 'Installing MarGo0')
start = time.time()
m0_out, err, _ = _run(['go', 'build', '-o', MARGO0_BIN], cwd=MARGO0_SRC)
m0_out, m0_ok = _so(m0_out, err, start, time.time())
if os.path.exists(MARGO0_BIN):
margo.bye_ni()
gs.notify('GoSublime', 'Installing MarGo9')
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())
if m_ok and m0_ok:
def f():
gs.aso().set('mg9_install_tokens', _gen_tokens())
gs.save_aso()
sublime.set_timeout(f, 0)
gs.notify('GoSublime', 'Syncing environment variables')
out, err, _ = gsshell.run([MARGO9_EXE, '-env'], cwd=gs.home_path(), shell=True)
# notify this early so we don't mask any notices below
gs.notify('GoSublime', 'Ready')
_check_changes()
if err:
gs.notice(DOMAIN, 'Cannot run get env vars: %s' % (MARGO9_EXE, err))
else:
env, err = gs.json_decode(out, {})
if err:
gs.notice(DOMAIN, 'Cannot load env vars: %s\nenv output: %s' % (err, out))
else:
gs.environ9.update(env)
e = gs.env()
a = [
'GoSublime init (%0.3fs)' % (time.time() - init_start),
'| install margo0: %s' % m0_out,
'| install margo9: %s' % m_out,
]
a.extend(['| %14s: %s' % ln for ln in _sanity_check(e)])
gs.println(*a)
missing = [k for k in ('GOROOT', 'GOPATH') if not e.get(k)]
if missing:
gs.notice(DOMAIN, "Missing environment variable(s): %s" % ', '.join(missing))
killSrv()
start = time.time()
# acall('ping', {}, lambda res, err: gs.println('MarGo Ready %0.3fs' % (time.time() - start)))
report_x = lambda: gs.println("GoSublime: Exception while cleaning up old binaries", gs.traceback())
try:
d = gs.home_path('bin')
for fn in os.listdir(d):
try:
if fn not in (MARGO9_EXE, MARGO0_EXE) and fn.startswith(('gosublime', 'gocode', 'margo')):
fn = os.path.join(d, fn)
gs.println("GoSublime: removing old binary: %s" % fn)
os.remove(fn)
except Exception:
report_x()
except Exception:
report_x()
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 REV
def do_init():
aso_tokens = gs.aso().get('mg9_install_tokens', '')
f = lambda: install(aso_tokens, False)
gsq.do('GoSublime', f, msg='Installing MarGo', set_status=True)
def completion_options(m={}):
res, err = bcall('gocode_options', {})
res = gs.dval(res.get('options'), {})
return res, err
def complete(fn, src, pos):
home = gs.home_path()
builtins = (gs.setting('autocomplete_builtins') is True or gs.setting('complete_builtins') is True)
res, err = bcall('gocode_complete', {
'Dir': gs.basedir_or_cwd(fn),
'Builtins': builtins,
'Fn': fn or '',
'Src': src or '',
'Pos': pos or 0,
'Home': home,
'Env': gs.env({
'XDG_CONFIG_HOME': home,
}),
})
res = gs.dval(res.get('completions'), [])
return res, err
def fmt(fn, src):
res, err = bcall('fmt', {
'fn': fn or '',
'src': src or '',
'tabIndent': gs.setting('fmt_tab_indent'),
'tabWidth': gs.setting('fmt_tab_width'),
})
return res.get('src', ''), err
def import_paths(fn, src, f):
acall('import_paths', {
'fn': fn or '',
'src': src or '',
'env': gs.env(),
}, f)
def pkg_name(fn, src):
res, err = bcall('pkg', {
'fn': fn or '',
'src': src or '',
})
return res.get('name'), err
def declarations(fn, src, pkg_dir):
return bcall('declarations', {
'fn': fn or '',
'src': src,
'env': gs.env(),
'pkg_dir': pkg_dir,
})
def imports(fn, src, toggle):
return bcall('imports', {
'fn': fn or '',
'src': src or '',
'toggle': toggle or [],
'tabIndent': gs.setting('fmt_tab_indent'),
'tabWidth': gs.setting('fmt_tab_width'),
})
def acall(method, arg, cb):
if not gs.checked(DOMAIN, 'launch _send'):
gsq.launch(DOMAIN, _send)
_send_q.put((method, arg, cb))
def bcall(method, arg):
q = Queue.Queue()
acall(method, arg, lambda r,e: q.put((r, e)))
try:
res, err = q.get(True, 1)
return res, err
except:
return {}, 'Blocking Call: Timeout'
def expand_jdata(v):
if gs.is_a(v, {}):
for k in v:
v[k] = expand_jdata(v[k])
elif gs.is_a_string(v) and v.startswith('base64:'):
try:
v = base64.b64decode(v[7:])
except Exception:
v = ''
gs.error_traceback(DOMAIN)
return v
def _recv():
while True:
try:
ln = _recv_q.get()
try:
ln = ln.strip()
if ln:
r, _ = gs.json_decode(ln, {})
token = r.get('token', '')
k = REQUEST_PREFIX+token
req = gs.attr(k)
gs.del_attr(k)
if req and req.f:
gs.debug(DOMAIN, "margo response: method: %s, token: %s, dur: %0.3fs, err: `%s'" % (
req.method,
req.token,
(time.time() - req.tm),
r.get('error', ''),
))
dat = expand_jdata(r.get('data', {}))
err = r.get('error', '')
try:
keep = req.f(dat, err) is not True
if keep:
req.tm = time.time()
gs.set_attr(k, req)
except Exception:
gs.error_traceback(DOMAIN)
else:
gs.debug(DOMAIN, 'Ignoring margo: token: %s' % token)
except Exception:
gs.println(gs.traceback())
except Exception:
gs.println(gs.traceback())
break
def _send():
while True:
try:
try:
method, arg, cb = _send_q.get()
proc = gs.attr(PROC_ATTR_NAME)
if not proc or proc.poll() is not None:
killSrv()
maybe_install()
if not gs.checked(DOMAIN, 'launch _recv'):
gsq.launch(DOMAIN, _recv)
proc, _, err = gsshell.proc([MARGO9_BIN, '-poll=30'], stderr=gs.LOGFILE ,env={
'XDG_CONFIG_HOME': gs.home_path(),
})
gs.set_attr(PROC_ATTR_NAME, proc)
if not proc:
gs.notice(DOMAIN, 'Cannot start MarGo: %s' % err)
try:
cb({}, 'Abort. Cannot start MarGo')
except:
pass
continue
gsq.launch(DOMAIN, lambda: _read_stdout(proc))
req = Request(f=cb, method=method)
gs.set_attr(REQUEST_PREFIX+req.token, req)
gs.debug(DOMAIN, 'margo request: method: %s, token: %s' % (req.method, req.token))
header, _ = gs.json_encode({'method': method, 'token': req.token})
body, _ = gs.json_encode(arg)
ln = '%s %s\n' % (header, body)
proc.stdin.write(ln)
except Exception:
killSrv()
gs.println(gs.traceback())
except Exception:
gs.println(gs.traceback())
break
def _read_stdout(proc):
try:
while True:
ln = proc.stdout.readline()
if not ln:
break
_recv_q.put(ln)
except Exception:
gs.println(gs.traceback())
proc.stdout.close()
proc.wait()
proc = None
def killSrv():
p = gs.del_attr(PROC_ATTR_NAME)
if p:
try:
p.stdout.close()
except Exception:
pass
try:
p.stdin.close()
except Exception:
pass
def _dump(res, err):
gs.println(json.dumps({
'res': res,
'err': err,
}, sort_keys=True, indent=2))
if not gs.checked(DOMAIN, 'do_init'):
atexit.register(killSrv)
sublime.set_timeout(do_init, 0)