blob: 827633379021ca2f21340cc9e99998824e024064 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Guido van Rossum21bc15b1995-04-10 11:40:26 +00002
3# 3) System Test
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +00004#
Guido van Rossum21bc15b1995-04-10 11:40:26 +00005# Given a list of directories, report any bogus symbolic links contained
6# anywhere in those subtrees. A bogus symbolic link is one that cannot
7# be resolved because it points to a nonexistent or otherwise
8# unresolvable file. Do *not* use an external find executable.
9# Directories may be very very deep. Print a warning immediately if the
10# system you're running on doesn't support symbolic links.
11
12# This implementation:
13# - takes one optional argument, using the current directory as default
14# - uses chdir to increase performance
15# - sorts the names per directory
16# - prints output lines of the form "path1 -> path2" as it goes
17# - prints error messages about directories it can't list or chdir into
18
19import os
20import sys
21from stat import *
22
23def main():
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000024 try:
25 # Note: can't test for presence of lstat -- it's always there
26 dummy = os.readlink
27 except AttributeError:
Collin Winter6f2df4d2007-07-17 20:59:35 +000028 print("This system doesn't have symbolic links")
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000029 sys.exit(0)
30 if sys.argv[1:]:
31 prefix = sys.argv[1]
32 else:
33 prefix = ''
34 if prefix:
35 os.chdir(prefix)
36 if prefix[-1:] != '/': prefix = prefix + '/'
37 reportboguslinks(prefix)
38 else:
39 reportboguslinks('')
Guido van Rossum21bc15b1995-04-10 11:40:26 +000040
41def reportboguslinks(prefix):
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000042 try:
43 names = os.listdir('.')
Guido van Rossumb940e112007-01-10 16:19:56 +000044 except os.error as msg:
Collin Winter6f2df4d2007-07-17 20:59:35 +000045 print("%s%s: can't list: %s" % (prefix, '.', msg))
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000046 return
47 names.sort()
48 for name in names:
49 if name == os.curdir or name == os.pardir:
50 continue
51 try:
52 mode = os.lstat(name)[ST_MODE]
53 except os.error:
Collin Winter6f2df4d2007-07-17 20:59:35 +000054 print("%s%s: can't stat: %s" % (prefix, name, msg))
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000055 continue
56 if S_ISLNK(mode):
57 try:
58 os.stat(name)
59 except os.error:
Collin Winter6f2df4d2007-07-17 20:59:35 +000060 print("%s%s -> %s" % \
61 (prefix, name, os.readlink(name)))
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000062 elif S_ISDIR(mode):
63 try:
64 os.chdir(name)
Guido van Rossumb940e112007-01-10 16:19:56 +000065 except os.error as msg:
Collin Winter6f2df4d2007-07-17 20:59:35 +000066 print("%s%s: can't chdir: %s" % \
67 (prefix, name, msg))
Andrew M. Kuchling946c53e2003-04-24 17:13:18 +000068 continue
69 try:
70 reportboguslinks(prefix + name + '/')
71 finally:
72 os.chdir('..')
Guido van Rossum21bc15b1995-04-10 11:40:26 +000073
74main()