Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/which.py @ 32260:cdad005bc030 legacy-trunk
[Bug #936837] Add missing word
| author | Andrew M. Kuchling <amk@amk.ca> |
|---|---|
| date | Sat, 05 Jun 2004 19:00:55 +0000 |
| parents | 71a294bd7950 |
| children | 981fff7363b9 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
1 #! /usr/bin/env python |
| 274 | 2 |
| 3 # Variant of "which". | |
| 4 # On stderr, near and total misses are reported. | |
|
685
ca28a3a82bba
Added '-l[flags]' option.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
5 # '-l<flags>' argument adds ls -l<flags> of each file found. |
| 274 | 6 |
|
6506
df6dfaac2d0b
Remove "." or "" from front of sys.path (this bit me once).
Guido van Rossum <guido@python.org>
parents:
4573
diff
changeset
|
7 import sys |
|
df6dfaac2d0b
Remove "." or "" from front of sys.path (this bit me once).
Guido van Rossum <guido@python.org>
parents:
4573
diff
changeset
|
8 if sys.path[0] in (".", ""): del sys.path[0] |
|
df6dfaac2d0b
Remove "." or "" from front of sys.path (this bit me once).
Guido van Rossum <guido@python.org>
parents:
4573
diff
changeset
|
9 |
|
25219
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
15930
diff
changeset
|
10 import sys, os |
| 274 | 11 from stat import * |
| 12 | |
| 13 def msg(str): | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
14 sys.stderr.write(str + '\n') |
| 274 | 15 |
|
29928
86c476a425a7
fix for bug 773020 - splitting PATH should use os.pathsep
Skip Montanaro <skip@pobox.com>
parents:
29107
diff
changeset
|
16 pathlist = os.environ['PATH'].split(os.pathsep) |
| 274 | 17 |
| 18 sts = 0 | |
|
685
ca28a3a82bba
Added '-l[flags]' option.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
19 longlist = '' |
|
ca28a3a82bba
Added '-l[flags]' option.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
20 |
|
ca28a3a82bba
Added '-l[flags]' option.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
21 if sys.argv[1:] and sys.argv[1][:2] == '-l': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
22 longlist = sys.argv[1] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
23 del sys.argv[1] |
| 274 | 24 |
| 25 for prog in sys.argv[1:]: | |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
26 ident = () |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
27 for dir in pathlist: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
28 filename = os.path.join(dir, prog) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
29 try: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
30 st = os.stat(filename) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
31 except os.error: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
32 continue |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
33 if not S_ISREG(st[ST_MODE]): |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
34 msg(filename + ': not a disk file') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
35 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
36 mode = S_IMODE(st[ST_MODE]) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
37 if mode & 0111: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
38 if not ident: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
39 print filename |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
40 ident = st[:3] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
41 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
42 if st[:3] == ident: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
43 s = 'same as: ' |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
44 else: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
45 s = 'also: ' |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
46 msg(s + filename) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
47 else: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
48 msg(filename + ': not executable') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
49 if longlist: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
50 sts = os.system('ls ' + longlist + ' ' + filename) |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
29928
diff
changeset
|
51 if sts: msg('"ls -l" exit status: ' + repr(sts)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
52 if not ident: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
53 msg(prog + ': not found') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
6506
diff
changeset
|
54 sts = 1 |
| 274 | 55 |
| 56 sys.exit(sts) |
