Пример #1
0
def _install(maybe=False):
	if _inst_state() != "":
		return

	start = time.time()

	gs.set_attr(_inst_name(), 'busy')
	m_out = build_mg()
	gs.set_attr(_inst_name(), 'done')

	if m_out == 'ok':
		ev.margo_ready()
		gs.notify('GoSublime', 'ready')

		if maybe:
			return

	e = sh.env()
	a = [
		'GoSublime init %s (%0.3fs)' % (INSTALL_VERSION, time.time() - start),
	]

	sl = [('install margo', m_out)]
	sl.extend(sanity_check(e))
	a.extend(sanity_check_sl(sl))
	gs.println(*a)

	_check_env(e)
	killSrv()
	_cleanup()
Пример #2
0
		def f(docs, err):
			doc = ''
			if err:
				self.show_output('// Error: %s' % err)
			elif docs:
				if mode == "goto":
					fn = ''
					flags = 0
					if len(docs) > 0:
						d = docs[0]
						fn = d.get('fn', '')
						row = d.get('row', 0)
						col = d.get('col', 0)
						if fn:
							gs.println('opening %s:%s:%s' % (fn, row, col))
							gs.focus(fn, row, col)
							return
					self.show_output("%s: cannot find definition" % DOMAIN)
				elif mode == "hint":
					s = []
					for d in docs:
						name = d.get('name', '')
						if name:
							kind = d.get('kind', '')
							pkg = d.get('pkg', '')
							if pkg:
								name = '%s.%s' % (pkg, name)
							src = d.get('src', '')
							if src:
								src = '\n//\n%s' % src
							doc = '// %s %s%s' % (name, kind, src)

						s.append(doc)
					doc = '\n\n\n'.join(s).strip()
			self.show_output(doc or "// %s: no docs found" % DOMAIN)
Пример #3
0
	def run(self, edit):
		domain = '%s: GsFmt' % DOMAIN
		vsize = self.view.size()
		src = self.view.substr(sublime.Region(0, vsize))
		if not src.strip():
			return

		src, err = mg9.fmt(self.view.file_name(), src)

		if not err and not src.strip():
			err = "it appears to be empty"

		if err:
			err = "Cannot fmt file. Error: `%s'" % err
			short_err = '%s: %s... error logged to console' % (domain, err[:15])

			def clear_status():
				if self.view.get_status(domain) == short_err:
					self.view.set_status(domain, '')

			gs.println(domain, err)
			self.view.set_status(domain, short_err)
			sublime.set_timeout(clear_status, 10000)

			if 'ipc_timeout' in err:
				self.view.show_popup("%s: <b>%s</b>" % (domain, err))

			return

		_, err = gspatch.merge(self.view, vsize, src, edit)
		if err:
			msg = 'PANIC: Cannot fmt file. Check your source for errors (and maybe undo any changes).'
			sublime.error_message("%s: %s: Merge failure: `%s'" % (domain, msg, err))
Пример #4
0
 def write_lines(self, view, lines):
     try:
         view.run_command(
             "gs_insert_content", {"content": "\n".join(lines), "pos": view.size()}
         )
     except Exception:
         gs.println(gs.traceback(DOMAIN))
     view.show(view.line(view.size() - 1).begin())
Пример #5
0
					def open(d):
						fn = d.get('fn', '')
						row = d.get('row', 0)
						col = d.get('col', 0)
						if fn:
							gs.println('opening %s:%s:%s' % (fn, row, col))
							gs.focus(fn, row, col)
							return
						self.show_output("%s: cannot find definition" % DOMAIN)
Пример #6
0
	def write_lines(self, view, lines):
		try:
			view.run_command('gs_insert_content', {
				'content': '\n'.join(lines),
				'pos': view.size(),
			})
		except Exception:
			gs.println(gs.traceback(DOMAIN))
		view.show(view.line(view.size() - 1).begin())
Пример #7
0
def _recv():
    while True:
        try:
            ln = gs.mg9_recv_q.get()
            try:
                ln = ln.strip()
                if ln:
                    r, _ = gs.json_decode(ln, {})
                    token = r.get("token", "")
                    tag = r.get("tag", "")
                    k = REQUEST_PREFIX + token
                    req = gs.attr(k, {})
                    gs.del_attr(k)
                    if req and req.f:
                        if tag != TAG:
                            gs.notice(
                                DOMAIN,
                                "\n".join(
                                    [
                                        "GoSublime/MarGo appears to be out-of-sync.",
                                        "Maybe restart Sublime Text.",
                                        "Received tag `%s', expected tag `%s'. " % (tag, TAG),
                                    ]
                                ),
                            )

                        err = r.get("error", "")

                        gs.debug(
                            DOMAIN,
                            "margo response: %s"
                            % {
                                "method": req.method,
                                "tag": tag,
                                "token": token,
                                "dur": "%0.3fs" % (time.time() - req.tm),
                                "err": err,
                                "size": "%0.1fK" % (len(ln) / 1024.0),
                            },
                        )

                        dat = expand_jdata(r.get("data", {}))
                        try:
                            keep = req.f(dat, err) is 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
Пример #8
0
        def f(res, err):
            if err:
                ui.error(DOMAIN, err)
                return

            fn = res.get('Fn')
            row = res.get('Line', -1) - 1
            col = res.get('Col', 0) - 1
            if not fn or row < 0:
                ui.note(DOMAIN, "no definition found")
                return

            gs.println('opening %s:%s:%s' % (fn, row, col))
            vu.open(fn=fn, row=row, col=col)
Пример #9
0
		def on_done(new_name):
			if new_name == current_selection:
				return

			view.window().run_command("show_panel", {"panel": "console"})

			gs.println(DOMAIN, 'Requested New Name: {0}'.format(new_name))

			offset =  '{0}:#{1}'.format(filename, region.begin())
			command = ['gorename', '-v', '-offset', offset, '-to', new_name]

			gs.println(DOMAIN, 'CMD: {0}'.format(' '.join(command)))

			# Save all Go views
			for win in sublime.windows():
				for v in win.views():
					if not self._is_go_source(v):
						continue
					v.run_command("save")

			out = err = ""
			try:
				p = gs.popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, environ=os.environ)
				out = p.communicate()[0]
				gs.println('GsGorename', out)

				if p.returncode != 0:
					raise OSError("GoRename failed: %s" % command)

			except Exception as e:
				msg = gs.tbck.format_exc()
				gs.println(DOMAIN, msg)
