Example #1
0
def run(cmd=[], shell=False, env={}, cwd=None, input=None, stderr=subprocess.STDOUT):
	out = ""
	err = ""
	exc = None

	try:
		p, opts, err = proc(cmd, input=input, shell=shell, stderr=stderr, env=env, cwd=cwd)
		if p:
			out, _ = p.communicate(input=opts.get('input'))
			out = gs.ustr(out) if out else ''
	except Exception as ex:
		err = 'Error communicating with command %s: %s' % (opts.get('cmd'), gs.traceback())
		exc = ex

	return (out, err, exc)
Example #2
0
def run(cmd=[], shell=False, env={}, cwd=None, input=None, stderr=subprocess.STDOUT):
	out = u""
	err = u""
	exc = None

	try:
		p, opts, err = proc(cmd, input=input, shell=shell, stderr=stderr, env=env, cwd=cwd)
		if p:
			out, _ = p.communicate(input=opts.get('input'))
			out = gs.ustr(out) if out else u''
	except Exception as ex:
		err = u'Error communicating with command %s: %s' % (opts.get('cmd'), gs.traceback())
		exc = ex

	return (out, err, exc)
Example #3
0
def push_output(view, rkey, out, hourglass_repl=''):
	out = '\t%s' % gs.ustr(out).strip().replace('\r', '').replace('\n', '\n\t')
	edit = view.begin_edit()
	try:
		regions = view.get_regions(rkey)
		if regions:
			line = view.line(regions[0].begin())
			lsrc = view.substr(line).replace(HOURGLASS, (hourglass_repl or '| done'))
			view.replace(edit, line, lsrc)
			if out.strip():
				line = view.line(regions[0].begin())
				view.insert(edit, line.end(), '\n%s' % out)
		else:
			view.insert(edit, view.size(), '\n%s' % out)
	finally:
		view.end_edit(edit)
Example #4
0
def push_output(view, rkey, out, hourglass_repl=''):
    out = '\t%s' % gs.ustr(out).strip().replace('\r', '').replace('\n', '\n\t')
    edit = view.begin_edit()
    try:
        regions = view.get_regions(rkey)
        if regions:
            line = view.line(regions[0].begin())
            lsrc = view.substr(line).replace(HOURGLASS,
                                             (hourglass_repl or '| done'))
            view.replace(edit, line, lsrc)
            if out.strip():
                line = view.line(regions[0].begin())
                view.insert(edit, line.end(), '\n%s' % out)
        else:
            view.insert(edit, view.size(), '\n%s' % out)
    finally:
        view.end_edit(edit)
Example #5
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))
Example #6
0
def run(cmd=[], shell=False, env={}, cwd=None, input=None):
	out = u""
	err = u""
	exc = None

	try:
		env = fix_env(env)
		shell, cmd = fix_shell_cmd(shell, cmd)
		p = gs.popen(cmd, shell=shell, stderr=subprocess.STDOUT, environ=env, cwd=cwd)
		if input is not None:
			input = gs.astr(input)
		out, _ = p.communicate(input=input)
		out = gs.ustr(out) if out else u''
	except Exception as ex:
		err = u'Error while running %s: %s' % (cmd, gs.traceback())
		exc = ex

	return (out, err, exc)
Example #7
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))
def run(cmd=[], shell=False, env={}, cwd=None, input=None):
	out = u""
	err = u""
	exc = None

	try:
		env = fix_env(env)
		shell, cmd = fix_shell_cmd(shell, cmd)
		p = gs.popen(cmd, shell=shell, stderr=subprocess.STDOUT, environ=env, cwd=cwd)
		if input is not None:
			input = gs.astr(input)
		out, _ = p.communicate(input=input)
		out = gs.ustr(out) if out else u''
	except Exception as ex:
		err = u'Error while running %s: %s' % (cmd, gs.traceback())
		exc = ex

	return (out, err, exc)
