2010-03-11 22:53:45 +00:00
|
|
|
#! /usr/bin/env python3
|
1991-06-04 20:36:54 +00:00
|
|
|
|
|
|
|
# findlinksto
|
|
|
|
#
|
1994-01-21 15:47:00 +00:00
|
|
|
# find symbolic links to a path matching a regular expression
|
1991-06-04 20:36:54 +00:00
|
|
|
|
1994-01-21 15:47:00 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2003-02-06 19:55:35 +00:00
|
|
|
import re
|
1994-01-21 15:47:00 +00:00
|
|
|
import getopt
|
1991-06-04 20:36:54 +00:00
|
|
|
|
1994-01-21 15:47:00 +00:00
|
|
|
def main():
|
2001-01-17 08:48:39 +00:00
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], '')
|
|
|
|
if len(args) < 2:
|
2003-02-06 19:55:35 +00:00
|
|
|
raise getopt.GetoptError('not enough arguments', None)
|
2007-01-10 16:19:56 +00:00
|
|
|
except getopt.GetoptError as msg:
|
2001-01-17 08:48:39 +00:00
|
|
|
sys.stdout = sys.stderr
|
2007-08-03 17:06:41 +00:00
|
|
|
print(msg)
|
|
|
|
print('usage: findlinksto pattern directory ...')
|
2001-01-17 08:48:39 +00:00
|
|
|
sys.exit(2)
|
|
|
|
pat, dirs = args[0], args[1:]
|
2003-02-06 19:55:35 +00:00
|
|
|
prog = re.compile(pat)
|
2001-01-17 08:48:39 +00:00
|
|
|
for dirname in dirs:
|
2008-05-09 20:00:17 +00:00
|
|
|
os.walk(dirname, visit, prog)
|
1994-01-21 15:47:00 +00:00
|
|
|
|
|
|
|
def visit(prog, dirname, names):
|
2001-01-17 08:48:39 +00:00
|
|
|
if os.path.islink(dirname):
|
|
|
|
names[:] = []
|
|
|
|
return
|
|
|
|
if os.path.ismount(dirname):
|
2007-08-03 17:06:41 +00:00
|
|
|
print('descend into', dirname)
|
2001-01-17 08:48:39 +00:00
|
|
|
for name in names:
|
|
|
|
name = os.path.join(dirname, name)
|
|
|
|
try:
|
|
|
|
linkto = os.readlink(name)
|
2003-02-06 19:55:35 +00:00
|
|
|
if prog.search(linkto) is not None:
|
2007-08-03 17:06:41 +00:00
|
|
|
print(name, '->', linkto)
|
2012-12-24 17:58:48 +00:00
|
|
|
except OSError:
|
2001-01-17 08:48:39 +00:00
|
|
|
pass
|
1991-06-04 20:36:54 +00:00
|
|
|
|
2004-08-09 17:27:55 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|