diff --git a/IPython/Extensions/rehash_dir.py b/IPython/Extensions/rehash_dir.py new file mode 100644 index 0000000..763e40b --- /dev/null +++ b/IPython/Extensions/rehash_dir.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- +""" IPython extension: add %rehashdir magic + +Usage: + +%rehash_dir 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). + +To install, add + +"import_mod rehash_dir" + +To your ipythonrc or just execute "import rehash_dir" in ipython +prompt. + + + + +$Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ +""" + +import IPython.ipapi as ip + + +import os,re + +@ip.asmagic("rehashdir") +def rehashdir_f(self,arg): + """ Add executables in all specified dirs to alias table + + Usage: + + %rehash_dir 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. + """ + + # most of the code copied from Magic.magic_rehashx + if not arg: + arg = '.' + path = 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' + + 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): + # each entry in the alias table must be (N,name), + # where N is the number of positional arguments of the + # alias. + print "Aliasing",ff + alias_table[ff] = (0,os.path.abspath(ff)) + else: + for pdir in path: + os.chdir(pdir) + for ff in os.listdir(pdir): + if isexec(ff): + print "Aliasing",ff + alias_table[execre.sub(r'\1',ff)] = (0,os.path.abspath(ff)) + # 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 + self.shell.init_auto_alias() + finally: + os.chdir(savedir) \ No newline at end of file diff --git a/IPython/ipmaker.py b/IPython/ipmaker.py index a03ab72..9394fab 100644 --- a/IPython/ipmaker.py +++ b/IPython/ipmaker.py @@ -6,7 +6,7 @@ Requires Python 2.1 or better. This file contains the main make_IPython() starter function. -$Id: ipmaker.py 1017 2006-01-14 09:46:45Z vivainio $""" +$Id: ipmaker.py 1033 2006-01-20 10:41:20Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -478,6 +478,7 @@ object? -> Details about 'object'. ?object also works, ?? prints more. # add personal .ipython dir to sys.path so that users can put things in # there for customization sys.path.append(IP_rc.ipythondir) + sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran # update IP_rc with some special things that need manual @@ -560,6 +561,8 @@ object? -> Details about 'object'. ?object also works, ?? prints more. print 'loaded first).\n' pprint(IP_rc.__dict__) + # Make it easy to import extensions + sys.path.append(os.path.join(IPython_dir,"Extensions")) for mod in IP_rc.import_mod: try: exec 'import '+mod in IP.user_ns diff --git a/doc/ChangeLog b/doc/ChangeLog index d54c4c0..1019e45 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,9 @@ +2006-01-20 Ville Vainio + + * Ipython/Extensions/rehash_dir.py: Created a usable example + of how to extend ipython with new magics. Also added Extensions + dir to pythonpath to make executing extensions easy. + 2006-01-20 Fernando Perez * IPython/iplib.py (raw_input): I _think_ I got the pasting of