Пример #10
0
def _recv():
    while True:
        try:
            ln = gs.mg9_recv_q.get()
            try:
                ln = ln.strip()
                if ln:
                    r, _ = gs.json_decode(ln, {})
                    token = r.get('token', '')
                    tag = r.get('tag', '')
                    k = REQUEST_PREFIX + token
                    req = gs.attr(k, {})
                    gs.del_attr(k)
                    if req and req.f:
                        if tag != TAG:
                            gs.notice(
                                DOMAIN, "\n".join([
                                    "GoSublime/MarGo appears to be out-of-sync.",
                                    "Maybe restart Sublime Text.",
                                    "Received tag `%s', expected tag `%s'. " %
                                    (tag, TAG),
                                ]))

                        err = r.get('error', '')

                        ev.debug(
                            DOMAIN, "margo response: %s" % {
                                'method': req.method,
                                'tag': tag,
                                'token': token,
                                'dur': '%0.3fs' % (time.time() - req.tm),
                                'err': err,
                                'size': '%0.1fK' % (len(ln) / 1024.0),
                            })

                        dat = expand_jdata(r.get('data', {}))
                        try:
                            keep = req.f(dat, err) is True
                            if keep:
                                req.tm = time.time()
                                gs.set_attr(k, req)
                        except Exception:
                            gs.error_traceback(DOMAIN)
                    else:
                        ev.debug(DOMAIN, 'Ignoring margo: token: %s' % token)
            except Exception:
                gs.println(gs.traceback())
        except Exception:
            gs.println(gs.traceback())
            break
Пример #11
0
def _read_stdout(proc):
    try:
        while True:
            ln = proc.stdout.readline()
            if not ln:
                break

            gs.mg9_recv_q.put(gs.ustr(ln))
    except Exception:
        gs.println(gs.traceback())

        proc.stdout.close()
        proc.wait()
        proc = None
Пример #12
0
		def f(res, err):
			if err:
				ui.error(DOMAIN, err)
				return

			fn = res.get('Fn')
			row = res.get('Line', -1) - 1
			col = res.get('Col', 0) - 1
			if not fn or row < 0:
				ui.note(DOMAIN, "no definition found")
				return

			gs.println('opening %s:%s:%s' % (fn, row, col))
			vu.open(fn=fn, row=row, col=col)
Пример #13
0
def _read_stdout(proc):
	try:
		while True:
			ln = proc.stdout.readline()
			if not ln:
				break

			gs.mg9_recv_q.put(gs.ustr(ln))
	except Exception:
		gs.println(gs.traceback())

		proc.stdout.close()
		proc.wait()
		proc = None
Пример #14
0
def _cleanup():
	try:
		vdir, vnm = os.path.split(sh.vdir())
		for nm in os.listdir(vdir):
			fn = os.path.join(vdir, nm)
			if nm != vnm and os.path.isdir(fn):
				try:
					gs.println("GoSublime: removing old directory: `%s'" % fn)
					shutil.rmtree(fn)
				except Exception:
					pass

	except Exception:
		pass
