Port hb-manifest-read to Python
authorBehdad Esfahbod <behdad@behdad.org>
Sat, 21 Jan 2012 02:16:34 +0000 (21:16 -0500)
committerBehdad Esfahbod <behdad@behdad.org>
Sat, 21 Jan 2012 02:16:34 +0000 (21:16 -0500)
test/shaping/hb-diff-filter-failures
test/shaping/hb-manifest-read
test/shaping/hb_test_tools.py

index 663c9ec..5b00eae 100755 (executable)
@@ -2,4 +2,4 @@
 
 from hb_test_tools import *
 
-UtilMains.process_multiple_files (DiffFilters.filter_failures)
+UtilMains.filter_multiple_strings_or_stdin (Unicode.decode, "UNICODE_STRING")
index 46c6eb5..bfca2c0 100755 (executable)
@@ -1,36 +1,5 @@
-#!/bin/bash
+#!/usr/bin/python
 
-if test $# = 0; then
-       echo "Usage: $0 DIR..."
-       exit 1
-fi
+from hb_test_tools import *
 
-strict=true
-
-recurse () {
-       if ! test -e "$1"; then
-               if $strict; then
-                       echo "$0: $1 does not exist" 1>&2
-                       exit 1
-               fi
-       fi
-       if test -d "$1"; then
-               if test -f "$1/MANIFEST"; then
-                       cat "$1/MANIFEST" | while read f; do
-                               recurse "$1/$f"
-                       done
-               else
-                       if $strict; then
-                               echo "$0: $1/MANIFEST does not exist" 1>&2
-                               exit 1
-                       fi
-               fi
-       else
-               echo "$1"
-       fi
-}
-
-for root in "$@"; do
-       root=${root%/}
-       recurse "$root";
-done
+UtilMains.process_multiple_args (Manifest.print_to_stdout, mnemonic="DIR")
index 18217d7..471502f 100644 (file)
@@ -106,23 +106,33 @@ class ShapeFilters:
 class UtilMains:
 
        @staticmethod
-       def process_multiple_files (callback):
+       def process_multiple_files (callback, mnemonic = "FILE"):
 
                if len (sys.argv) == 1:
-                       print "Usage: %s FILE..." % sys.argv[0]
+                       print "Usage: %s %s..." % (sys.argv[0], mnemonic)
                        sys.exit (1)
 
                for s in sys.argv[1:]:
                        callback (FileHelpers.open_file_or_stdin (s))
 
        @staticmethod
-       def filter_multiple_strings_or_stdin (callback, string_mnemonic, \
+       def process_multiple_args (callback, mnemonic):
+
+               if len (sys.argv) == 1:
+                       print "Usage: %s %s..." % (sys.argv[0], mnemonic)
+                       sys.exit (1)
+
+               for s in sys.argv[1:]:
+                       callback (s)
+
+       @staticmethod
+       def filter_multiple_strings_or_stdin (callback, mnemonic, \
                                              separator = " ", \
                                              concat_separator = False):
 
                if len (sys.argv) == 1 or ('--stdin' in sys.argv and len (sys.argv) != 2):
                        print "Usage:\n  %s %s...\nor:\n  %s --stdin" \
-                             % (sys.argv[0], string_mnemonic, sys.argv[0])
+                             % (sys.argv[0], mnemonic, sys.argv[0])
                        sys.exit (1)
 
                if '--stdin' in sys.argv:
@@ -189,7 +199,7 @@ class Unicode:
                return u' + '.join (Unicode.pretty_name (x) for x in s).encode ('utf-8')
 
 
-class FileHelprs:
+class FileHelpers:
 
        @staticmethod
        def open_file_or_stdin (f):
@@ -197,5 +207,34 @@ class FileHelprs:
                        return sys.stdin
                return file (f)
 
+
+class Manifest:
+
+       @staticmethod
+       def print_to_stdout (s, strict = True):
+               if not os.path.exists (s):
+                       if strict:
+                               print >> sys.stderr, "%s: %s does not exist" (sys.argv[0], s)
+                               sys.exit (1)
+                       return
+
+               if os.path.isdir (s):
+
+                       if s[-1] != '/':
+                               s += "/"
+
+                       try:
+                               m = file (s + "/MANIFEST")
+                               items = [x.strip () for x in m.readlines ()]
+                               for f in items:
+                                       Manifest.print_to_stdout (s + f)
+                       except IOError:
+                               if strict:
+                                       print >> sys.stderr, "%s: %s does not exist" (sys.argv[0], s + "/MANIFEST")
+                                       sys.exit (1)
+                               return
+               else:
+                       print s
+
 if __name__ == '__main__':
        pass