[gobject-introspection/wip/transformer] Add support for --reparse-validate



commit 10553006a9323f1f21c8b6cc30593dd2ac339d50
Author: Colin Walters <walters verbum org>
Date:   Fri Jul 23 04:09:10 2010 -0400

    Add support for --reparse-validate

 gir/Makefile.am          |   12 ++++++------
 giscanner/scannermain.py |   30 +++++++++++++++++++++++-------
 giscanner/utils.py       |   13 +++++++++++++
 3 files changed, 42 insertions(+), 13 deletions(-)
---
diff --git a/gir/Makefile.am b/gir/Makefile.am
index 039f34b..f938e98 100644
--- a/gir/Makefile.am
+++ b/gir/Makefile.am
@@ -31,7 +31,7 @@ GLIB_LIBRARY=glib-2.0
 endif
 
 GLib_2_0_gir_LIBS = $(GLIB_LIBRARY)
-GLib_2_0_gir_SCANNERFLAGS = --warn-all --strip-prefix=G --c-include="glib.h"
+GLib_2_0_gir_SCANNERFLAGS = --warn-all --reparse-validate --strip-prefix=G --c-include="glib.h"
 GLib_2_0_gir_PACKAGES = glib-2.0
 GLib_2_0_gir_CFLAGS = \
             -I$(GLIB_INCLUDEDIR) \
@@ -61,7 +61,7 @@ endif
 GObject-2.0.gir: GLib-2.0.gir
 
 GObject_2_0_gir_LIBS = $(GOBJECT_LIBRARY)
-GObject_2_0_gir_SCANNERFLAGS = --warn-all --strip-prefix=G --c-include="glib-object.h" --add-include-path=.
+GObject_2_0_gir_SCANNERFLAGS = --warn-all --reparse-validate --strip-prefix=G --c-include="glib-object.h" --add-include-path=.
 GObject_2_0_gir_PACKAGES = gobject-2.0
 GObject_2_0_gir_INCLUDES = GLib-2.0
 GObject_2_0_gir_CFLAGS = \
@@ -87,7 +87,7 @@ endif
 GModule-2.0.gir: GLib-2.0.gir
 
 GModule_2_0_gir_LIBS = $(GMODULE_LIBRARY)
-GModule_2_0_gir_SCANNERFLAGS = --warn-all --strip-prefix=G --c-include="gmodule.h" --add-include-path=.
+GModule_2_0_gir_SCANNERFLAGS = --warn-all --reparse-validate --strip-prefix=G --c-include="gmodule.h" --add-include-path=.
 GModule_2_0_gir_PACKAGES = gmodule-2.0
 GModule_2_0_gir_INCLUDES = GLib-2.0
 GModule_2_0_gir_CFLAGS = \
@@ -118,7 +118,7 @@ endif
 Gio-2.0.gir: GObject-2.0.gir
 
 Gio_2_0_gir_LIBS = $(GIO_LIBRARY)
-Gio_2_0_gir_SCANNERFLAGS = --warn-all --strip-prefix=G --c-include="gio/gio.h" --add-include-path=.
+Gio_2_0_gir_SCANNERFLAGS = --warn-all --reparse-validate --strip-prefix=G --c-include="gio/gio.h" --add-include-path=.
 Gio_2_0_gir_PACKAGES = gio-2.0 $(GIO_UNIX_PACKAGES)
 Gio_2_0_gir_INCLUDES = GObject-2.0
 Gio_2_0_gir_CFLAGS = \
@@ -179,7 +179,7 @@ if BUILD_TESTS
 Everything-1.0.gir: Gio-2.0.gir libgirepository-everything-1.0.la
 
 Everything_1_0_gir_LIBS = libgirepository-everything-1.0.la
-Everything_1_0_gir_SCANNERFLAGS = --warn-all --warn-error
+Everything_1_0_gir_SCANNERFLAGS = --warn-all --warn-error --reparse-validate
 Everything_1_0_gir_PACKAGES = gobject-2.0 cairo gio-2.0
 Everything_1_0_gir_INCLUDES = GObject-2.0 cairo-1.0 Gio-2.0
 Everything_1_0_gir_FILES = everything.h everything.c
@@ -209,7 +209,7 @@ GIMarshallingTests_1_0_gir_LIBS = libgirepository-gimarshallingtests-1.0.la
 GIMarshallingTests_1_0_gir_PACKAGES = gobject-2.0
 GIMarshallingTests_1_0_gir_INCLUDES = GObject-2.0
 GIMarshallingTests_1_0_gir_FILES = gimarshallingtests.h gimarshallingtests.c
