1996-11-27 15:52:01 -04:00
|
|
|
#! /usr/bin/env python
|
1991-06-04 17:36:54 -03:00
|
|
|
|
|
|
|
# findlinksto
|
|
|
|
#
|
1994-01-21 11:47:00 -04:00
|
|
|
# find symbolic links to a path matching a regular expression
|
1991-06-04 17:36:54 -03:00
|
|
|
|
1994-01-21 11:47:00 -04:00
|
|
|
import os
|
|
|
|
import sys
|
2003-02-06 15:55:35 -04:00
|
|
|
import re
|
1994-01-21 11:47:00 -04:00
|
|
|
import getopt
|
1991-06-04 17:36:54 -03:00
|
|
|
|
1994-01-21 11:47:00 -04:00
|
|
|
def main():
|
2001-01-17 04:48:39 -04:00
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], '')
|
|
|
|
if len(args) < 2:
|
2003-02-06 15:55:35 -04:00
|
|
|
raise getopt.GetoptError('not enough arguments', None)
|
|
|
|
except getopt.GetoptError, msg:
|
2001-01-17 04:48:39 -04:00
|
|
|
sys.stdout = sys.stderr
|
|
|
|
print msg
|
|
|
|
print 'usage: findlinksto pattern directory ...'
|
|
|
|
sys.exit(2)
|
|
|
|
pat, dirs = args[0], args[1:]
|
2003-02-06 15:55:35 -04:00
|
|
|
prog = re.compile(pat)
|
2001-01-17 04:48:39 -04:00
|
|
|
for dirname in dirs:
|
|
|
|
os.path.walk(dirname, visit, prog)
|
1994-01-21 11:47:00 -04:00
|
|
|
|
|
|
|
def visit(prog, dirname, names):
|
2001-01-17 04:48:39 -04:00
|
|
|
if os.path.islink(dirname):
|
|
|
|
names[:] = []
|
|
|
|
return
|
|
|
|
if os.path.ismount(dirname):
|
|
|
|
print 'descend into', dirname
|
|
|
|
for name in names:
|
|
|
|
name = os.path.join(dirname, name)
|
|
|
|
try:
|
|
|
|
linkto = os.readlink(name)
|
2003-02-06 15:55:35 -04:00
|
|
|
if prog.search(linkto) is not None:
|
2001-01-17 04:48:39 -04:00
|
|
|
print name, '->', linkto
|
|
|
|
except os.error:
|
|
|
|
pass
|
1991-06-04 17:36:54 -03:00
|
|
|
|
1994-01-21 11:47:00 -04:00
|
|
|
main()
|