ipy_rehashdir.py
140 lines
| 4.3 KiB
| text/x-python
|
PythonLexer
fperez
|
r281 | # -*- coding: utf-8 -*- | ||
""" IPython extension: add %rehashdir magic | ||||
Usage: | ||||
%rehashdir c:/bin c:/tools | ||||
- Add all executables under c:/bin and c:/tools to alias table, in | ||||
order to make them directly executable from any directory. | ||||
This also serves as an example on how to extend ipython | ||||
with new magic functions. | ||||
Unlike rest of ipython, this requires Python 2.4 (optional | ||||
extensions are allowed to do that). | ||||
""" | ||||
import IPython.ipapi | ||||
ip = IPython.ipapi.get() | ||||
vivainio
|
r801 | import os,re,fnmatch,sys | ||
fperez
|
r281 | |||
vivainio
|
r833 | def selflaunch(ip,line): | ||
vivainio
|
r801 | """ Launch python script with 'this' interpreter | ||
vivainio
|
r841 | e.g. d:\foo\ipykit.exe a.py | ||
vivainio
|
r801 | |||
""" | ||||
vivainio
|
r841 | |||
tup = line.split(None,1) | ||||
if len(tup) == 1: | ||||
print "Launching nested ipython session" | ||||
os.system(sys.executable) | ||||
return | ||||
cmd = sys.executable + ' ' + tup[1] | ||||
vivainio
|
r801 | print ">",cmd | ||
os.system(cmd) | ||||
class PyLauncher: | ||||
""" Invoke selflanucher on the specified script | ||||
This is mostly useful for associating with scripts using:: | ||||
_ip.defalias('foo',PyLauncher('foo_script.py')) | ||||
""" | ||||
def __init__(self,script): | ||||
self.script = os.path.abspath(script) | ||||
vivainio
|
r833 | def __call__(self, ip, line): | ||
vivainio
|
r841 | if self.script.endswith('.ipy'): | ||
ip.runlines(open(self.script).read()) | ||||
else: | ||||
vivainio
|
r861 | # first word is the script/alias name itself, strip it | ||
tup = line.split(None,1) | ||||
if len(tup) == 2: | ||||
tail = ' ' + tup[1] | ||||
else: | ||||
tail = '' | ||||
selflaunch(ip,"py " + self.script + tail) | ||||
vivainio
|
r801 | def __repr__(self): | ||
return 'PyLauncher("%s")' % self.script | ||||
vivainio
|
r841 | |||
fperez
|
r281 | def rehashdir_f(self,arg): | ||
""" Add executables in all specified dirs to alias table | ||||
Usage: | ||||
%rehashdir c:/bin;c:/tools | ||||
- Add all executables under c:/bin and c:/tools to alias table, in | ||||
order to make them directly executable from any directory. | ||||
Without arguments, add all executables in current directory. | ||||
""" | ||||
# most of the code copied from Magic.magic_rehashx | ||||
def isjunk(fname): | ||||
junk = ['*~'] | ||||
for j in junk: | ||||
if fnmatch.fnmatch(fname, j): | ||||
return True | ||||
return False | ||||
vivainio
|
r687 | created = [] | ||
fperez
|
r281 | if not arg: | ||
arg = '.' | ||||
path = map(os.path.abspath,arg.split(';')) | ||||
alias_table = self.shell.alias_table | ||||
if os.name == 'posix': | ||||
isexec = lambda fname:os.path.isfile(fname) and \ | ||||
os.access(fname,os.X_OK) | ||||
else: | ||||
try: | ||||
winext = os.environ['pathext'].replace(';','|').replace('.','') | ||||
except KeyError: | ||||
winext = 'exe|com|bat|py' | ||||
vivainio
|
r380 | if 'py' not in winext: | ||
winext += '|py' | ||||
fperez
|
r281 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | ||
isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | ||||
savedir = os.getcwd() | ||||
try: | ||||
# write the whole loop for posix/Windows so we don't have an if in | ||||
# the innermost part | ||||
if os.name == 'posix': | ||||
for pdir in path: | ||||
os.chdir(pdir) | ||||
for ff in os.listdir(pdir): | ||||
if isexec(ff) and not isjunk(ff): | ||||
# each entry in the alias table must be (N,name), | ||||
# where N is the number of positional arguments of the | ||||
# alias. | ||||
vivainio
|
r687 | src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff) | ||
created.append(src) | ||||
fperez
|
r281 | alias_table[src] = (0,tgt) | ||
else: | ||||
for pdir in path: | ||||
os.chdir(pdir) | ||||
for ff in os.listdir(pdir): | ||||
if isexec(ff) and not isjunk(ff): | ||||
src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff) | ||||
vivainio
|
r649 | src = src.lower() | ||
vivainio
|
r687 | created.append(src) | ||
fperez
|
r281 | alias_table[src] = (0,tgt) | ||
# Make sure the alias table doesn't contain keywords or builtins | ||||
self.shell.alias_table_validate() | ||||
# Call again init_auto_alias() so we get 'rm -i' and other | ||||
# modified aliases since %rehashx will probably clobber them | ||||
vivainio
|
r543 | # self.shell.init_auto_alias() | ||
fperez
|
r281 | finally: | ||
os.chdir(savedir) | ||||
vivainio
|
r687 | return created | ||
vivainio
|
r801 | |||
fperez
|
r281 | ip.expose_magic("rehashdir",rehashdir_f) | ||