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