]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | #! /usr/bin/env python\r |
2 | \r | |
3 | # 3) System Test\r | |
4 | #\r | |
5 | # Given a list of directories, report any bogus symbolic links contained\r | |
6 | # anywhere in those subtrees. A bogus symbolic link is one that cannot\r | |
7 | # be resolved because it points to a nonexistent or otherwise\r | |
8 | # unresolvable file. Do *not* use an external find executable.\r | |
9 | # Directories may be very very deep. Print a warning immediately if the\r | |
10 | # system you're running on doesn't support symbolic links.\r | |
11 | \r | |
12 | # This implementation:\r | |
13 | # - takes one optional argument, using the current directory as default\r | |
14 | # - uses chdir to increase performance\r | |
15 | # - sorts the names per directory\r | |
16 | # - prints output lines of the form "path1 -> path2" as it goes\r | |
17 | # - prints error messages about directories it can't list or chdir into\r | |
18 | \r | |
19 | import os\r | |
20 | import sys\r | |
21 | from stat import *\r | |
22 | \r | |
23 | def main():\r | |
24 | try:\r | |
25 | # Note: can't test for presence of lstat -- it's always there\r | |
26 | dummy = os.readlink\r | |
27 | except AttributeError:\r | |
28 | print "This system doesn't have symbolic links"\r | |
29 | sys.exit(0)\r | |
30 | if sys.argv[1:]:\r | |
31 | prefix = sys.argv[1]\r | |
32 | else:\r | |
33 | prefix = ''\r | |
34 | if prefix:\r | |
35 | os.chdir(prefix)\r | |
36 | if prefix[-1:] != '/': prefix = prefix + '/'\r | |
37 | reportboguslinks(prefix)\r | |
38 | else:\r | |
39 | reportboguslinks('')\r | |
40 | \r | |
41 | def reportboguslinks(prefix):\r | |
42 | try:\r | |
43 | names = os.listdir('.')\r | |
44 | except os.error, msg:\r | |
45 | print "%s%s: can't list: %s" % (prefix, '.', msg)\r | |
46 | return\r | |
47 | names.sort()\r | |
48 | for name in names:\r | |
49 | if name == os.curdir or name == os.pardir:\r | |
50 | continue\r | |
51 | try:\r | |
52 | mode = os.lstat(name)[ST_MODE]\r | |
53 | except os.error:\r | |
54 | print "%s%s: can't stat: %s" % (prefix, name, msg)\r | |
55 | continue\r | |
56 | if S_ISLNK(mode):\r | |
57 | try:\r | |
58 | os.stat(name)\r | |
59 | except os.error:\r | |
60 | print "%s%s -> %s" % \\r | |
61 | (prefix, name, os.readlink(name))\r | |
62 | elif S_ISDIR(mode):\r | |
63 | try:\r | |
64 | os.chdir(name)\r | |
65 | except os.error, msg:\r | |
66 | print "%s%s: can't chdir: %s" % \\r | |
67 | (prefix, name, msg)\r | |
68 | continue\r | |
69 | try:\r | |
70 | reportboguslinks(prefix + name + '/')\r | |
71 | finally:\r | |
72 | os.chdir('..')\r | |
73 | \r | |
74 | main()\r |