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