Пример #1
0
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
Пример #2
0
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
Пример #3
0
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:
                    if proc:
                        try:
                            proc.kill()
                            proc.stdout.close()
                        except:
                            pass

                    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 MarGo: %s' % err)
                        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:
                gs.println(gs.traceback())
        except Exception:
            gs.println(gs.traceback())
            break
Пример #4
0
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)
Пример #5
0
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)
Пример #6
0
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:
					if proc:
						try:
							proc.kill()
							proc.stdout.close()
						except:
							pass

					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 MarGo: %s' % err)
						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:
				gs.println(gs.traceback())
		except Exception:
			gs.println(gs.traceback())
			break
Пример #7
0
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
Пример #8
0
def _send():
    while True:
        try:
            try:
                method, arg, cb = _send_q.get()

                proc = gs.attr("mg9.proc")
                if not proc or proc.poll() is not None:
                    if proc:
                        try:
                            proc.kill()
                            proc.stdout.close()
                        except:
                            pass

                    maybe_install()

                    if not gs.checked(DOMAIN, "launch _recv"):
                        gsq.launch(DOMAIN, _recv)

                        # ideally the env should be setup before-hand with a bcall
                        # so we won't run this through the shell
                    proc, _, err = gsshell.proc([MARGO9_BIN], stderr=gs.LOGFILE)
                    gs.set_attr("mg9.proc", proc)

                    if not proc:
                        gs.notice(DOMAIN, "Cannot start MarGo9: %s" % err)
                        continue

                    gsq.launch(DOMAIN, lambda: _read_stdout(proc))

                token = "mg9.autoken.%s" % uuid.uuid4()
                _stash[token] = cb

                header, _ = gs.json_encode({"method": method, "token": token})
                body, _ = gs.json_encode(arg)
                ln = "%s %s\n" % (header, body)
                proc.stdin.write(ln)
            except Exception:
                gs.println(gs.traceback())
        except Exception:
            gs.println(gs.traceback())
            break
Пример #9
0
def _send():
	while True:
		try:
			try:
				method, arg, cb = _send_q.get()

				proc = gs.attr('mg9.proc')
				if not proc or proc.poll() is not None:
					if proc:
						try:
							proc.kill()
							proc.stdout.close()
						except:
							pass

					maybe_install()

					if not gs.checked(DOMAIN, 'launch _recv'):
						gsq.launch(DOMAIN, _recv)

					proc, _, err = gsshell.proc([MARGO9_BIN, '-poll=5'], stderr=gs.LOGFILE)
					gs.set_attr('mg9.proc', proc)

					if not proc:
						gs.notice(DOMAIN, 'Cannot start MarGo9: %s' % err)
						continue

					gsq.launch(DOMAIN, lambda: _read_stdout(proc))

				token = 'mg9.autoken.%s' % uuid.uuid4()
				_stash[token] = cb

				header, _ = gs.json_encode({'method': method, 'token': token})
				body, _ = gs.json_encode(arg)
				ln = '%s %s\n' % (header, body)
				proc.stdin.write(ln)
			except Exception:
				gs.println(gs.traceback())
		except Exception:
			gs.println(gs.traceback())
			break
Пример #10
0
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)
Пример #11
0
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()
Пример #12
0
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()
Пример #13
0
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)
Пример #14
0
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()