def fix_shell_cmd(shell, cmd): if not gs.is_a(cmd, []): cmd = [cmd] if shell: cmd_str = ' '.join(cmd) sh = gs.setting('shell') if not sh: return (shell, gs.astr(cmd_str)) shell = False cmd_map = {'CMD': cmd_str} cmd = [] for v in sh: if v: cmd.append(string.Template(v).safe_substitute(cmd_map)) return (shell, [gs.astr(v) for v in cmd])
def fix_shell_cmd(shell, cmd): if not gs.is_a(cmd, []): cmd = [cmd] if shell: cmd_str = " ".join(cmd) sh = gs.setting("shell") if not sh: return (shell, gs.astr(cmd_str)) shell = False cmd_map = {"CMD": cmd_str} cmd = [] for v in sh: if v: cmd.append(string.Template(v).safe_substitute(cmd_map)) return (shell, [gs.astr(v) for v in cmd])
def proc(cmd, shell=False, env={}, cwd=None, input=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, bufsize=0): env = gs.env(env) shell, cmd = fix_shell_cmd(shell, cmd) if input is not None: input = gs.astr(input) if cwd: try: os.makedirs(cwd) except Exception: pass else: # an empty string isn't a valid value so just always set it None cwd = None try: setsid = os.setsid except Exception: setsid = None opts = { 'cmd': cmd, 'shell': shell, 'env': env, 'input': input, } p = None err = '' try: p = subprocess.Popen(cmd, stdout=stdout, stderr=stderr, stdin=stdin, startupinfo=gs.STARTUP_INFO, shell=shell, env=env, cwd=cwd, preexec_fn=setsid, bufsize=bufsize) except Exception: err = 'Error running command %s: %s' % (cmd, gs.traceback()) return (p, opts, err)
def proc( cmd, shell=False, env={}, cwd=None, input=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, bufsize=0, ): env = sh.env(env) shell, cmd = fix_shell_cmd(shell, cmd) if input is not None: input = gs.astr(input) if cwd: try: os.makedirs(cwd) except Exception: pass else: # an empty string isn't a valid value so just always set it None cwd = None try: setsid = os.setsid except Exception: setsid = None opts = {"cmd": cmd, "shell": shell, "env": env, "input": input} p = None err = "" try: p = subprocess.Popen( cmd, stdout=stdout, stderr=stderr, stdin=stdin, startupinfo=gs.STARTUP_INFO, shell=shell, env=env, cwd=cwd, preexec_fn=setsid, bufsize=bufsize, ) except Exception: err = "Error running command %s: %s" % (cmd, gs.traceback()) return (p, opts, err)
def fix_shell_cmd(shell, cmd): if not gs.is_a(cmd, []): cmd = [cmd] if shell: sh = gs.setting('shell') cmd_str = ' '.join(cmd) cmd_map = {'CMD': cmd_str} if sh: shell = False cmd = [] for v in sh: if v: cmd.append(string.Template(v).safe_substitute(cmd_map)) else: cmd = [cmd_str] return (shell, [gs.astr(v) for v in cmd])
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: gs.error_traceback(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') nv = sh.env() anv = nv.copy() seen = {} am = aliases() while True: cli = cmd.split(' ', 1) nm = cli[0] if not nm: break ag = cli[1].strip() if len(cli) == 2 else '' alias = am.get(nm, '') if not alias: break if alias in seen: gs.error(DOMAIN, 'recursive alias detected: `%s`' % alias) break seen[alias] = True anv['_args'] = ag cmd = string.Template(alias).safe_substitute(anv) if nm != 'sh': f = builtins().get(nm) if f: args = [] if ag: args = [_exparg(s, nv) for s in shlex.split(gs.astr(ag))] f(view, edit, args, wd, rkey) return if nm == 'sh': args = sh.cmd(ag) else: args = sh.cmd(cmd) cmd_sh(view, edit, args, wd, rkey) else: view.insert(edit, gs.sel(view).begin(), '\n')
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = '9o.hist.%s' % wd hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') cli = cmd.split(' ', 1) # todo: move this into margo if cli[0] == 'sh': def on_done(c): out = gs.ustr('\n'.join(c.consume_outq())) sublime.set_timeout(lambda: push_output(view, rkey, out), 0) c = gsshell.Command(cmd=cli[1], shell=True, cwd=wd) c.on_done = on_done c.start() return f = globals().get('cmd_%s' % cli[0]) if f: args = shlex.split(gs.astr(cli[1])) if len(cli) == 2 else [] f(view, edit, args, wd, rkey) else: push_output(view, rkey, 'Invalid command %s' % cli) else: view.insert(edit, gs.sel(view).begin(), '\n')
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') cli = cmd.split(' ', 1) nm = cli[0] ag = cli[1].strip() if len(cli) == 2 else '' if nm == "cd": args = [ag] if ag else [] cmd_cd(view, edit, args, wd, rkey) return # todo: move this into margo if nm == 'sh': def on_done(c): out = gs.ustr('\n'.join(c.consume_outq())) sublime.set_timeout(lambda: push_output(view, rkey, out), 0) c = gsshell.Command(cmd=ag, shell=True, cwd=wd) c.on_done = on_done c.start() return f = gs.gs9o.get(nm) or globals().get('cmd_%s' % nm) if f: args = shlex.split(gs.astr(ag)) if ag else [] f(view, edit, args, wd, rkey) else: push_output(view, rkey, 'Invalid command %s' % cli) else: view.insert(edit, gs.sel(view).begin(), '\n')
def _exec(view, edit, save_hist=False): pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: ui.trace(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return line = view.full_line(pos) ctx = '9o.exec.%s' % gs.uid() view.replace(edit, line, ('[`%s`]\n' % cmd)) view.run_command('gs9o_init') ep = view.full_line(line.begin()).end() view.add_regions(ctx, [sublime.Region(ep, ep)], '') hellip = u'[ \u22EF ]' ep += 1 view.insert(edit, ep, hellip+'\n\n') view.add_regions(ctx+'.done', [sublime.Region(ep, ep+len(hellip))], '') cli = cmd.split(' ', 1) if cli[0] == 'sh': a = cli[1].strip() if len(cli) == 2 else '' mk_cmd(view, wd, ctx, sh.cmd(a)).start() return nv = sh.env() a = [_exparg(s, nv) for s in shlex.split(gs.astr(cmd))] f = builtins().get(a[0]) if f: f(view, edit, a[1:], wd, ctx) else: mk_cmd(view, wd, ctx, a).start() else: view.insert(edit, gs.sel(view).begin(), '\n')
def _exec(view, edit, save_hist=False): pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: ui.trace(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return line = view.full_line(pos) ctx = '9o.exec.%s' % gs.uid() view.replace(edit, line, ('[`%s`]\n' % cmd)) view.run_command('gs9o_init') ep = view.full_line(line.begin()).end() view.add_regions(ctx, [sublime.Region(ep, ep)], '') hellip = u'[ \u22EF ]' ep += 1 view.insert(edit, ep, hellip + '\n\n') view.add_regions(ctx + '.done', [sublime.Region(ep, ep + len(hellip))], '') cli = cmd.split(' ', 1) if cli[0] == 'sh': a = cli[1].strip() if len(cli) == 2 else '' mk_cmd(view, wd, ctx, sh.cmd(a)).start() return nv = sh.env() a = [_exparg(s, nv) for s in shlex.split(gs.astr(cmd))] f = builtins().get(a[0]) if f: f(view, edit, a[1:], wd, ctx) else: mk_cmd(view, wd, ctx, a).start() else: view.insert(edit, gs.sel(view).begin(), '\n')
def run(self, edit, save_hist=False): view = self.view pos = gs.sel(view).begin() line = view.line(pos) wd = view.settings().get('9o.wd') try: os.chdir(wd) except Exception: gs.error_traceback(DOMAIN) ln = view.substr(line).split('#', 1) if len(ln) == 2: cmd = ln[1].strip() if cmd: vs = view.settings() aso = gs.aso() hkey = _hkey(wd) hist = gs.dval(aso.get(hkey), []) m = HIST_EXPAND_PAT.match(cmd) if m: pfx = m.group(1) hl = len(hist) idx = hl - int(m.group(2)) cmd = '' if idx >= 0 and idx < hl: cmd = hist[idx] if pfx == '^' or not cmd: view.replace(edit, line, ('%s# %s' % (ln[0], cmd))) return elif save_hist: try: hist.remove(cmd) except ValueError: pass hist.append(cmd) aso.set(hkey, hist) gs.save_aso() if not cmd: view.run_command('gs9o_init') return view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS))) rkey = '9o.exec.%s' % uuid.uuid4() view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '') view.run_command('gs9o_init') nv = sh.env() anv = nv.copy() seen = {} am = aliases() while True: cli = cmd.split(' ', 1) nm = cli[0] if not nm: break ag = cli[1].strip() if len(cli) == 2 else '' alias = am.get(nm, '') if not alias: break if alias in seen: gs.error(DOMAIN, 'recursive alias detected: `%s`' % alias) break seen[alias] = True anv['_args'] = ag cmd = string.Template(alias).safe_substitute(anv) if nm != 'sh': f = builtins().get(nm) if f: args = [] if ag: args = [ _exparg(s, nv) for s in shlex.split(gs.astr(ag)) ] f(view, edit, args, wd, rkey) return if nm == 'sh': args = sh.cmd(ag) else: args = sh.cmd(cmd) cmd_sh(view, edit, args, wd, rkey) else: view.insert(edit, gs.sel(view).begin(), '\n')