Пример #15
0
def _send():
	while True:
		try:
			try:
				method, arg, cb = gs.mg9_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([_margo_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)

				if gs.PY3K:
					proc.stdin.write(bytes(ln, 'UTF-8'))
				else:
					proc.stdin.write(ln)
			except Exception:
				killSrv()
				gs.println(gs.traceback())
		except Exception:
			gs.println(gs.traceback())
			break
Пример #16
0
def _recv():
	while True:
		try:
			ln = gs.mg9_recv_q.get()
			try:
				ln = ln.strip()
				if ln:
					r, _ = gs.json_decode(ln, {})
					token = r.get('token', '')
					tag = r.get('tag', '')
					k = REQUEST_PREFIX+token
					req = gs.attr(k, {})
					gs.del_attr(k)
					if req and req.f:
						if tag != TAG:
							gs.notice(DOMAIN, "\n".join([
								"GoSublime/MarGo appears to be out-of-sync.",
								"Maybe restart Sublime Text.",
								"Received tag `%s', expected tag `%s'. " % (tag, TAG),
							]))

						err = r.get('error', '')

						ev.debug(DOMAIN, "margo response: %s" % {
							'method': req.method,
							'tag': tag,
							'token': token,
							'dur': '%0.3fs' % (time.time() - req.tm),
							'err': err,
							'size': '%0.1fK' % (len(ln)/1024.0),
						})

						dat = expand_jdata(r.get('data', {}))
						try:
							keep = req.f(dat, err) is True
							if keep:
								req.tm = time.time()
								gs.set_attr(k, req)
						except Exception:
							gs.error_traceback(DOMAIN)
					else:
						ev.debug(DOMAIN, 'Ignoring margo: token: %s' % token)
			except Exception:
				gs.println(gs.traceback())
		except Exception:
			gs.println(gs.traceback())
			break
Пример #17
0
def _read_stdout(proc):
    """Reads lines from proc stdout into the mg9_recv_q queue.Queue, which
    is polled by _recv().
    """
    try:
        while True:
            ln = proc.stdout.readline()
            if not ln:
                break

            gs.mg9_recv_q.put(gs.ustr(ln))
    except Exception:
        gs.println(gs.traceback())

        proc.stdout.close()
        proc.wait()
        proc = None
Пример #18
0
    def run(self):
        try:
            while True:
                line = self.stdout.readline()

                if not line:
                    self.c.close_stdout()
                    break

                if not self.c.output_started:
                    self.c.output_started = time.time()

                try:
                    self.c.on_output(self.c, gs.ustr(line.rstrip("\r\n")))
                except Exception:
                    gs.println(gs.traceback(DOMAIN))
        except Exception:
            gs.println(gs.traceback(DOMAIN))
Пример #19
0
	def run(self, edit):
		vsize = self.view.size()
		src = self.view.substr(sublime.Region(0, vsize))
		if not src.strip():
			return

		src, err = mg9.fmt(self.view.file_name(), src)
		if err:
			gs.println(DOMAIN, "cannot fmt file. error: `%s'" % err)
			return

		if not src.strip():
			return

		_, err = gspatch.merge(self.view, vsize, src, edit)
		if err:
			msg = 'PANIC: Cannot fmt file. Check your source for errors (and maybe undo any changes).'
			sublime.error_message("%s: %s: Merge failure: `%s'" % (DOMAIN, msg, err))
Пример #20
0
	def run(self):
		try:
			while True:
				line = self.stdout.readline()

				if not line:
					self.c.close_stdout()
					break

				if not self.c.output_started:
					self.c.output_started = time.time()

				try:
					self.c.on_output(self.c, gs.ustr(line.rstrip('\r\n')))
				except Exception:
					gs.println(gs.traceback(DOMAIN))
		except Exception:
			gs.println(gs.traceback(DOMAIN))
Пример #21
0
	def run(self, edit):
		vsize = self.view.size()
		src = self.view.substr(sublime.Region(0, vsize))
		if not src.strip():
			return

		src, err = mg9.fmt(self.view.file_name(), src)
		if err:
			gs.println(DOMAIN, "cannot fmt file. error: `%s'" % err)
			return

		if not src.strip():
			gs.println(DOMAIN, "cannot fmt file. it appears to be empty")
			return

		_, err = gspatch.merge(self.view, vsize, src, edit)
		if err:
			msg = 'PANIC: Cannot fmt file. Check your source for errors (and maybe undo any changes).'
			sublime.error_message("%s: %s: Merge failure: `%s'" % (DOMAIN, msg, err))
Пример #22
0
		def on_done(new_name):
			if new_name == current_selection:
				return

			gs.println(DOMAIN, 'Requested New Name: {0}'.format(new_name))

			offset =  '{0}:#{1}'.format(filename, region.begin())
			command = ['gorename', '-offset', offset, '-to', new_name]

			gs.println(DOMAIN, 'CMD: {0}'.format(' '.join(command)))

			out = ""
			try:
				p = gs.popen(command, stderr=subprocess.STDOUT)
				out = p.communicate()[0]
				if p.returncode != 0:
					raise OSError("GoRename failed")

			except Exception as e:
				msg = gs.tbck.format_exc()
				if out:
					msg = '{0}\n{1}'.format(msg, gs.ustr(out))
				gs.show_output('GsGorename', msg, replace=False, merge_domain=False)
Пример #23
0
def _recv():
	while True:
		try:
			ln = gs.mg9_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
Пример #24
0
def install(aso_install_vesion, force_install, _reinstall=False):
	global INSTALL_EXE

	if not _reinstall and _inst_state() != "":
		gs.notify(DOMAIN, 'Installation aborted. Install command already called for GoSublime %s.' % INSTALL_VERSION)
		return ''

	INSTALL_EXE = INSTALL_EXE.replace('_%s.exe' % about.DEFAULT_GO_VERSION, '_%s.exe' % sh.GO_VERSION)
	about.MARGO_EXE = INSTALL_EXE

	is_update = about.VERSION != INSTALL_VERSION

	gs.set_attr(_inst_name(), 'busy')

	init_start = time.time()

	if not _reinstall and not is_update and not force_install and _bins_exist() and aso_install_vesion == INSTALL_VERSION:
		m_out = 'no'
	else:
		gs.notify('GoSublime', 'Installing MarGo')
		start = time.time()

		cmd = sh.Command([
			'go', 'build',
			'-tags', 'gosublime' if ext_main_file() else '',
			'-v',
			'-o', INSTALL_EXE,
			'disposa.blue/cmd/margo',
		])
		cmd.wd = gs.home_dir_path('bin')
		cmd.env = {
			'CGO_ENABLED': '0',
			'GOBIN': '',
			'GOPATH': install_gopath(),
		}

		ev.debug('%s.build' % DOMAIN, {
			'cmd': cmd.cmd_lst,
			'cwd': cmd.wd,
		})

		cr = cmd.run()
		m_out = 'cmd: `%s`\nstdout: `\n%s\n`\nstderr: `\n%s\n`\nexception: `%s`' % (
			cr.cmd_lst,
			cr.out.strip(),
			cr.err.strip(),
			cr.exc,
		)

		if cr.ok and _bins_exist():
			def f():
				gs.aso().set('install_version', INSTALL_VERSION)
				gs.save_aso()

			sublime.set_timeout(f, 0)
		else:
			err_prefix = 'MarGo build failed'
			gs.error(DOMAIN, '%s\n%s' % (err_prefix, m_out))

			sl = [
				('GoSublime error', '\n'.join((
					err_prefix,
					'This is possibly a bug or miss-configuration of your environment.',
					'For more help, please file an issue with the following build output',
					'at: https://github.com/DisposaBoy/GoSublime/issues/new',
					'or alternatively, you may send an email to: [email protected]',
					'\n',
					m_out,
				)))
			]
			sl.extend(sanity_check({}, False))
			gs.show_output('GoSublime', '\n'.join(sanity_check_sl(sl)))

	gs.set_attr(_inst_name(), 'done')

	if is_update:
		gs.show_output('GoSublime-source', '\n'.join([
			'GoSublime source has been updated.',
			'New version: `%s`, current version: `%s`' % (INSTALL_VERSION, about.VERSION),
			'Please restart Sublime Text to complete the update.',
		]))
	else:
		e = sh.env()
		a = [
			'GoSublime init %s (%0.3fs)' % (INSTALL_VERSION, time.time() - init_start),
		]

		sl = [('install margo', m_out)]
		sl.extend(sanity_check(e))
		a.extend(sanity_check_sl(sl))
		gs.println(*a)

		missing = [k for k in ('GOROOT', 'GOPATH') if not e.get(k)]
		if missing:
			missing_message = '\n'.join([
				'Missing required environment variables: %s' % ' '.join(missing),
				'See the `Quirks` section of USAGE.md for info',
			])

			cb = lambda ok: gs.show_output(DOMAIN, missing_message, merge_domain=True, print_output=False)
			gs.error(DOMAIN, missing_message)
			gs.focus(gs.dist_path('USAGE.md'), focus_pat='^Quirks', cb=cb)

		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:
			bin_dirs = [
				gs.home_path('bin'),
			]

			l = []
			for d in bin_dirs:
				try:
					for fn in os.listdir(d):
						if fn != INSTALL_EXE and about.MARGO_EXE_PAT.match(fn):
							l.append(os.path.join(d, fn))
				except Exception:
					pass

			for fn in l:
				try:
					gs.println("GoSublime: removing old binary: `%s'" % fn)
					os.remove(fn)
				except Exception:
					report_x()

		except Exception:
			report_x()

	return m_out
Пример #25
0
def _dump(res, err):
    gs.println(json.dumps({
        'res': res,
        'err': err,
    }, sort_keys=True, indent=2))
Пример #26
0
def install(aso_install_vesion, force_install):
    """Install GoSublime margo.
    """

    global INSTALL_EXE

    if _inst_state() != "":
        gs.notify(
            DOMAIN,
            "Installation aborted. Install command already called for GoSublime %s."
            % INSTALL_VERSION,
        )
        return

    INSTALL_EXE = INSTALL_EXE.replace(
        "_%s.exe" % about.DEFAULT_GO_VERSION, "_%s.exe" % sh.GO_VERSION
    )
    about.MARGO_EXE = INSTALL_EXE

    is_update = about.VERSION != INSTALL_VERSION

    gs.set_attr(_inst_name(), "busy")

    init_start = time.time()

    if (
        not is_update
        and not force_install
        and _bins_exist()
        and aso_install_vesion == INSTALL_VERSION
    ):
        m_out = "no"
    else:
        gs.notify("GoSublime", "Installing MarGo")
        start = time.time()

        # WARN (CEV): Hard coded GOPATH
        # gopath = gs.dist_path() + os.pathsep + "/Users/Charlie/go"
        gopath = gs.dist_path() + os.pathsep + sh.getenv("GOPATH")

        cmd = sh.Command(
            ["go", "build", "-v", "-x", "-o", INSTALL_EXE, "gosubli.me/margo"]
        )
        cmd.wd = gs.home_dir_path("bin")
        cmd.env = {"CGO_ENABLED": "0", "GOBIN": "", "GOPATH": gopath}

        ev.debug("%s.build" % DOMAIN, {"cmd": cmd.cmd_lst, "cwd": cmd.wd})

        cr = cmd.run()
        m_out = "cmd: `%s`\nstdout: `%s`\nstderr: `%s`\nexception: `%s`" % (
            cr.cmd_lst,
            cr.out.strip(),
            cr.err.strip(),
            cr.exc,
        )

        if cr.ok and _bins_exist():

            def f():
                gs.aso().set("install_version", INSTALL_VERSION)
                gs.save_aso()

            sublime.set_timeout(f, 0)
        else:
            err_prefix = "MarGo build failed"
            gs.error(DOMAIN, "%s\n%s" % (err_prefix, m_out))

            sl = [
                (
                    "GoSublime error",
                    "\n".join(
                        (
                            err_prefix,
                            "This is possibly a bug or miss-configuration of your environment.",
                            "For more help, please file an issue with the following build output",
                            "at: https://github.com/DisposaBoy/GoSublime/issues/new",
                            "or alternatively, you may send an email to: [email protected]",
                            "\n",
                            m_out,
                        )
                    ),
                )
            ]
            sl.extend(sanity_check({}, False))
            gs.show_output("GoSublime", "\n".join(sanity_check_sl(sl)))

    gs.set_attr(_inst_name(), "done")

    if is_update:
        gs.show_output(
            "GoSublime-source",
            "\n".join(
                [
                    "GoSublime source has been updated.",
                    "New version: `%s`, current version: `%s`"
                    % (INSTALL_VERSION, about.VERSION),
                    "Please restart Sublime Text to complete the update.",
                ]
            ),
        )
    else:
        e = sh.env()
        a = ["GoSublime init %s (%0.3fs)" % (INSTALL_VERSION, time.time() - init_start)]

        sl = [("install margo", m_out)]
        sl.extend(sanity_check(e))
        a.extend(sanity_check_sl(sl))
        gs.println(*a)

        missing = [k for k in ("GOROOT", "GOPATH") if not e.get(k)]
        if missing:
            missing_message = "\n".join(
                [
                    "Missing required environment variables: %s" % " ".join(missing),
                    "See the `Quirks` section of USAGE.md for info",
                ]
            )

            cb = lambda ok: gs.show_output(
                DOMAIN, missing_message, merge_domain=True, print_output=False
            )
            gs.error(DOMAIN, missing_message)
            gs.focus(gs.dist_path("USAGE.md"), focus_pat="^Quirks", cb=cb)

        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:
            bin_dirs = [gs.home_path("bin")]

            l = []
            for d in bin_dirs:
                try:
                    for fn in os.listdir(d):
                        if fn != INSTALL_EXE and about.MARGO_EXE_PAT.match(fn):
                            l.append(os.path.join(d, fn))
                except Exception:
                    pass

            for fn in l:
                try:
                    gs.println("GoSublime: removing old binary: `%s'" % fn)
                    os.remove(fn)
                except Exception:
                    report_x()

        except Exception:
            report_x()
Пример #27
0
def _send():
    while True:
        try:
            try:
                method, arg, cb = gs.mg9_send_q.get()

                proc = gs.attr(PROC_ATTR_NAME)
                if not proc or proc.poll() is not None:
                    killSrv()

                    if _inst_state() != "busy":
                        maybe_install()

                    while _inst_state() == "busy":
                        time.sleep(0.100)

                    mg_bin = _margo_bin()
                    cmd = [
                        mg_bin,
                        '-oom',
                        gs.setting('margo_oom', 0),
                        '-poll',
                        30,
                        '-tag',
                        TAG,
                    ]

                    c = sh.Command(cmd)
                    c.stderr = gs.LOGFILE
                    c.env = {
                        'GOGC': 10,
                        'XDG_CONFIG_HOME': gs.home_path(),
                    }

                    pr = c.proc()
                    if pr.ok:
                        proc = pr.p
                        err = ''
                    else:
                        proc = None
                        err = 'Exception: %s' % pr.exc

                    if err or not proc or proc.poll() is not None:
                        killSrv()
                        _call(cb, {}, 'Abort. Cannot start MarGo: %s' % err)

                        continue

                    gs.set_attr(PROC_ATTR_NAME, proc)
                    gsq.launch(DOMAIN, lambda: _read_stdout(proc))

                req = Request(f=cb, method=method)
                gs.set_attr(REQUEST_PREFIX + req.token, req)

                header, err = gs.json_encode(req.header())
                if err:
                    _cb_err(cb, 'Failed to construct ipc header: %s' % err)
                    continue

                body, err = gs.json_encode(arg)
                if err:
                    _cb_err(cb, 'Failed to construct ipc body: %s' % err)
                    continue

                ev.debug(DOMAIN, 'margo request: %s ' % header)

                ln = '%s %s\n' % (header, body)

                try:
                    if gs.PY3K:
                        proc.stdin.write(bytes(ln, 'UTF-8'))
                    else:
                        proc.stdin.write(ln)

                except Exception as ex:
                    _cb_err(cb, 'Cannot talk to MarGo: %s' % err)
                    killSrv()
                    gs.println(gs.traceback())

            except Exception:
                killSrv()
                gs.println(gs.traceback())
        except Exception:
            gs.println(gs.traceback())
            break
Пример #28
0
def _recv():
    """Polls the mg9_recv_q queue parsing responses.
    """
    # TODO: REFACTOR.
    while True:
        try:
            ln = gs.mg9_recv_q.get()
            try:
                ln = ln.strip()
                if ln:
                    r, _ = gs.json_decode(ln, {})
                    token = r.get("token", "")
                    tag = r.get("tag", "")
                    k = REQUEST_PREFIX + token

                    # TODO: try req = gs.del_attr(k)
                    req = gs.attr(k, {})
                    gs.del_attr(k)

                    if req and req.f:
                        if tag != TAG:
                            gs.notice(
                                DOMAIN,
                                "\n".join(
                                    [
                                        "GoSublime/MarGo appears to be out-of-sync.",
                                        "Maybe restart Sublime Text.",
                                        "Received tag `%s', expected tag `%s'. "
                                        % (tag, TAG),
                                    ]
                                ),
                            )

                        err = r.get("error", "")

                        # TODO: Check if debug is enabled (len()).
                        ev.debug(
                            DOMAIN,
                            "margo response: %s"
                            % {
                                "method": req.method,
                                "tag": tag,
                                "token": token,
                                "dur": "%0.3fs" % (time.time() - req.tm),
                                "err": err,
                                "size": "%0.1fK" % (len(ln) / 1024.0),
                            },
                        )

                        # CEV: req.f is the callback 'cb' set in _send().
                        #
                        dat = expand_jdata(r.get("data", {}))
                        try:
                            # Add request back to the attr dict.
                            #
                            # TODO: Document, which calls keep the request.
                            keep = req.f(dat, err) is True
                            if keep:
                                req.tm = time.time()
                                gs.set_attr(k, req)
                        except Exception:
                            gs.error_traceback(DOMAIN)
                    else:
                        ev.debug(DOMAIN, "Ignoring margo: token: %s" % token)
            except Exception:
                gs.println(gs.traceback())
        except Exception:
            gs.println(gs.traceback())
            break
Пример #29
0
def install(aso_install_vesion, force_install, _reinstall=False):
    global INSTALL_EXE

    if not _reinstall and _inst_state() != "":
        gs.notify(
            DOMAIN,
            'Installation aborted. Install command already called for GoSublime %s.'
            % INSTALL_VERSION)
        return ''

    INSTALL_EXE = INSTALL_EXE.replace('_%s.exe' % about.DEFAULT_GO_VERSION,
                                      '_%s.exe' % sh.GO_VERSION)
    about.MARGO_EXE = INSTALL_EXE

    is_update = about.VERSION != INSTALL_VERSION

    gs.set_attr(_inst_name(), 'busy')

    init_start = time.time()

    if not _reinstall and not is_update and not force_install and _bins_exist(
    ) and aso_install_vesion == INSTALL_VERSION:
        m_out = 'no'
    else:
        gs.notify('GoSublime', 'Installing MarGo')
        start = time.time()

        cmd = sh.Command([
            'go',
            'build',
            '-tags',
            'gosublime' if ext_main_file() else '',
            '-v',
            '-o',
            INSTALL_EXE,
            'gosublime/cmd/margo',
        ])
        cmd.wd = gs.home_dir_path('bin')
        cmd.env = {
            'CGO_ENABLED': '0',
            'GOBIN': '',
            'GOPATH': install_gopath(),
        }

        ev.debug('%s.build' % DOMAIN, {
            'cmd': cmd.cmd_lst,
            'cwd': cmd.wd,
        })

        cr = cmd.run()
        m_out = 'cmd: `%s`\nstdout: `\n%s\n`\nstderr: `\n%s\n`\nexception: `%s`' % (
            cr.cmd_lst,
            cr.out.strip(),
            cr.err.strip(),
            cr.exc,
        )

        if cr.ok and _bins_exist():

            def f():
                gs.aso().set('install_version', INSTALL_VERSION)
                gs.save_aso()

            sublime.set_timeout(f, 0)
        else:
            err_prefix = 'MarGo build failed'
            gs.error(DOMAIN, '%s\n%s' % (err_prefix, m_out))

            sl = [('GoSublime error', '\n'.join((
                err_prefix,
                'This is possibly a bug or miss-configuration of your environment.',
                'For more help, please file an issue with the following build output',
                'at: https://github.com/DisposaBoy/GoSublime/issues/new',
                'or alternatively, you may send an email to: [email protected]',
                '\n',
                m_out,
            )))]
            sl.extend(sanity_check({}, False))
            gs.show_output('GoSublime', '\n'.join(sanity_check_sl(sl)))

    gs.set_attr(_inst_name(), 'done')

    if is_update:
        gs.show_output(
            'GoSublime-source', '\n'.join([
                'GoSublime source has been updated.',
                'New version: `%s`, current version: `%s`' %
                (INSTALL_VERSION, about.VERSION),
                'Please restart Sublime Text to complete the update.',
            ]))
    else:
        e = sh.env()
        a = [
            'GoSublime init %s (%0.3fs)' %
            (INSTALL_VERSION, time.time() - init_start),
        ]

        sl = [('install margo', m_out)]
        sl.extend(sanity_check(e))
        a.extend(sanity_check_sl(sl))
        gs.println(*a)

        missing = [k for k in ('GOROOT', 'GOPATH') if not e.get(k)]
        if missing:
            missing_message = '\n'.join([
                'Missing required environment variables: %s' %
                ' '.join(missing),
                'See the `Quirks` section of USAGE.md for info',
            ])

            cb = lambda ok: gs.show_output(
                DOMAIN, missing_message, merge_domain=True, print_output=False)
            gs.error(DOMAIN, missing_message)
            gs.focus(gs.dist_path('USAGE.md'), focus_pat='^Quirks', cb=cb)

        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:
            bin_dirs = [
                gs.home_path('bin'),
            ]

            l = []
            for d in bin_dirs:
                try:
                    for fn in os.listdir(d):
                        if fn != INSTALL_EXE and about.MARGO_EXE_PAT.match(fn):
                            l.append(os.path.join(d, fn))
                except Exception:
                    pass

            for fn in l:
                try:
                    gs.println("GoSublime: removing old binary: `%s'" % fn)
                    os.remove(fn)
                except Exception:
                    report_x()

        except Exception:
            report_x()

    return m_out
Пример #30
0
def install(aso_install_vesion, force_install):
	global INSTALL_EXE

	if gs.attr(_inst_name(), '') != "":
		gs.notify(DOMAIN, 'Installation aborted. Install command already called for GoSublime %s.' % INSTALL_VERSION)
		return

	_init_go_version()
	INSTALL_EXE = INSTALL_EXE.replace('_%s.exe' % about.DEFAULT_GO_VERSION, '_%s.exe' % GO_VERSION)
	about.MARGO_EXE = INSTALL_EXE

	is_update = about.VERSION != INSTALL_VERSION

	gs.set_attr(_inst_name(), 'busy')

	init_start = time.time()

	if not is_update and not force_install and _bins_exist() and aso_install_vesion == INSTALL_VERSION:
		m_out = 'no'
	else:
		gs.notify('GoSublime', 'Installing MarGo')
		start = time.time()

		go_bin = _go_bin()
		if go_bin:
			cmd = [go_bin, 'build', '-o', _margo_bin(INSTALL_EXE)]
			cwd = _margo_src()
			ev.debug('%s.build' % DOMAIN, {
				'cmd': cmd,
				'cwd': cwd,
			})
			m_out, err, _ = _run(cmd, cwd=cwd)
		else:
			m_out = ''
			err = 'Cannot find the `go` exe'

		m_out = gs.ustr(m_out)
		err = gs.ustr(err)
		m_out, m_ok = _so(m_out, err, start, time.time())

		if m_ok:
			def f():
				gs.aso().set('install_version', INSTALL_VERSION)
				gs.save_aso()

			sublime.set_timeout(f, 0)

	if not is_update:
		gs.notify('GoSublime', 'Syncing environment variables')
		out, err, _ = gsshell.run([INSTALL_EXE, '-env'], cwd=gs.home_dir_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' % (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)

	gs.set_attr(_inst_name(), 'done')

	if is_update:
		gs.show_output('GoSublime-source', '\n'.join([
			'GoSublime source has been updated.',
			'New version: `%s`, current version: `%s`' % (INSTALL_VERSION, about.VERSION),
			'Please restart Sublime Text to complete the update.',
		]))
	else:
		e = gs.env()
		a = [
			'GoSublime init %s (%0.3fs)' % (INSTALL_VERSION, time.time() - init_start),
		]
		sl = [('install margo', m_out)]
		sl.extend(sanity_check(e))
		a.extend(sanity_check_sl(sl))
		gs.println(*a)

		missing = [k for k in ('GOROOT', 'GOPATH') if not e.get(k)]
		if missing:
			missing_message = '\n'.join([
				'Missing required environment variables: %s' % ' '.join(missing),
				'See the `Quirks` section of USAGE.md for info',
			])

			cb = lambda ok: gs.show_output(DOMAIN, missing_message, merge_domain=True, print_output=False)
			gs.error(DOMAIN, missing_message)
			gs.focus(gs.dist_path('USAGE.md'), focus_pat='^Quirks', cb=cb)

		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:
			bin_dirs = [
				gs.home_path('bin'),
				os.path.join(sublime.packages_path(), 'User', 'GoSublime', '9', 'bin'),
			]

			l = []
			for d in bin_dirs:
				try:
					for fn in os.listdir(d):
						if fn != INSTALL_EXE and about.MARGO_EXE_PAT.match(fn):
							l.append(os.path.join(d, fn))
				except Exception:
					pass

			for fn in l:
				try:
					gs.println("GoSublime: removing old binary: `%s'" % fn)
					os.remove(fn)
				except Exception:
					report_x()

		except Exception:
			report_x()
Пример #31
0
def install(aso_install_vesion, force_install):
	if gs.attr(_inst_name(), '') != "":
		gs.notify(DOMAIN, 'Installation aborted. Install command already called for GoSublime %s.' % INSTALL_VERSION)
		return

	is_update = about.VERSION != INSTALL_VERSION

	gs.set_attr(_inst_name(), 'busy')

	init_start = time.time()

	try:
		os.makedirs(gs.home_path('bin'))
	except:
		pass

	if not is_update and not force_install and _bins_exist() and aso_install_vesion == INSTALL_VERSION:
		m_out = 'no'
	else:
		gs.notify('GoSublime', 'Installing MarGo')
		start = time.time()

		vars = ['%PATH%', '$PATH']
		out, err, _ = gsshell.run('echo %s' % os.pathsep.join(vars), shell=True, stderr=subprocess.PIPE, env=gs.env())
		if not err:
			pl = []
			for p in out.strip().split(os.pathsep):
				p = os.path.normcase(p)
				if p not in vars and p not in pl:
					pl.append(p)

			if pl:
				gs.environ9.update({'PATH': os.pathsep.join(pl)})

		go_exe = gs.which('go')
		if go_exe:
			cmd = [go_exe, 'build', '-o', _margo_bin(INSTALL_EXE)]
			cwd = _margo_src()
			ev.debug('%s.build' % DOMAIN, {
				'cmd': cmd,
				'cwd': cwd,
			})
			m_out, err, _ = _run(cmd, cwd=cwd)
		else:
			m_out = ''
			err = 'Cannot find the `go` exe'

		m_out = gs.ustr(m_out)
		err = gs.ustr(err)
		m_out, m_ok = _so(m_out, err, start, time.time())

		if m_ok:
			def f():
				gs.aso().set('install_version', INSTALL_VERSION)
				gs.save_aso()

			sublime.set_timeout(f, 0)

	if not is_update:
		gs.notify('GoSublime', 'Syncing environment variables')
		out, err, _ = gsshell.run([about.MARGO_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' % (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)

	gs.set_attr(_inst_name(), 'done')

	if is_update:
		gs.show_output('GoSublime-source', '\n'.join([
			'GoSublime source has been updated.',
			'New version: `%s`, current version: `%s`' % (INSTALL_VERSION, about.VERSION),
			'Please restart Sublime Text to complete the update.',
		]))
	else:
		e = gs.env()
		a = [
			'GoSublime init %s (%0.3fs)' % (INSTALL_VERSION, time.time() - init_start),
		]
		sl = [('install margo', m_out)]
		sl.extend(sanity_check(e))
		a.extend(sanity_check_sl(sl))
		gs.println(*a)

		missing = [k for k in ('GOROOT', 'GOPATH') if not e.get(k)]
		if missing:
			missing_message = '\n'.join([
				'Missing required environment variables: %s' % ' '.join(missing),
				'See the `Quirks` section of USAGE.md for info',
			])

			cb = lambda ok: gs.show_output(DOMAIN, missing_message, merge_domain=True, print_output=False)
			gs.error(DOMAIN, missing_message)
			gs.focus(gs.dist_path('USAGE.md'), focus_pat='^Quirks', cb=cb)

		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')
			old_pat = re.compile(r'^gosublime.r\d{2}.\d{2}.\d{2}-\d+.margo.exe$')
			for fn in os.listdir(d):
				try:
					if fn != about.MARGO_EXE and (about.MARGO_EXE_PAT.match(fn) or old_pat.match(fn)):
						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()
Пример #32
0
def install(aso_install_vesion, force_install):
    global INSTALL_EXE

    if gs.attr(_inst_name(), '') != "":
        gs.notify(
            DOMAIN,
            'Installation aborted. Install command already called for GoSublime %s.'
            % INSTALL_VERSION)
        return

    _init_go_version()
    INSTALL_EXE = INSTALL_EXE.replace('_%s.exe' % about.DEFAULT_GO_VERSION,
                                      '_%s.exe' % GO_VERSION)
    about.MARGO_EXE = INSTALL_EXE

    is_update = about.VERSION != INSTALL_VERSION

    gs.set_attr(_inst_name(), 'busy')

    init_start = time.time()

    if not is_update and not force_install and _bins_exist(
    ) and aso_install_vesion == INSTALL_VERSION:
        m_out = 'no'
    else:
        gs.notify('GoSublime', 'Installing MarGo')
        start = time.time()

        go_bin = _go_bin()
        if go_bin:
            cmd = [go_bin, 'build', '-o', _margo_bin(INSTALL_EXE)]
            cwd = _margo_src()
            ev.debug('%s.build' % DOMAIN, {
                'cmd': cmd,
                'cwd': cwd,
            })
            m_out, err, _ = _run(cmd, cwd=cwd)
        else:
            m_out = ''
            err = 'Cannot find the `go` exe'

        m_out = gs.ustr(m_out)
        err = gs.ustr(err)
        m_out, m_ok = _so(m_out, err, start, time.time())

        if m_ok:

            def f():
                gs.aso().set('install_version', INSTALL_VERSION)
                gs.save_aso()

            sublime.set_timeout(f, 0)

    if not is_update:
        gs.notify('GoSublime', 'Syncing environment variables')
        out, err, _ = gsshell.run([INSTALL_EXE, '-env'],
                                  cwd=gs.home_dir_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' % (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)

    gs.set_attr(_inst_name(), 'done')

    if is_update:
        gs.show_output(
            'GoSublime-source', '\n'.join([
                'GoSublime source has been updated.',
                'New version: `%s`, current version: `%s`' %
                (INSTALL_VERSION, about.VERSION),
                'Please restart Sublime Text to complete the update.',
            ]))
    else:
        e = gs.env()
        a = [
            'GoSublime init %s (%0.3fs)' %
            (INSTALL_VERSION, time.time() - init_start),
        ]
        sl = [('install margo', m_out)]
        sl.extend(sanity_check(e))
        a.extend(sanity_check_sl(sl))
        gs.println(*a)

        missing = [k for k in ('GOROOT', 'GOPATH') if not e.get(k)]
        if missing:
            missing_message = '\n'.join([
                'Missing required environment variables: %s' %
                ' '.join(missing),
                'See the `Quirks` section of USAGE.md for info',
            ])

            cb = lambda ok: gs.show_output(
                DOMAIN, missing_message, merge_domain=True, print_output=False)
            gs.error(DOMAIN, missing_message)
            gs.focus(gs.dist_path('USAGE.md'), focus_pat='^Quirks', cb=cb)

        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:
            bin_dirs = [
                gs.home_path('bin'),
                os.path.join(sublime.packages_path(), 'User', 'GoSublime', '9',
                             'bin'),
            ]

            l = []
            for d in bin_dirs:
                try:
                    for fn in os.listdir(d):
                        if fn != INSTALL_EXE and about.MARGO_EXE_PAT.match(fn):
                            l.append(os.path.join(d, fn))
                except Exception:
                    pass

            for fn in l:
                try:
                    gs.println("GoSublime: removing old binary: `%s'" % fn)
                    os.remove(fn)
                except Exception:
                    report_x()

        except Exception:
            report_x()
Пример #33
0
def _send():
    """Polls the mg9_send_q queue, sending requests to margo.  If the margo
    process is not running _send() starts it and sets the PROC_ATTR_NAME attr.
    """
    # TODO: REFACTOR.
    while True:
        try:
            try:
                method, arg, cb = gs.mg9_send_q.get()

                # CEV: proc should be the margo process.
                proc = gs.attr(PROC_ATTR_NAME)

                # CEV: Looks like this starts/restarts the margo process.
                if not proc or proc.poll() is not None:
                    killSrv()

                    if _inst_state() != "busy":
                        maybe_install()

                    # TODO: Improve the handling of install state.
                    while _inst_state() == "busy":
                        time.sleep(0.100)

                    # Margo path and command line options.
                    mg_bin = _margo_bin()
                    cmd = [
                        mg_bin,
                        "-oom",
                        gs.setting("margo_oom", 0),
                        "-poll",
                        30,
                        "-tag",
                        TAG,
                    ]

                    c = sh.Command(cmd)
                    c.stderr = gs.LOGFILE
                    c.env = {"GOGC": 10, "XDG_CONFIG_HOME": gs.home_path()}

                    pr = c.proc()
                    if pr.ok:
                        proc = pr.p
                        err = ""
                    else:
                        proc = None
                        err = "Exception: %s" % pr.exc

                    if err or not proc or proc.poll() is not None:
                        killSrv()
                        _call(cb, {}, "Abort. Cannot start MarGo: %s" % err)

                        continue

                    # Set the process name
                    gs.set_attr(PROC_ATTR_NAME, proc)
                    # Launch stdout feed.
                    gsq.launch(DOMAIN, lambda: _read_stdout(proc))

                req = Request(f=cb, method=method)
                gs.set_attr(REQUEST_PREFIX + req.token, req)

                header, err = gs.json_encode(req.header())
                if err:
                    _cb_err(cb, "Failed to construct ipc header: %s" % err)
                    continue

                body, err = gs.json_encode(arg)
                if err:
                    _cb_err(cb, "Failed to construct ipc body: %s" % err)
                    continue

                ev.debug(DOMAIN, "margo request: %s " % header)

                ln = "%s %s\n" % (header, body)

                try:
                    if gs.PY3K:
                        proc.stdin.write(bytes(ln, "UTF-8"))
                    else:
                        proc.stdin.write(ln)

                except Exception as ex:
                    _cb_err(cb, "Cannot talk to MarGo: %s" % err)
                    killSrv()
                    gs.println(gs.traceback())

            except Exception:
                killSrv()
                gs.println(gs.traceback())
        except Exception:
            gs.println(gs.traceback())
            break
Пример #34
0
def _dump(res, err):
    gs.println(json.dumps({"res": res, "err": err}, sort_keys=True, indent=2))
Пример #35
0
def install(aso_tokens, force_install):
	k = 'mg9.install.%s' % about.VERSION
	if gs.attr(k, False):
		gs.error(DOMAIN, 'Installation aborted. Install command already called for GoSublime %s.' % about.VERSION)
		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([about.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' % (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 != about.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()
Пример #36
0
def install(aso_tokens, force_install):
	if gs.attr(INSTALL_ATTR_NAME, '') != "":
		gs.notify(DOMAIN, 'Installation aborted. Install command already called for GoSublime %s.' % about.VERSION)
		return

	gs.set_attr(INSTALL_ATTR_NAME, 'busy')

	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 = gs.ustr(m_out)
		err = gs.ustr(err)
		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([about.MARGO_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' % (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)

	gs.set_attr(INSTALL_ATTR_NAME, 'done')

	e = gs.env()
	a = [
		'GoSublime init (%0.3fs)' % (time.time() - init_start),
	]
	sl = [('install margo', m_out)]
	sl.extend(sanity_check(e))
	a.extend(sanity_check_sl(sl))
	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')
		old_pat = re.compile(r'^gosublime.r\d{2}.\d{2}.\d{2}-\d+.margo.exe$')
		for fn in os.listdir(d):
			try:
				if fn != about.MARGO_EXE and (about.MARGO_EXE_PAT.match(fn) or old_pat.match(fn)):
					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()
Пример #37
0
def _send():
	while True:
		try:
			try:
				method, arg, cb = gs.mg9_send_q.get()

				proc = gs.attr(PROC_ATTR_NAME)
				if not proc or proc.poll() is not None:
					killSrv()

					if gs.attr(_inst_name(), '') != "busy":
						maybe_install()

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

					while gs.attr(_inst_name(), '') == "busy":
						time.sleep(0.100)

					mg_bin = _margo_bin()
					cmd = [
						mg_bin,
						'-oom', gs.setting('margo_oom', 0),
						'-poll', 30,
						'-tag', TAG,
					]

					if os.path.exists(mg_bin):
						proc, _, err = gsshell.proc(cmd, stderr=gs.LOGFILE ,env={
							'GOGC': 10,
							'XDG_CONFIG_HOME': gs.home_path(),
						})
					else:
						proc = None
						err = "Can't find the MarGo binary at `%s`" % mg_bin

					if err or not proc or proc.poll() is not None:
						killSrv()

						gs.notice(DOMAIN, 'Cannot start MarGo:\n%s' % err)
						try:
							cb({}, 'Abort. Cannot start MarGo')
						except:
							pass

						continue

					gs.set_attr(PROC_ATTR_NAME, proc)
					gsq.launch(DOMAIN, lambda: _read_stdout(proc))

				req = Request(f=cb, method=method)
				gs.set_attr(REQUEST_PREFIX+req.token, req)

				header, err = gs.json_encode(req.header())
				if err:
					_cb_err(cb, 'Failed to construct ipc header: %s' % err)
					continue

				body, err = gs.json_encode(arg)
				if err:
					_cb_err(cb, 'Failed to construct ipc body: %s' % err)
					continue

				ev.debug(DOMAIN, 'margo request: %s ' % header)

				ln = '%s %s\n' % (header, body)

				try:
					if gs.PY3K:
						proc.stdin.write(bytes(ln, 'UTF-8'))
					else:
						proc.stdin.write(ln)

				except Exception as ex:
					_cb_err(cb, 'Cannot talk to MarGo: %s' % err)
					killSrv()
					gs.println(gs.traceback())

			except Exception:
				killSrv()
				gs.println(gs.traceback())
		except Exception:
			gs.println(gs.traceback())
			break
Пример #38
0
def _send():
	while True:
		try:
			try:
				method, arg, cb = gs.mg9_send_q.get()

				proc = gs.attr(PROC_ATTR_NAME)
				if not proc or proc.poll() is not None:
					killSrv()

					if _inst_state() != "busy":
						maybe_install()

					while _inst_state() == "busy":
						time.sleep(0.100)

					mg_bin = _margo_bin()
					cmd = [
						mg_bin,
						'-oom', gs.setting('margo_oom', 0),
						'-poll', 30,
						'-tag', TAG,
					]

					c = sh.Command(cmd)
					c.stderr = gs.LOGFILE
					c.env = {
						'GOGC': 10,
						'XDG_CONFIG_HOME': gs.home_path(),
					}

					pr = c.proc()
					if pr.ok:
						proc = pr.p
						err = ''
					else:
						proc = None
						err = 'Exception: %s' % pr.exc

					if err or not proc or proc.poll() is not None:
						killSrv()
						_call(cb, {}, 'Abort. Cannot start MarGo: %s' % err)

						continue

					gs.set_attr(PROC_ATTR_NAME, proc)
					gsq.launch(DOMAIN, lambda: _read_stdout(proc))

				req = Request(f=cb, method=method)
				gs.set_attr(REQUEST_PREFIX+req.token, req)

				header, err = gs.json_encode(req.header())
				if err:
					_cb_err(cb, 'Failed to construct ipc header: %s' % err)
					continue

				body, err = gs.json_encode(arg)
				if err:
					_cb_err(cb, 'Failed to construct ipc body: %s' % err)
					continue

				ev.debug(DOMAIN, 'margo request: %s ' % header)

				ln = '%s %s\n' % (header, body)

				try:
					if gs.PY3K:
						proc.stdin.write(bytes(ln, 'UTF-8'))
					else:
						proc.stdin.write(ln)

				except Exception as ex:
					_cb_err(cb, 'Cannot talk to MarGo: %s' % err)
					killSrv()
					gs.println(gs.traceback())

			except Exception:
				killSrv()
				gs.println(gs.traceback())
		except Exception:
			gs.println(gs.traceback())
			break
Пример #39
0
def _send():
	while True:
		try:
			try:
				method, arg, cb = gs.mg9_send_q.get()

				proc = gs.attr(PROC_ATTR_NAME)
				if not proc or proc.poll() is not None:
					killSrv()

					if gs.attr(INSTALL_ATTR_NAME) != "busy":
						maybe_install()

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

					while gs.attr(INSTALL_ATTR_NAME) == "busy":
						time.sleep(0.100)

					proc, _, err = gsshell.proc([_margo_bin(), '-poll', 30, '-tag', TAG], 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)

				header, err = gs.json_encode(req.header())
				if err:
					_cb_err('Failed to construct ipc header: ' % err)
					continue

				body, err = gs.json_encode(arg)
				if err:
					_cb_err(cb, 'Failed to construct ipc body: ' % err)
					continue

				gs.debug(DOMAIN, 'margo request: %s ' % header)

				ln = '%s %s\n' % (header, body)

				if gs.PY3K:
					proc.stdin.write(bytes(ln, 'UTF-8'))
				else:
					proc.stdin.write(ln)
			except Exception:
				killSrv()
				gs.println(gs.traceback())
		except Exception:
			gs.println(gs.traceback())
			break
Пример #40
0
def _dump(res, err):
	gs.println(json.dumps({
		'res': res,
		'err': err,
	}, sort_keys=True, indent=2))
Пример #41
0
def _send():
    while True:
        try:
            try:
                method, arg, cb = gs.mg9_send_q.get()

                proc = gs.attr(PROC_ATTR_NAME)
                if not proc or proc.poll() is not None:
                    killSrv()

                    if gs.attr(INSTALL_ATTR_NAME) != "busy":
                        maybe_install()

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

                    while gs.attr(INSTALL_ATTR_NAME) == "busy":
                        time.sleep(0.100)

                    proc, _, err = gsshell.proc(
                        [_margo_bin(), '-poll', 30, '-tag', TAG],
                        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)

                header, err = gs.json_encode(req.header())
                if err:
                    _cb_err('Failed to construct ipc header: ' % err)
                    continue

                body, err = gs.json_encode(arg)
                if err:
                    _cb_err(cb, 'Failed to construct ipc body: ' % err)
                    continue

                gs.debug(DOMAIN, 'margo request: %s ' % header)

                ln = '%s %s\n' % (header, body)

                if gs.PY3K:
                    proc.stdin.write(bytes(ln, 'UTF-8'))
                else:
                    proc.stdin.write(ln)
            except Exception:
                killSrv()
                gs.println(gs.traceback())
        except Exception:
            gs.println(gs.traceback())
            break