Example #9
0
 def on_done(c):
     out = gs.ustr('\n'.join(c.consume_outq()))
     sublime.set_timeout(lambda: push_output(view, rkey, out),
                         0)
Example #10
0
				def on_done(c):
					out = gs.ustr('\n'.join(c.consume_outq()))
					sublime.set_timeout(lambda: push_output(view, rkey, out), 0)
Example #11
0
		def cb(s):
			file_name = self.view.file_name() or ''
			s = GO_PLAY_PAT.sub(r'\1go run\2', s)
			s = s.strip()
			if s and s.lower() != "go" and self.change_history:
				hist = self.settings.get('cmd_hist')
				if not gs.is_a(hist, {}):
					hist = {}
				basedir = gs.basedir_or_cwd(file_name)
				hist[basedir] = [s] # todo: store a list of historical commands
				hst = {}
				for k in hist:
					# :|
					hst[gs.ustr(k)] = gs.ustr(hist[k])
				self.settings.set('cmd_hist', hst)
				sublime.save_settings('GoSublime-GsShell.sublime-settings')

			if GO_SHARE_PAT.match(s):
				s = ''
				host = "play.golang.org"
				warning = 'Are you sure you want to share this file. It will be public on %s' % host
				if not sublime.ok_cancel_dialog(warning):
					return

				try:
					c = httplib.HTTPConnection(host)
					src = gs.astr(self.view.substr(sublime.Region(0, self.view.size())))
					c.request('POST', '/share', src, {'User-Agent': 'GoSublime'})
					s = 'http://%s/p/%s' % (host, c.getresponse().read())
				except Exception as ex:
					s = 'Error: %s' % ex

				self.show_output(s, focus=True)
				return

			if GO_RUN_PAT.match(s):
				if not file_name:
					# todo: clean this up after the command runs
					err = ''
					tdir, _ = gs.temp_dir('play')
					file_name = hashlib.sha1(gs.view_fn(self.view) or 'a').hexdigest()
					file_name = os.path.join(tdir, ('%s.go' % file_name))
					try:
						with open(file_name, 'w') as f:
							src = gs.astr(self.view.substr(sublime.Region(0, self.view.size())))
							f.write(src)
					except Exception as ex:
						err = str(ex)

					if err:
						self.show_output('Error: %s' % err)
						return

				s = ['go', 'run', file_name]

			self.view.window().run_command("exec", { 'kill': True })
			if gs.is_a(s, []):
				use_shell = False
			else:
				use_shell = True
				s = [s]
			gs.println('running %s' % ' '.join(s))
			self.view.window().run_command("exec", {
				'shell': use_shell,
				'env': gs.env(),
				'cmd': s,
				'file_regex': '^(.+\.go):([0-9]+):(?:([0-9]+):)?\s*(.*)',
			})
