2010-03-11 18:53:45 -04:00
|
|
|
#! /usr/bin/env python3
|
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)
|
2007-01-10 12:19:56 -04:00
|
|
|
except getopt.GetoptError as msg:
|
2001-01-17 04:48:39 -04:00
|
|
|
sys.stdout = sys.stderr
|
2007-08-03 14:06:41 -03:00
|
|
|
print(msg)
|
|
|
|
print('usage: findlinksto pattern directory ...')
|
2001-01-17 04:48:39 -04:00
|
|
|
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:
|
2008-05-09 17:00:17 -03:00
|
|
|
os.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):
|
2007-08-03 14:06:41 -03:00
|
|
|
print('descend into', dirname)
|
2001-01-17 04:48:39 -04:00
|
|
|
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:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(name, '->', linkto)
|
2012-12-24 13:58:48 -04:00
|
|
|
except OSError:
|
2001-01-17 04:48:39 -04:00
|
|
|
pass
|
1991-06-04 17:36:54 -03:00
|
|
|
|
2004-08-09 14:27:55 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|