cpython/Tools/scripts/findlinksto.py

43 lines
870 B
Python
Raw Normal View History

#! /usr/bin/env python
1991-06-04 17:36:54 -03:00
# findlinksto
#
# find symbolic links to a path matching a regular expression
1991-06-04 17:36:54 -03:00
import os
import sys
import regex
import getopt
1991-06-04 17:36:54 -03: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 07:14:36 -04:00
if os.path.islink(dirname):
1991-06-04 17:36:54 -03:00
names[:] = []
return
1992-03-30 07:14:36 -04:00
if os.path.ismount(dirname):
1991-06-04 17:36:54 -03:00
print 'descend into', dirname
for name in names:
1992-03-30 07:14:36 -04:00
name = os.path.join(dirname, name)
1991-06-04 17:36:54 -03:00
try:
1992-03-30 07:14:36 -04:00
linkto = os.readlink(name)
if prog.search(linkto) >= 0:
1991-06-04 17:36:54 -03:00
print name, '->', linkto
1992-03-30 07:14:36 -04:00
except os.error:
1991-06-04 17:36:54 -03:00
pass
main()