]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.2/Demo/comparisons/systemtest.py
AppPkg/Applications/Python: Add Python 2.7.2 sources since the release of Python...
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Demo / comparisons / systemtest.py
diff --git a/AppPkg/Applications/Python/Python-2.7.2/Demo/comparisons/systemtest.py b/AppPkg/Applications/Python/Python-2.7.2/Demo/comparisons/systemtest.py
new file mode 100644 (file)
index 0000000..a715fe0
--- /dev/null
@@ -0,0 +1,74 @@
+#! /usr/bin/env python\r
+\r
+# 3)  System Test\r
+#\r
+#     Given a list of directories, report any bogus symbolic links contained\r
+#     anywhere in those subtrees.  A bogus symbolic link is one that cannot\r
+#     be resolved because it points to a nonexistent or otherwise\r
+#     unresolvable file.  Do *not* use an external find executable.\r
+#     Directories may be very very deep.  Print a warning immediately if the\r
+#     system you're running on doesn't support symbolic links.\r
+\r
+# This implementation:\r
+# - takes one optional argument, using the current directory as default\r
+# - uses chdir to increase performance\r
+# - sorts the names per directory\r
+# - prints output lines of the form "path1 -> path2" as it goes\r
+# - prints error messages about directories it can't list or chdir into\r
+\r
+import os\r
+import sys\r
+from stat import *\r
+\r
+def main():\r
+    try:\r
+        # Note: can't test for presence of lstat -- it's always there\r
+        dummy = os.readlink\r
+    except AttributeError:\r
+        print "This system doesn't have symbolic links"\r
+        sys.exit(0)\r
+    if sys.argv[1:]:\r
+        prefix = sys.argv[1]\r
+    else:\r
+        prefix = ''\r
+    if prefix:\r
+        os.chdir(prefix)\r
+        if prefix[-1:] != '/': prefix = prefix + '/'\r
+        reportboguslinks(prefix)\r
+    else:\r
+        reportboguslinks('')\r
+\r
+def reportboguslinks(prefix):\r
+    try:\r
+        names = os.listdir('.')\r
+    except os.error, msg:\r
+        print "%s%s: can't list: %s" % (prefix, '.', msg)\r
+        return\r
+    names.sort()\r
+    for name in names:\r
+        if name == os.curdir or name == os.pardir:\r
+            continue\r
+        try:\r
+            mode = os.lstat(name)[ST_MODE]\r
+        except os.error:\r
+            print "%s%s: can't stat: %s" % (prefix, name, msg)\r
+            continue\r
+        if S_ISLNK(mode):\r
+            try:\r
+                os.stat(name)\r
+            except os.error:\r
+                print "%s%s -> %s" % \\r
+                      (prefix, name, os.readlink(name))\r
+        elif S_ISDIR(mode):\r
+            try:\r
+                os.chdir(name)\r
+            except os.error, msg:\r
+                print "%s%s: can't chdir: %s" % \\r
+                      (prefix, name, msg)\r
+                continue\r
+            try:\r
+                reportboguslinks(prefix + name + '/')\r
+            finally:\r
+                os.chdir('..')\r
+\r
+main()\r