Example #12
0
        def cb(s):
            file_name = self.view.file_name() or ''
            s = GO_PLAY_PAT.sub(r'\1go run\2', s)
            s = s.strip()
            if s and s.lower() != "go" and self.change_history:
                hist = self.settings.get('cmd_hist')
                if not gs.is_a(hist, {}):
                    hist = {}
                basedir = gs.basedir_or_cwd(file_name)
                hist[basedir] = [s
                                 ]  # todo: store a list of historical commands
                hst = {}
                for k in hist:
                    # :|
                    hst[gs.ustr(k)] = gs.ustr(hist[k])
                self.settings.set('cmd_hist', hst)
                sublime.save_settings('GoSublime-GsShell.sublime-settings')

            if GO_SHARE_PAT.match(s):
                s = ''
                host = "play.golang.org"
                warning = 'Are you sure you want to share this file. It will be public on %s' % host
                if not sublime.ok_cancel_dialog(warning):
                    return

                try:
                    c = httplib.HTTPConnection(host)
                    src = gs.astr(
                        self.view.substr(sublime.Region(0, self.view.size())))
                    c.request('POST', '/share', src,
                              {'User-Agent': 'GoSublime'})
                    s = 'http://%s/p/%s' % (host, c.getresponse().read())
                except Exception as ex:
                    s = 'Error: %s' % ex

                self.show_output(s, focus=True)
                return

            if GO_RUN_PAT.match(s):
                if not file_name:
                    # todo: clean this up after the command runs
                    err = ''
                    tdir, _ = gs.temp_dir('play')
                    file_name = hashlib.sha1(gs.view_fn(self.view)
                                             or 'a').hexdigest()
                    file_name = os.path.join(tdir, ('%s.go' % file_name))
                    try:
                        with open(file_name, 'w') as f:
                            src = gs.astr(
                                self.view.substr(
                                    sublime.Region(0, self.view.size())))
                            f.write(src)
                    except Exception as ex:
                        err = str(ex)

                    if err:
                        self.show_output('Error: %s' % err)
                        return

                s = ['go', 'run', file_name]

            self.view.window().run_command("exec", {'kill': True})
            if gs.is_a(s, []):
                use_shell = False
            else:
                use_shell = True
                s = [s]
            gs.println('running %s' % ' '.join(s))
            self.view.window().run_command(
                "exec", {
                    'shell': use_shell,
                    'env': gs.env(),
                    'cmd': s,
                    'file_regex': '^(.+\.go):([0-9]+):(?:([0-9]+):)?\s*(.*)',
                })
Example #13
0
        def cb(s):
            file_name = self.view.file_name() or ""
            s = GO_PLAY_PAT.sub(r"\1go run\2", s)
            s = s.strip()
            if s and s.lower() != "go" and self.change_history:
                hist = self.settings.get("cmd_hist")
                if not gs.is_a(hist, {}):
                    hist = {}
                basedir = gs.basedir_or_cwd(file_name)
                hist[basedir] = [s]  # todo: store a list of historical commands
                hst = {}
                for k in hist:
                    # :|
                    hst[gs.ustr(k)] = gs.ustr(hist[k])
                self.settings.set("cmd_hist", hst)
                sublime.save_settings("GoSublime-GsShell.sublime-settings")

            if GO_SHARE_PAT.match(s):
                s = ""
                host = "play.golang.org"
                warning = "Are you sure you want to share this file. It will be public on %s" % host
                if not sublime.ok_cancel_dialog(warning):
                    return

                try:
                    c = httplib.HTTPConnection(host)
                    src = gs.astr(self.view.substr(sublime.Region(0, self.view.size())))
                    c.request("POST", "/share", src, {"User-Agent": "GoSublime"})
                    s = "http://%s/p/%s" % (host, c.getresponse().read())
                except Exception as ex:
                    s = "Error: %s" % ex

                self.show_output(s, focus=True)
                return

            if GO_RUN_PAT.match(s):
                if not file_name:
                    # todo: clean this up after the command runs
                    err = ""
                    tdir, _ = gs.temp_dir("play")
                    file_name = hashlib.sha1(gs.view_fn(self.view) or "a").hexdigest()
                    file_name = os.path.join(tdir, ("%s.go" % file_name))
                    try:
                        with open(file_name, "w") as f:
                            src = gs.astr(self.view.substr(sublime.Region(0, self.view.size())))
                            f.write(src)
                    except Exception as ex:
                        err = str(ex)

                    if err:
                        self.show_output("Error: %s" % err)
                        return

                s = ["go", "run", file_name]

            self.view.window().run_command("exec", {"kill": True})
            if gs.is_a(s, []):
                use_shell = False
            else:
                use_shell = True
                s = [s]
            gs.println("running %s" % " ".join(s))
            self.view.window().run_command(
                "exec",
                {
                    "shell": use_shell,
                    "env": gs.env(),
                    "cmd": s,
                    "file_regex": "^(.+\.go):([0-9]+):(?:([0-9]+):)?\s*(.*)",
                },
            )