~ipython-dev/ipython/0.10.1

« back to all changes in this revision

Viewing changes to IPython/Extensions/ipy_which.py

  • Committer: Fernando Perez
  • Date: 2008-06-02 01:26:30 UTC
  • mfrom: (0.1.130 ipython-local)
  • Revision ID: fernando.perez@berkeley.edu-20080602012630-m14vezrhydzvahf8
Merge in all development done in bzr since February 16 2008.

At that time, a clean bzr branch was started from the SVN tree, but
without SVN history.  That SVN history has now been used as the basis
of this branch, and the development done on the history-less BZR
branch has been added and is the content of this merge.  

This branch will be the new official main line of development in
Launchpad (equivalent to the old SVN trunk).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
        if callable(tgt):
30
 
            print "Callable alias",tgt
31
 
            d = tgt.__doc__
32
 
            if d:
33
 
                print "Docstring:\n",d
34
 
                continue
35
 
        trg = tgt[1]
36
 
        
37
 
        trans = ip.expand_alias(trg)
38
 
        cmd = trans.split(None,1)[0]
39
 
        print al,"->",trans
40
 
        for realcmd in which(cmd):
41
 
            print "  ==",realcmd
42
 
        
43
 
def which_f(self, arg):
44
 
    r""" %which <cmd> => search PATH for files matching cmd. Also scans aliases.
45
 
 
46
 
    Traverses PATH and prints all files (not just executables!) that match the
47
 
    pattern on command line. Probably more useful in finding stuff
48
 
    interactively than 'which', which only prints the first matching item.
49
 
    
50
 
    Also discovers and expands aliases, so you'll see what will be executed
51
 
    when you call an alias.
52
 
    
53
 
    Example:
54
 
    
55
 
    [~]|62> %which d
56
 
    d -> ls -F --color=auto
57
 
      == c:\cygwin\bin\ls.exe
58
 
    c:\cygwin\bin\d.exe
59
 
    
60
 
    [~]|64> %which diff*
61
 
    diff3 -> diff3
62
 
      == c:\cygwin\bin\diff3.exe
63
 
    diff -> diff
64
 
      == c:\cygwin\bin\diff.exe
65
 
    c:\cygwin\bin\diff.exe
66
 
    c:\cygwin\bin\diff3.exe
67
 
 
68
 
    """
69
 
    
70
 
    which_alias(arg)
71
 
 
72
 
    for e in which(arg):
73
 
        print e
74
 
    
75
 
ip.expose_magic("which",which_f)        
76