Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 2 | |
| 3 | # Variant of "which". |
| 4 | # On stderr, near and total misses are reported. |
Guido van Rossum | fcd1e6e | 1992-03-02 16:17:31 +0000 | [diff] [blame] | 5 | # '-l<flags>' argument adds ls -l<flags> of each file found. |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 9476a78 | 1997-10-20 23:48:01 +0000 | [diff] [blame] | 7 | import sys |
| 8 | if sys.path[0] in (".", ""): del sys.path[0] |
| 9 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 10 | import sys, os |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 11 | from stat import * |
| 12 | |
| 13 | def msg(str): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 14 | sys.stderr.write(str + '\n') |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 15 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 16 | pathlist = os.environ['PATH'].split(':') |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 17 | |
| 18 | sts = 0 |
Guido van Rossum | fcd1e6e | 1992-03-02 16:17:31 +0000 | [diff] [blame] | 19 | longlist = '' |
| 20 | |
| 21 | if sys.argv[1:] and sys.argv[1][:2] == '-l': |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 22 | longlist = sys.argv[1] |
| 23 | del sys.argv[1] |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 24 | |
| 25 | for prog in sys.argv[1:]: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 26 | ident = () |
| 27 | for dir in pathlist: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 28 | filename = os.path.join(dir, prog) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 29 | try: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 30 | st = os.stat(filename) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 31 | except os.error: |
| 32 | continue |
| 33 | if not S_ISREG(st[ST_MODE]): |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 34 | msg(filename + ': not a disk file') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 35 | else: |
| 36 | mode = S_IMODE(st[ST_MODE]) |
| 37 | if mode & 0111: |
| 38 | if not ident: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 39 | print filename |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 40 | ident = st[:3] |
| 41 | else: |
| 42 | if st[:3] == ident: |
| 43 | s = 'same as: ' |
| 44 | else: |
| 45 | s = 'also: ' |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 46 | msg(s + filename) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 47 | else: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 48 | msg(filename + ': not executable') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 49 | if longlist: |
Andrew M. Kuchling | ac6df95 | 2003-05-13 18:14:25 +0000 | [diff] [blame] | 50 | sts = os.system('ls ' + longlist + ' ' + filename) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 51 | if sts: msg('"ls -l" exit status: ' + `sts`) |
| 52 | if not ident: |
| 53 | msg(prog + ': not found') |
| 54 | sts = 1 |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 55 | |
| 56 | sys.exit(sts) |