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', ''), )) keep = req.f(r.get('data', {}), r.get('error', '')) is not True if keep: req.tm = time.time() gs.set_attr(k, req) else: gs.debug(DOMAIN, 'Ignoring margo: token: %s' % token) except Exception: gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break
def bcall(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=gs.LOGFILE, 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
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', ''), )) keep = req.f(r.get('data', {}), r.get('error', '')) is not True if keep: req.tm = time.time() gs.set_attr(k, req) else: gs.debug(DOMAIN, 'Ignoring margo: token: %s' % token) except Exception: gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break
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
def _recv(): while True: try: ln = _recv_q.get() try: ln = ln.strip() if ln: r, _ = gs.json_decode(ln, {}) token = r.get('token', '') f = _stash.get(token) if f: del _stash[token] f(r.get('data', {}), r.get('error', '')) except Exception: gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break
def install(aso_tokens, force_install): 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') 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, '| ~bin: %s' % gs.home_path('bin'), '| margo0: %s (%s)' % _tp(MARGO0_BIN), '| margo9: %s (%s)' % _tp(MARGO9_BIN), '| GOROOT: %s' % e.get('GOROOT', '(not set)'), '| GOPATH: %s' % e.get('GOPATH', '(not set)'), '| GOBIN: %s (should usually be (not set))' % e.get('GOBIN', '(not set)'), ) 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)))
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(): m_out = 'no' else: gs.notify('GoSublime', 'Installing MarGo') start = time.time() m_out, err, _ = _run(['go', 'build', '-o', MARGO_BIN], cwd=MARGO_SRC) m_out, m_ok = _so(m_out, err, start, time.time()) if m_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([MARGO_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' % (MARGO_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 margo: %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 != MARGO_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() gsq.launch(DOMAIN, margo.bye_ni)
def install(aso_tokens, force_install): 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') 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, '| ~bin: %s' % gs.home_path('bin'), '| margo0: %s (%s)' % _tp(MARGO0_BIN), '| margo9: %s (%s)' % _tp(MARGO9_BIN), '| GOROOT: %s' % e.get('GOROOT', '(not set)'), '| GOPATH: %s' % e.get('GOPATH', '(not set)'), '| GOBIN: %s (should usually be (not set))' % e.get('GOBIN', '(not set)'), ) 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()
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()
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()
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(): m_out = 'no' else: gs.notify('GoSublime', 'Installing MarGo') start = time.time() m_out, err, _ = _run(['go', 'build', '-o', MARGO_BIN], cwd=MARGO_SRC) m_out, m_ok = _so(m_out, err, start, time.time()) if m_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([MARGO_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' % (MARGO_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 margo: %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 != MARGO_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() gsq.launch(DOMAIN, margo.bye_ni)
def install(aso_tokens, force_install): 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' g_out = 'no' else: 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(GOCODE_BIN): margo.bye_ni() 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 m0_ok and g_ok: def f(): gs.aso().set('mg9_install_tokens', _gen_tokens()) gs.save_aso() sublime.set_timeout(f, 0) out, err, _ = gsshell.run([MARGO9_EXE, '-env'], cwd=gs.home_path(), shell=True) 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, '| install gocode: %s' % g_out, '| ~bin: %s' % gs.home_path('bin'), '| margo0: %s (%s)' % _tp(MARGO0_BIN), '| margo9: %s (%s)' % _tp(MARGO9_BIN), '| gocode: %s (%s)' % _tp(GOCODE_BIN), '| GOROOT: %s' % e.get('GOROOT', '(not set)'), '| GOPATH: %s' % e.get('GOPATH', '(not set)'), '| GOBIN: %s (should usually be (not set))' % e.get('GOBIN', '(not set)'), ) 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))
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()