blob: 00d4a187c88778f91ebeff7a89110e2b119425c3 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumec758ea1991-06-04 20:36:54 +00002
3# findlinksto
4#
Guido van Rossum5ec8b521994-01-21 15:47:00 +00005# find symbolic links to a path matching a regular expression
Guido van Rossumec758ea1991-06-04 20:36:54 +00006
Guido van Rossum5ec8b521994-01-21 15:47:00 +00007import os
8import sys
Andrew M. Kuchlingd6835042003-02-06 19:55:35 +00009import re
Guido van Rossum5ec8b521994-01-21 15:47:00 +000010import getopt
Guido van Rossumec758ea1991-06-04 20:36:54 +000011
Guido van Rossum5ec8b521994-01-21 15:47:00 +000012def main():
Tim Peters70c43782001-01-17 08:48:39 +000013 try:
14 opts, args = getopt.getopt(sys.argv[1:], '')
15 if len(args) < 2:
Andrew M. Kuchlingd6835042003-02-06 19:55:35 +000016 raise getopt.GetoptError('not enough arguments', None)
17 except getopt.GetoptError, msg:
Tim Peters70c43782001-01-17 08:48:39 +000018 sys.stdout = sys.stderr
19 print msg
20 print 'usage: findlinksto pattern directory ...'
21 sys.exit(2)
22 pat, dirs = args[0], args[1:]
Andrew M. Kuchlingd6835042003-02-06 19:55:35 +000023 prog = re.compile(pat)
Tim Peters70c43782001-01-17 08:48:39 +000024 for dirname in dirs:
25 os.path.walk(dirname, visit, prog)
Guido van Rossum5ec8b521994-01-21 15:47:00 +000026
27def visit(prog, dirname, names):
Tim Peters70c43782001-01-17 08:48:39 +000028 if os.path.islink(dirname):
29 names[:] = []
30 return
31 if os.path.ismount(dirname):
32 print 'descend into', dirname
33 for name in names:
34 name = os.path.join(dirname, name)
35 try:
36 linkto = os.readlink(name)
Andrew M. Kuchlingd6835042003-02-06 19:55:35 +000037 if prog.search(linkto) is not None:
Tim Peters70c43782001-01-17 08:48:39 +000038 print name, '->', linkto
39 except os.error:
40 pass
Guido van Rossumec758ea1991-06-04 20:36:54 +000041
Guido van Rossum5ec8b521994-01-21 15:47:00 +000042main()