Fix compileall.py so that it fails on SyntaxErrors

The changes cause compilation failures in any file in the Python
installation lib directory to cause the install to fail.  It looks
like compileall.py intended to behave this way, but a change to
py_compile.py and a separate bug defeated it.

Fixes SF bug #412436

This change affects the test suite, which contains several files that
contain intentional errors.  The solution is to extend compileall.py
with the ability to skip compilation of selected files.

NB compileall.py is changed so that compile_dir() returns success only
if all recursive calls to compile_dir() also check success.
diff --git a/Lib/compileall.py b/Lib/compileall.py
index 93ec7ad..cb4a3e9 100644
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -19,7 +19,7 @@
 
 __all__ = ["compile_dir","compile_path"]
 
-def compile_dir(dir, maxlevels=10, ddir=None, force=0):
+def compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None):
     """Byte-compile all modules in the given directory tree.
 
     Arguments (only dir is required):
@@ -45,6 +45,10 @@
             dfile = os.path.join(ddir, name)
         else:
             dfile = None
+        if rx:
+            mo = rx.search(fullname)
+            if mo:
+                continue
         if os.path.isfile(fullname):
             head, tail = name[:-3], name[-3:]
             if tail == '.py':
@@ -55,21 +59,26 @@
                 if (ctime > ftime) and not force: continue
                 print 'Compiling', fullname, '...'
                 try:
-                    py_compile.compile(fullname, None, dfile)
+                    ok = py_compile.compile(fullname, None, dfile)
                 except KeyboardInterrupt:
                     raise KeyboardInterrupt
                 except:
+                    # XXX py_compile catches SyntaxErrors
                     if type(sys.exc_type) == type(''):
                         exc_type_name = sys.exc_type
                     else: exc_type_name = sys.exc_type.__name__
                     print 'Sorry:', exc_type_name + ':',
                     print sys.exc_value
                     success = 0
+                else:
+                    if ok == 0:
+                        success = 0
         elif maxlevels > 0 and \
              name != os.curdir and name != os.pardir and \
              os.path.isdir(fullname) and \
              not os.path.islink(fullname):
-            compile_dir(fullname, maxlevels - 1, dfile, force)
+            if not compile_dir(fullname, maxlevels - 1, dfile, force, rx):
+                success = 0
     return success
 
 def compile_path(skip_curdir=1, maxlevels=0, force=0):
@@ -94,22 +103,29 @@
     """Script main program."""
     import getopt
     try:
-        opts, args = getopt.getopt(sys.argv[1:], 'lfd:')
+        opts, args = getopt.getopt(sys.argv[1:], 'lfd:x:')
     except getopt.error, msg:
         print msg
-        print "usage: compileall [-l] [-f] [-d destdir] [directory ...]"
+        print "usage: python compileall.py [-l] [-f] [-d destdir] " \
+              "[-s regexp] [directory ...]"
         print "-l: don't recurse down"
         print "-f: force rebuild even if timestamps are up-to-date"
         print "-d destdir: purported directory name for error messages"
-        print "if no directory arguments, -l sys.path is assumed"
+        print "   if no directory arguments, -l sys.path is assumed"
+        print "-x regexp: skip files matching the regular expression regexp"
+        print "   the regexp is search for in the full path of the file"
         sys.exit(2)
     maxlevels = 10
     ddir = None
     force = 0
+    rx = None
     for o, a in opts:
         if o == '-l': maxlevels = 0
         if o == '-d': ddir = a
         if o == '-f': force = 1
+        if o == '-x':
+            import re
+            rx = re.compile(a)
     if ddir:
         if len(args) != 1:
             print "-d destdir require exactly one directory argument"
@@ -118,7 +134,8 @@
     try:
         if args:
             for dir in args:
-                success = success and compile_dir(dir, maxlevels, ddir, force)
+                if not compile_dir(dir, maxlevels, ddir, force, rx):
+                    success = 0
         else:
             success = compile_path()
     except KeyboardInterrupt:
@@ -127,4 +144,5 @@
     return success
 
 if __name__ == '__main__':
-    sys.exit(not main())
+    exit_status = not main()
+    sys.exit(exit_status)