##// END OF EJS Templates
%rehashdir mods
vivainio -
Show More
@@ -1,90 +1,101 b''
1 1 # -*- coding: utf-8 -*-
2 2 """ IPython extension: add %rehashdir magic
3 3
4 4 Usage:
5 5
6 %rehash_dir c:/bin c:/tools
6 %rehashdir c:/bin c:/tools
7 7 - Add all executables under c:/bin and c:/tools to alias table, in
8 8 order to make them directly executable from any directory.
9 9
10 10 This also serves as an example on how to extend ipython
11 11 with new magic functions.
12 12
13 13 Unlike rest of ipython, this requires Python 2.4 (optional
14 14 extensions are allowed to do that).
15 15
16 16 To install, add
17 17
18 "import_mod rehash_dir"
18 "import_mod ext_rehashdir"
19 19
20 20 To your ipythonrc or just execute "import rehash_dir" in ipython
21 21 prompt.
22 22
23 23
24
25
26 24 $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $
27 25 """
28 26
29 27 import IPython.ipapi as ip
30 28
31 29
32 import os,re
30 import os,re,fnmatch
33 31
34 32 @ip.asmagic("rehashdir")
35 33 def rehashdir_f(self,arg):
36 34 """ Add executables in all specified dirs to alias table
37 35
38 36 Usage:
39 37
40 %rehash_dir c:/bin c:/tools
38 %rehashdir c:/bin c:/tools
41 39 - Add all executables under c:/bin and c:/tools to alias table, in
42 40 order to make them directly executable from any directory.
41
42 Without arguments, add all executables in current directory.
43
43 44 """
44 45
45 46 # most of the code copied from Magic.magic_rehashx
47
48 def isjunk(fname):
49 junk = ['*~']
50 for j in junk:
51 if fnmatch.fnmatch(fname, j):
52 return True
53 return False
54
46 55 if not arg:
47 56 arg = '.'
48 path = arg.split()
57 path = map(os.path.abspath,arg.split())
49 58 alias_table = self.shell.alias_table
50 59
51 60 if os.name == 'posix':
52 61 isexec = lambda fname:os.path.isfile(fname) and \
53 62 os.access(fname,os.X_OK)
54 63 else:
55 64
56 65 try:
57 66 winext = os.environ['pathext'].replace(';','|').replace('.','')
58 67 except KeyError:
59 winext = 'exe|com|bat'
68 winext = 'exe|com|bat|py'
60 69
61 70 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
62 71 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
63 72 savedir = os.getcwd()
64 73 try:
65 74 # write the whole loop for posix/Windows so we don't have an if in
66 75 # the innermost part
67 76 if os.name == 'posix':
68 77 for pdir in path:
69 78 os.chdir(pdir)
70 79 for ff in os.listdir(pdir):
71 if isexec(ff):
80 if isexec(ff) and not isjunk(ff):
72 81 # each entry in the alias table must be (N,name),
73 82 # where N is the number of positional arguments of the
74 83 # alias.
75 print "Aliasing",ff
76 alias_table[ff] = (0,os.path.abspath(ff))
84 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
85 print "Aliasing:",src,"->",tgt
86 alias_table[src] = (0,tgt)
77 87 else:
78 88 for pdir in path:
79 89 os.chdir(pdir)
80 90 for ff in os.listdir(pdir):
81 if isexec(ff):
82 print "Aliasing",ff
83 alias_table[execre.sub(r'\1',ff)] = (0,os.path.abspath(ff))
91 if isexec(ff) and not isjunk(ff):
92 src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
93 print "Aliasing:",src,"->",tgt
94 alias_table[src] = (0,tgt)
84 95 # Make sure the alias table doesn't contain keywords or builtins
85 96 self.shell.alias_table_validate()
86 97 # Call again init_auto_alias() so we get 'rm -i' and other
87 98 # modified aliases since %rehashx will probably clobber them
88 99 self.shell.init_auto_alias()
89 100 finally:
90 os.chdir(savedir) No newline at end of file
101 os.chdir(savedir)
General Comments 0
You need to be logged in to leave comments. Login now