0
0
mirror of https://github.com/python/cpython.git synced 2024-12-01 03:01:36 +01:00
cpython/Tools/scripts/findlinksto.py

43 lines
872 B
Python
Raw Normal View History

1993-04-01 22:50:35 +02:00
#! /usr/local/bin/python
1991-06-04 22:36:54 +02:00
# findlinksto
#
# find symbolic links to a path matching a regular expression
1991-06-04 22:36:54 +02:00
import os
import sys
import regex
import getopt
1991-06-04 22:36:54 +02: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)
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
main()