1993-04-01 22:50:35 +02:00
|
|
|
#! /usr/local/bin/python
|
1991-06-04 22:36:54 +02:00
|
|
|
|
|
|
|
# findlinksto
|
|
|
|
#
|
1994-01-21 16:47:00 +01:00
|
|
|
# find symbolic links to a path matching a regular expression
|
1991-06-04 22:36:54 +02:00
|
|
|
|
1994-01-21 16:47:00 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import regex
|
|
|
|
import getopt
|
1991-06-04 22:36:54 +02:00
|
|
|
|
1994-01-21 16:47:00 +01:00
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], '')
|
|
|
|
if len(args) < 2:
|
|
|
|
raise getopt.error, 'not enough arguments'
|
|
|
|
except getopt.error, msg:
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
print msg
|
|
|
|
print 'usage: findlinksto pattern directory ...'
|
|
|
|
sys.exit(2)
|
|
|
|
pat, dirs = args[0], args[1:]
|
|
|
|
prog = regex.compile(pat)
|
|
|
|
for dirname in dirs:
|
|
|
|
os.path.walk(dirname, visit, prog)
|
|
|
|
|
|
|
|
def visit(prog, dirname, names):
|
1992-03-30 13:14:36 +02:00
|
|
|
if os.path.islink(dirname):
|
1991-06-04 22:36:54 +02:00
|
|
|
names[:] = []
|
|
|
|
return
|
1992-03-30 13:14:36 +02:00
|
|
|
if os.path.ismount(dirname):
|
1991-06-04 22:36:54 +02:00
|
|
|
print 'descend into', dirname
|
|
|
|
for name in names:
|
1992-03-30 13:14:36 +02:00
|
|
|
name = os.path.join(dirname, name)
|
1991-06-04 22:36:54 +02:00
|
|
|
try:
|
1992-03-30 13:14:36 +02:00
|
|
|
linkto = os.readlink(name)
|
1994-01-21 16:47:00 +01:00
|
|
|
if prog.search(linkto) >= 0:
|
1991-06-04 22:36:54 +02:00
|
|
|
print name, '->', linkto
|
1992-03-30 13:14:36 +02:00
|
|
|
except os.error:
|
1991-06-04 22:36:54 +02:00
|
|
|
pass
|
|
|
|
|
1994-01-21 16:47:00 +01:00
|
|
|
main()
|