Show More
@@ -0,0 +1,70 b'' | |||
|
1 | r""" %which magic command | |
|
2 | ||
|
3 | %which <cmd> => search PATH for files matching PATH. Also scans aliases | |
|
4 | ||
|
5 | """ | |
|
6 | ||
|
7 | import IPython.ipapi | |
|
8 | ip = IPython.ipapi.get() | |
|
9 | ||
|
10 | import os,sys | |
|
11 | from fnmatch import fnmatch | |
|
12 | def which(fname): | |
|
13 | fullpath = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) | |
|
14 | ||
|
15 | if '.' not in fullpath: | |
|
16 | fullpath = ['.'] + fullpath | |
|
17 | fn = fname | |
|
18 | for p in fullpath: | |
|
19 | for f in os.listdir(p): | |
|
20 | head, ext = os.path.splitext(f) | |
|
21 | if f == fn or fnmatch(head, fn): | |
|
22 | yield os.path.join(p,f) | |
|
23 | return | |
|
24 | ||
|
25 | def which_alias(fname): | |
|
26 | for al, tgt in ip.IP.alias_table.items(): | |
|
27 | if not (al == fname or fnmatch(al, fname)): | |
|
28 | continue | |
|
29 | trg = tgt[1] | |
|
30 | ||
|
31 | trans = ip.expand_alias(trg) | |
|
32 | cmd = trans.split(None,1)[0] | |
|
33 | print al,"->",trans | |
|
34 | for realcmd in which(cmd): | |
|
35 | print " ==",realcmd | |
|
36 | ||
|
37 | def which_f(self, arg): | |
|
38 | r""" %which <cmd> => search PATH for files matching cmd. Also scans aliases. | |
|
39 | ||
|
40 | Traverses PATH and prints all files (not just executables!) that match the | |
|
41 | pattern on command line. Probably more useful in finding stuff | |
|
42 | interactively than 'which', which only prints the first matching item. | |
|
43 | ||
|
44 | Also discovers and expands aliases, so you'll see what will be executed | |
|
45 | when you call an alias. | |
|
46 | ||
|
47 | Example: | |
|
48 | ||
|
49 | [~]|62> %which d | |
|
50 | d -> ls -F --color=auto | |
|
51 | == c:\cygwin\bin\ls.exe | |
|
52 | c:\cygwin\bin\d.exe | |
|
53 | ||
|
54 | [~]|64> %which diff* | |
|
55 | diff3 -> diff3 | |
|
56 | == c:\cygwin\bin\diff3.exe | |
|
57 | diff -> diff | |
|
58 | == c:\cygwin\bin\diff.exe | |
|
59 | c:\cygwin\bin\diff.exe | |
|
60 | c:\cygwin\bin\diff3.exe | |
|
61 | ||
|
62 | """ | |
|
63 | ||
|
64 | which_alias(arg) | |
|
65 | ||
|
66 | for e in which(arg): | |
|
67 | print e | |
|
68 | ||
|
69 | ip.expose_magic("which",which_f) | |
|
70 |
@@ -31,6 +31,7 b' def main():' | |||
|
31 | 31 | |
|
32 | 32 | # beefed up %env is handy in shell mode |
|
33 | 33 | import envpersist |
|
34 | import ipy_which | |
|
34 | 35 | |
|
35 | 36 | |
|
36 | 37 | ip.ex('import os') |
@@ -300,6 +300,7 b' class IPApi:' | |||
|
300 | 300 | |
|
301 | 301 | pre,fn,rest = self.IP.split_user_input(line) |
|
302 | 302 | res = pre + self.IP.expand_aliases(fn,rest) |
|
303 | return res | |
|
303 | 304 | |
|
304 | 305 | def defalias(self, name, cmd): |
|
305 | 306 | """ Define a new alias |
@@ -1,3 +1,12 b'' | |||
|
1 | 2007-04-25 Ville Vainio <vivainio@gmail.com> | |
|
2 | ||
|
3 | * Extensions/ipy_which.py: added extension for %which magic, works | |
|
4 | a lot like unix 'which' but also finds and expands aliases, and | |
|
5 | allows wildcards. | |
|
6 | ||
|
7 | * ipapi.py (expand_alias): Now actually *return* the expanded alias, | |
|
8 | as opposed to returning nothing. | |
|
9 | ||
|
1 | 10 | 2007-04-22 J�rgen Stenarson <jorgen.stenarson@bostream.nu> |
|
2 | 11 | |
|
3 | 12 | * Fix bug in iplib.py/safe_execfile when launching ipython with a script like ipython.py foo.py |
General Comments 0
You need to be logged in to leave comments.
Login now