-GIMarshallingTests_1_0_gir_SCANNERFLAGS = --warn-all --warn-error
+GIMarshallingTests_1_0_gir_SCANNERFLAGS = --warn-all --warn-error  --reparse-validate
 
 INTROSPECTION_GIRS += GIMarshallingTests-$(TYPELIB_VERSION).gir
 EXPECTEDGIRS += GIMarshallingTests-$(TYPELIB_VERSION)-expected.gir
diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
index 3b33753..2afbff6 100644
--- a/giscanner/scannermain.py
+++ b/giscanner/scannermain.py
@@ -21,6 +21,7 @@
 #
 
 import subprocess
+import tempfile
 import optparse
 import os
 import sys
@@ -36,6 +37,7 @@ from giscanner.shlibs import resolve_shlibs
 from giscanner.transformer import Transformer
 from giscanner.girparser import GIRParser
 from giscanner.girwriter import GIRWriter
+from giscanner.utils import files_are_identical
 
 def _get_option_parser():
     parser = optparse.OptionParser('%prog [options] sources')
@@ -54,6 +56,9 @@ def _get_option_parser():
     parser.add_option('', "--passthrough-gir",
                       action="store", dest="passthrough_gir", default=None,
                       help="Parse and re-output the specified GIR")
+    parser.add_option('', "--reparse-validate",
+                      action="store_true", dest="reparse_validate_gir", default=False,
+                      help="After generating the GIR, re-parse it to ensure validity")
     parser.add_option("", "--add-include-path",
                       action="append", dest="include_paths", default=[],
                       help="include paths for other GIR files")
@@ -135,7 +140,7 @@ def _get_option_parser():
 def _error(msg):
     raise SystemExit('ERROR: %s' % (msg, ))
 
-def passthrough_gir(path):
+def passthrough_gir(path, f):
     parser = GIRParser()
     parser.parse(path)
 
@@ -144,8 +149,7 @@ def passthrough_gir(path):
                        parser.get_includes(),
                        parser.get_pkgconfig_packages(),
                        parser.get_c_includes())
-    sys.stdout.write(writer.get_xml())
-    sys.exit(0)
+    f.write(writer.get_xml())
 
 def validate(assertions, path):
     from xml.etree.cElementTree import parse
@@ -200,7 +204,7 @@ def scanner_main(args):
     (options, args) = parser.parse_args(args)
 
     if options.passthrough_gir:
-        return passthrough_gir(options.passthrough_gir)
+        passthrough_gir(options.passthrough_gir, sys.stdout)
 
     if len(args) <= 1:
         _error('Need at least one filename')
@@ -307,9 +311,21 @@ def scanner_main(args):
                     exported_packages, options.c_includes)
     data = writer.get_xml()
     if options.output:
-        fd = open(options.output, "w")
-        fd.write(data)
+        tempdir = os.path.dirname(options.output) or os.getcwd()
+        (tempfd, main_temppath) = tempfile.mkstemp(suffix='.gir', dir=tempdir)
+        f = os.fdopen(tempfd, 'w')
+        f.write(data)
+        f.close()
+        if options.reparse_validate_gir:
+            (tempfd, tempgir_path) = tempfile.mkstemp(suffix='.gir', dir=tempdir)
+            temp_f = os.fdopen(tempfd, 'w')
+            passthrough_gir(main_temppath, temp_f)
+            temp_f.close()
+            if not files_are_identical(main_temppath, tempgir_path):
+                raise SystemExit("Failed to re-parse .gir file; scanned=%r passthrough=%r" % (main_temppath, tempgir_path))
+            os.unlink(tempgir_path)
+        os.rename(main_temppath, options.output)
     else:
-        print data
+        sys.stdout.write(data)
 
     return 0
diff --git a/giscanner/utils.py b/giscanner/utils.py
index 1bd23fc..dba958e 100644
--- a/giscanner/utils.py
+++ b/giscanner/utils.py
@@ -102,3 +102,16 @@ def get_libtool_command(options):
         return None
 
     return ['libtool']
+
+
+def files_are_identical(path1, path2):
+    f1 = open(path1)
+    f2 = open(path2)
+    buf1 = f1.read(8192)
+    buf2 = f2.read(8192)
+    while buf1 == buf2 and buf1 != '':
+        buf1 = f1.read(8192)
+        buf2 = f2.read(8192)
+    f1.close()
+    f2.close()
+    return buf1 == buf2



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]