diff options
author | Guido van Rossum <guido@python.org> | 1994-01-21 15:47:00 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1994-01-21 15:47:00 (GMT) |
commit | 5ec8b525d26aad1bc77ef81aefb081250478cd37 (patch) | |
tree | cacf11c0ed077a7ff005ffe61221d0206b6561f5 /Tools/scripts/findlinksto.py | |
parent | cb38cd01c7995f0e5a710a230ba4267243bf7fce (diff) | |
download | cpython-5ec8b525d26aad1bc77ef81aefb081250478cd37.zip cpython-5ec8b525d26aad1bc77ef81aefb081250478cd37.tar.gz cpython-5ec8b525d26aad1bc77ef81aefb081250478cd37.tar.bz2 |
Restructured main, use getopt, use regular expression search instead
of prefix match
Diffstat (limited to 'Tools/scripts/findlinksto.py')
-rwxr-xr-x | Tools/scripts/findlinksto.py | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/Tools/scripts/findlinksto.py b/Tools/scripts/findlinksto.py index 6fb5cd7..d2fc430 100755 --- a/Tools/scripts/findlinksto.py +++ b/Tools/scripts/findlinksto.py @@ -2,28 +2,41 @@ # findlinksto # -# find symbolic links to a given path +# find symbolic links to a path matching a regular expression -import os, sys +import os +import sys +import regex +import getopt -def visit(pattern, dirname, names): +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): if os.path.islink(dirname): names[:] = [] return if os.path.ismount(dirname): print 'descend into', dirname - n = len(pattern) for name in names: name = os.path.join(dirname, name) try: linkto = os.readlink(name) - if linkto[:n] == pattern: + if prog.search(linkto) >= 0: print name, '->', linkto except os.error: pass -def main(pattern, args): - for dirname in args: - os.path.walk(dirname, visit, pattern) - -main(sys.argv[1], sys.argv[2:]) +main() |