Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/findlinksto.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 981fff7363b9 |
| children | 6b18a095c2e7 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
1525
diff
changeset
|
1 #! /usr/bin/env python |
| 274 | 2 |
| 3 # findlinksto | |
| 4 # | |
|
1525
dc8aed8b204d
Restructured main, use getopt, use regular expression search instead
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
5 # find symbolic links to a path matching a regular expression |
|
dc8aed8b204d
Restructured main, use getopt, use regular expression search instead
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
6 |
|
dc8aed8b204d
Restructured main, use getopt, use regular expression search instead
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
7 import os |
|
dc8aed8b204d
Restructured main, use getopt, use regular expression search instead
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
8 import sys |
|
27734
7b2cd800c229
Use new name for GetoptError, and pass it two arguments
Andrew M. Kuchling <amk@amk.ca>
parents:
15930
diff
changeset
|
9 import re |
|
1525
dc8aed8b204d
Restructured main, use getopt, use regular expression search instead
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
10 import getopt |
| 274 | 11 |
|
1525
dc8aed8b204d
Restructured main, use getopt, use regular expression search instead
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
12 def main(): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
13 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
14 opts, args = getopt.getopt(sys.argv[1:], '') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
15 if len(args) < 2: |
|
27734
7b2cd800c229
Use new name for GetoptError, and pass it two arguments
Andrew M. Kuchling <amk@amk.ca>
parents:
15930
diff
changeset
|
16 raise getopt.GetoptError('not enough arguments', None) |
|
7b2cd800c229
Use new name for GetoptError, and pass it two arguments
Andrew M. Kuchling <amk@amk.ca>
parents:
15930
diff
changeset
|
17 except getopt.GetoptError, msg: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
18 sys.stdout = sys.stderr |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
19 print msg |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
20 print 'usage: findlinksto pattern directory ...' |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
21 sys.exit(2) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
22 pat, dirs = args[0], args[1:] |
|
27734
7b2cd800c229
Use new name for GetoptError, and pass it two arguments
Andrew M. Kuchling <amk@amk.ca>
parents:
15930
diff
changeset
|
23 prog = re.compile(pat) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
24 for dirname in dirs: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
25 os.path.walk(dirname, visit, prog) |
| 274 | 26 |
|
1525
dc8aed8b204d
Restructured main, use getopt, use regular expression search instead
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
27 def visit(prog, dirname, names): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
28 if os.path.islink(dirname): |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
29 names[:] = [] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
30 return |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
31 if os.path.ismount(dirname): |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
32 print 'descend into', dirname |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
33 for name in names: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
34 name = os.path.join(dirname, name) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
35 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
36 linkto = os.readlink(name) |
|
27734
7b2cd800c229
Use new name for GetoptError, and pass it two arguments
Andrew M. Kuchling <amk@amk.ca>
parents:
15930
diff
changeset
|
37 if prog.search(linkto) is not None: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
38 print name, '->', linkto |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
39 except os.error: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
40 pass |
| 274 | 41 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
27734
diff
changeset
|
42 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
27734
diff
changeset
|
43 main() |
