Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,105 +1,96 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 | %rehashdir c:/bin c:/tools |
|
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 |
|
|||
17 |
|
||||
18 | "import_mod ext_rehashdir" |
|
|||
19 |
|
||||
20 | To your ipythonrc or just execute "import rehash_dir" in ipython |
|
|||
21 | prompt. |
|
|||
22 |
|
||||
23 |
|
||||
24 | $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ |
|
|||
25 | """ |
|
16 | """ | |
26 |
|
17 | |||
27 | import IPython.ipapi |
|
18 | import IPython.ipapi | |
28 | ip = IPython.ipapi.get() |
|
19 | ip = IPython.ipapi.get() | |
29 |
|
20 | |||
30 |
|
21 | |||
31 | import os,re,fnmatch |
|
22 | import os,re,fnmatch | |
32 |
|
23 | |||
33 | def rehashdir_f(self,arg): |
|
24 | def rehashdir_f(self,arg): | |
34 | """ Add executables in all specified dirs to alias table |
|
25 | """ Add executables in all specified dirs to alias table | |
35 |
|
26 | |||
36 | Usage: |
|
27 | Usage: | |
37 |
|
28 | |||
38 | %rehashdir c:/bin;c:/tools |
|
29 | %rehashdir c:/bin;c:/tools | |
39 | - Add all executables under c:/bin and c:/tools to alias table, in |
|
30 | - Add all executables under c:/bin and c:/tools to alias table, in | |
40 | order to make them directly executable from any directory. |
|
31 | order to make them directly executable from any directory. | |
41 |
|
32 | |||
42 | Without arguments, add all executables in current directory. |
|
33 | Without arguments, add all executables in current directory. | |
43 |
|
34 | |||
44 | """ |
|
35 | """ | |
45 |
|
36 | |||
46 | # most of the code copied from Magic.magic_rehashx |
|
37 | # most of the code copied from Magic.magic_rehashx | |
47 |
|
38 | |||
48 | def isjunk(fname): |
|
39 | def isjunk(fname): | |
49 | junk = ['*~'] |
|
40 | junk = ['*~'] | |
50 | for j in junk: |
|
41 | for j in junk: | |
51 | if fnmatch.fnmatch(fname, j): |
|
42 | if fnmatch.fnmatch(fname, j): | |
52 | return True |
|
43 | return True | |
53 | return False |
|
44 | return False | |
54 |
|
45 | |||
55 | if not arg: |
|
46 | if not arg: | |
56 | arg = '.' |
|
47 | arg = '.' | |
57 | path = map(os.path.abspath,arg.split(';')) |
|
48 | path = map(os.path.abspath,arg.split(';')) | |
58 | alias_table = self.shell.alias_table |
|
49 | alias_table = self.shell.alias_table | |
59 |
|
50 | |||
60 | if os.name == 'posix': |
|
51 | if os.name == 'posix': | |
61 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
52 | isexec = lambda fname:os.path.isfile(fname) and \ | |
62 | os.access(fname,os.X_OK) |
|
53 | os.access(fname,os.X_OK) | |
63 | else: |
|
54 | else: | |
64 |
|
55 | |||
65 | try: |
|
56 | try: | |
66 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
57 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |
67 | except KeyError: |
|
58 | except KeyError: | |
68 | winext = 'exe|com|bat|py' |
|
59 | winext = 'exe|com|bat|py' | |
69 | if 'py' not in winext: |
|
60 | if 'py' not in winext: | |
70 | winext += '|py' |
|
61 | winext += '|py' | |
71 |
|
62 | |||
72 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
63 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |
73 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
64 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | |
74 | savedir = os.getcwd() |
|
65 | savedir = os.getcwd() | |
75 | try: |
|
66 | try: | |
76 | # write the whole loop for posix/Windows so we don't have an if in |
|
67 | # write the whole loop for posix/Windows so we don't have an if in | |
77 | # the innermost part |
|
68 | # the innermost part | |
78 | if os.name == 'posix': |
|
69 | if os.name == 'posix': | |
79 | for pdir in path: |
|
70 | for pdir in path: | |
80 | os.chdir(pdir) |
|
71 | os.chdir(pdir) | |
81 | for ff in os.listdir(pdir): |
|
72 | for ff in os.listdir(pdir): | |
82 | if isexec(ff) and not isjunk(ff): |
|
73 | if isexec(ff) and not isjunk(ff): | |
83 | # each entry in the alias table must be (N,name), |
|
74 | # each entry in the alias table must be (N,name), | |
84 | # where N is the number of positional arguments of the |
|
75 | # where N is the number of positional arguments of the | |
85 | # alias. |
|
76 | # alias. | |
86 | src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff) |
|
77 | src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff) | |
87 | print "Aliasing:",src,"->",tgt |
|
78 | print "Aliasing:",src,"->",tgt | |
88 | alias_table[src] = (0,tgt) |
|
79 | alias_table[src] = (0,tgt) | |
89 | else: |
|
80 | else: | |
90 | for pdir in path: |
|
81 | for pdir in path: | |
91 | os.chdir(pdir) |
|
82 | os.chdir(pdir) | |
92 | for ff in os.listdir(pdir): |
|
83 | for ff in os.listdir(pdir): | |
93 | if isexec(ff) and not isjunk(ff): |
|
84 | if isexec(ff) and not isjunk(ff): | |
94 | src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff) |
|
85 | src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff) | |
95 | src = src.lower() |
|
86 | src = src.lower() | |
96 | print "Aliasing:",src,"->",tgt |
|
87 | print "Aliasing:",src,"->",tgt | |
97 | alias_table[src] = (0,tgt) |
|
88 | alias_table[src] = (0,tgt) | |
98 | # Make sure the alias table doesn't contain keywords or builtins |
|
89 | # Make sure the alias table doesn't contain keywords or builtins | |
99 | self.shell.alias_table_validate() |
|
90 | self.shell.alias_table_validate() | |
100 | # Call again init_auto_alias() so we get 'rm -i' and other |
|
91 | # Call again init_auto_alias() so we get 'rm -i' and other | |
101 | # modified aliases since %rehashx will probably clobber them |
|
92 | # modified aliases since %rehashx will probably clobber them | |
102 | # self.shell.init_auto_alias() |
|
93 | # self.shell.init_auto_alias() | |
103 | finally: |
|
94 | finally: | |
104 | os.chdir(savedir) |
|
95 | os.chdir(savedir) | |
105 | ip.expose_magic("rehashdir",rehashdir_f) |
|
96 | ip.expose_magic("rehashdir",rehashdir_f) |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now