def on_install_done(c): s = output_str(c) x = c.exception() if x: tpl = 'Error while installing dependencies\nCommand: %s\nException: %s\nOutput: %s' gs.show_output(DOMAIN, tpl % (c.cmd, x, s), merge_domain=True) js, _, _ = gsshell.run(cmd=BUNDLE_GOSUBLIME9, shell=True) js = json.loads(js) for k,v in js.iteritems(): if v: gs.environ9[k] = v print_install_log(c, s) c = gsshell.Command(cmd=[ BUNDLE_MARGO, "-d", "-call", "replace", "-addr", gs.setting('margo_addr', '') ]) c.on_done = on_margo_done c.start() gsshell.run(cmd=[BUNDLE_GOCODE, 'close'])
def complete(self, fn, offset, src, func_name_only): global last_gopath gopath = gs.env().get('GOPATH') if gopath and gopath != last_gopath: out, _, _ = gsshell.run(cmd=['go', 'env', 'GOOS', 'GOARCH']) vars = out.strip().split() if len(vars) == 2: last_gopath = gopath libpath = os.path.join(gopath, 'pkg', '_'.join(vars)) gsshell.run(cmd=['gocode', 'set', 'lib-path', libpath], cwd=gsbundle.BUNDLE_GOBIN) comps = [] cmd = gs.setting('gocode_cmd', 'gocode') offset = 'c%s' % offset args = [cmd, "-f=json", "autocomplete", fn, offset] js, err, _ = gsshell.run(cmd=args, input=src) if err: gs.notice(DOMAIN, err) else: try: js = json.loads(js) if js and js[1]: for ent in js[1]: tn = ent['type'] cn = ent['class'] nm = ent['name'] sfx = self.typeclass_prefix(cn, tn) if cn == 'func': if nm in ('main', 'init'): continue act = gs.setting('autocomplete_tests', False) if not act and nm.startswith( ('Test', 'Benchmark', 'Example')): continue params, ret = declex(tn) ret = ret.strip('() ') if func_name_only: a = nm else: a = [] for i, p in enumerate(params): n, t = p if t.startswith('...'): n = '...' a.append('${%d:%s}' % (i + 1, n)) a = '%s(%s)' % (nm, ', '.join(a)) comps.append(('%s\t%s %s' % (nm, ret, sfx), a)) elif cn != 'PANIC': comps.append(('%s\t%s %s' % (nm, tn, sfx), nm)) except KeyError as e: gs.notice( DOMAIN, 'Error while running gocode, possibly malformed data returned: %s' % e) except ValueError as e: gs.notice(DOMAIN, "Error while decoding gocode output: %s" % e) return comps
def complete(self, fn, offset, src, func_name_only): global last_gopath gopath = gs.env().get('GOPATH') if gopath and gopath != last_gopath: out, _, _ = gsshell.run(cmd=['go', 'env', 'GOOS', 'GOARCH']) vars = out.strip().split() if len(vars) == 2: last_gopath = gopath libpath = os.path.join(gopath, 'pkg', '_'.join(vars)) gsshell.run(cmd=['gocode', 'set', 'lib-path', libpath], cwd=gsbundle.BUNDLE_GOBIN) comps = [] cmd = gs.setting('gocode_cmd', 'gocode') offset = 'c%s' % offset args = [cmd, "-f=json", "autocomplete", fn, offset] js, err, _ = gsshell.run(cmd=args, input=src) if err: gs.notice(DOMAIN, err) else: try: js = json.loads(js) if js and js[1]: for ent in js[1]: tn = ent['type'] cn = ent['class'] nm = ent['name'] sfx = self.typeclass_prefix(cn, tn) if cn == 'func': if nm in ('main', 'init'): continue act = gs.setting('autocomplete_tests', False) if not act and nm.startswith(('Test', 'Benchmark', 'Example')): continue params, ret = declex(tn) ret = ret.strip('() ') if func_name_only: a = nm else: a = [] for i, p in enumerate(params): n, t = p if t.startswith('...'): n = '...' a.append('${%d:%s}' % (i+1, n)) a = '%s(%s)' % (nm, ', '.join(a)) comps.append(('%s\t%s %s' % (nm, ret, sfx), a)) elif cn != 'PANIC': comps.append(('%s\t%s %s' % (nm, tn, sfx), nm)) except KeyError as e: gs.notice(DOMAIN, 'Error while running gocode, possibly malformed data returned: %s' % e) except ValueError as e: gs.notice(DOMAIN, "Error while decoding gocode output: %s" % e) return comps
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 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 do_comp_lint(dirname, fn): fr = ref(fn, False) reports = {} if not fr: return fn = gs.apath(fn, dirname) bindir, _ = gs.temp_dir('bin') local_env = { 'GOBIN': bindir, } pat = r'%s:(\d+)(?:[:](\d+))?\W+(.+)\s*' % re.escape(os.path.basename(fn)) pat = re.compile(pat, re.IGNORECASE) for c in gs.setting('comp_lint_commands'): try: cmd = c.get('cmd') if not cmd: continue cmd_domain = ' '.join(cmd) shell = c.get('shell') is True env = {} if c.get('global') is True else local_env out, err, _ = gsshell.run(cmd=cmd, shell=shell, cwd=dirname, env=env) if err: gs.notice(DOMAIN, err) out = out.replace('\r', '').replace('\n ', '\\n ').replace('\n\t', '\\n\t') for m in pat.findall(out): try: row, col, msg = m row = int(row) - 1 col = int(col) - 1 if col else 0 msg = msg.replace('\\n', '\n').strip() if row >= 0 and msg: msg = '%s: %s' % (cmd_domain, msg) if reports.get(row): reports[row].msg = '%s\n%s' % (reports[row].msg, msg) reports[row].col = max(reports[row].col, col) else: reports[row] = Report(row, col, msg) except: pass except: gs.notice(DOMAIN, gs.traceback()) def cb(): fr.reports = reports fr.state = 1 highlight(fr) sublime.set_timeout(cb, 0)
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 do_comp_lint(dirname, fn): fr = ref(fn, False) reports = {} if not fr: return fn = gs.apath(fn, dirname) bindir, _ = gs.temp_dir('bin') local_env = { 'GOBIN': bindir, } pat = r'%s:(\d+)(?:[:](\d+))?\W+(.+)\s*' % re.escape(os.path.basename(fn)) pat = re.compile(pat, re.IGNORECASE) for c in gs.setting('comp_lint_commands'): try: cmd = c.get('cmd') if not cmd: continue cmd_domain = ' '.join(cmd) shell = c.get('shell') is True env = {} if c.get('global') is True else local_env out, err, _ = gsshell.run(cmd=cmd, shell=shell, cwd=dirname, env=env) if err: gs.notice(DOMAIN, err) out = out.replace('\r', '').replace('\n ', '\\n ').replace('\n\t', '\\n\t') for m in pat.findall(out): try: row, col, msg = m row = int(row)-1 col = int(col)-1 if col else 0 msg = msg.replace('\\n', '\n').strip() if row >= 0 and msg: msg = '%s: %s' % (cmd_domain, msg) if reports.get(row): reports[row].msg = '%s\n%s' % (reports[row].msg, msg) reports[row].col = max(reports[row].col, col) else: reports[row] = Report(row, col, msg) except: pass except: gs.notice(DOMAIN, gs.traceback()) def cb(): fr.reports = reports fr.state = 1 highlight(fr) sublime.set_timeout(cb, 0)
def on_install_done(c): s = output_str(c) x = c.exception() if x: tpl = 'Error while installing dependencies\nCommand: %s\nException: %s\nOutput: %s' gs.show_output(DOMAIN, tpl % (c.cmd, x, s), merge_domain=True) js, _, _ = gsshell.run(cmd=BUNDLE_GOSUBLIME9, shell=True) js = json.loads(js) for k, v in js.iteritems(): if v: gs.environ9[k] = v print_install_log(c, s) c = gsshell.Command(cmd=[ BUNDLE_MARGO, "-d", "-call", "replace", "-addr", gs.setting('margo_addr', '') ]) c.on_done = on_margo_done c.start() gsshell.run(cmd=[BUNDLE_GOCODE, 'close'])
def do_hello(): global hello_sarting if hello_sarting: return hello_sarting = True margo_cmd = [ mg9.MARGO0_BIN, "-d", "-call", "replace", "-addr", gs.setting('margo_addr', '') ] tid = gs.begin(DOMAIN, 'Starting MarGo', False) out, err, _ = gsshell.run(margo_cmd) gs.end(tid) out = out.strip() err = err.strip() if err: gs.notice(DOMAIN, err) elif out: gs.println(DOMAIN, 'MarGo started %s' % out) hello_sarting = False
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 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 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 _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 call_cmd(cmd): _, _, exc = gsshell.run(cmd) return not exc
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 _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 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): 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(): 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): 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): 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(): 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()