-
-
Save wolf0403/4058299 to your computer and use it in GitHub Desktop.
Python shell that doesn't work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
from subprocess import call | |
import subprocess as sp | |
class _cmd (object): | |
def __init__(self, sh, cmd): | |
self.c = cmd | |
self.sh = sh | |
def __call__(self, *p, **kw): | |
print self.c, p | |
rt = call ([self.c] + list(p)) | |
self.sh.setenv({'?' : rt}) | |
def cmd_split(s): | |
l = s.split(' ') | |
# FIXME quoting here | |
print l | |
return l | |
def env_cat(**kw): | |
r = '' | |
for k in kw: | |
r += '%s=%s '%(str(k), str(kw[k])) | |
return r | |
class sh(object): | |
def __init__(self): | |
pass | |
def __getattr__(self, name): | |
# FIXME dir(_) etc won't work | |
return _cmd(self, name) | |
def __dir__(self): | |
return object.__dir__(self) | |
def __call__(self, *p, **env): | |
if len(p) == 1: | |
p = cmd_split (p[0]) | |
call (p, env=env) | |
def cd(self, d): | |
os.chdir(d) | |
return self | |
def setenv(self, *envmap, **kw): | |
if len(envmap) == 1 and type(envmap[0]) == type({}): | |
for k in envmap[0]: | |
os.environ[k] = str(envmap[0][k]) | |
for k in kw: | |
os.environ[k] = str(kw[k]) | |
return self | |
def env(self, *k, **ev): | |
if len(ev) == 0: | |
for _k in k: | |
print '%s=%s' % (_k, os.environ[_k] if _k in os.environ else '') | |
else: | |
os.system('/usr/bin/env ' + env_cat(**ev) + k[0]) | |
_ = sh() | |
def usage(): | |
return ''' | |
from s import _ | |
_.cd('/tmp').ls() | |
_('ls /tmp') | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment