Don't replace generated files if they haven't changed.
authorBen Turner <bent.mozilla@gmail.com>
Wed, 08 Feb 2012 22:13:40 -0800
changeset 86465 3d27529bc495956a38f878bb559b242bd540bd6e
parent 86464 979451d9d4167e15d2eb787458652e673877e394
child 86466 be5ffa75e6f2116461dd2f279c309a6153175876
push id100
push userbturner@mozilla.com
push dateThu, 09 Feb 2012 06:13:57 +0000
milestone13.0a1
Don't replace generated files if they haven't changed.
dom/bindings/BindingGen.py
dom/bindings/Codegen.py
dom/bindings/GlobalGen.py
--- a/dom/bindings/BindingGen.py
+++ b/dom/bindings/BindingGen.py
@@ -1,43 +1,39 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
 # You can obtain one at http://mozilla.org/MPL/2.0/.
 
 import os
 import cPickle
 import WebIDL
 from Configuration import *
-from Codegen import CGBindingRoot
+from Codegen import CGBindingRoot, replaceFileIfChanged
 
 def generate_binding_header(config, outputprefix, webidlfile):
     """
     |config| Is the configuration object.
     |outputprefix| is a prefix to use for the header guards and filename.
     """
 
     filename = outputprefix + ".h"
-    print "Generating binding header: %s" % (filename)
-    f = open(filename, 'w')
     root = CGBindingRoot(config, outputprefix, webidlfile)
-    f.write(root.declare())
-    f.close()
+    if replaceFileIfChanged(filename, root.declare()):
+        print "Generating binding header: %s" % (filename)
 
 def generate_binding_cpp(config, outputprefix, webidlfile):
     """
     |config| Is the configuration object.
     |outputprefix| is a prefix to use for the header guards and filename.
     """
 
     filename = outputprefix + ".cpp"
-    print "Generating binding implementation: %s" % (filename)
-    f = open(filename, 'w')
     root = CGBindingRoot(config, outputprefix, webidlfile)
-    f.write(root.define())
-    f.close()
+    if replaceFileIfChanged(filename, root.define()):
+        print "Generating binding implementation: %s" % (filename)
 
 def main():
 
     # Parse arguments.
     from optparse import OptionParser
     usagestring = "usage: %prog [header|cpp] configFile outputPrefix webIDLFile"
     o = OptionParser(usage=usagestring)
     o.add_option("--verbose-errors", action='store_true', default=False,
--- a/dom/bindings/Codegen.py
+++ b/dom/bindings/Codegen.py
@@ -7,16 +7,36 @@
 import os
 import string
 
 from WebIDL import *
 
 FINALIZE_HOOK_NAME = 'Finalize'
 TRACE_HOOK_NAME = 'Trace'
 
+def replaceFileIfChanged(filename, newContents):
+    """
+    Read a copy of the old file, so that we don't touch it if it hasn't changed.
+    Returns True if the file was updated, false otherwise.
+    """
+    oldFileContents = ""
+    try:
+        oldFile = open(filename, 'rb')
+        oldFileContents = ''.join(oldFile.readlines())
+        oldFile.close()
+    except:
+        pass
+
+    if newContents == oldFileContents:
+        return False
+
+    f = open(filename, 'wb')
+    f.write(newContents)
+    f.close()
+
 class CGThing():
     """
     Abstract base case for things that spit out code.
     """
     def __init__(self):
         pass # Nothing for now
     def declare(self):
         """Produce code for a header file."""
--- a/dom/bindings/GlobalGen.py
+++ b/dom/bindings/GlobalGen.py
@@ -5,41 +5,26 @@
 # We do one global pass over all the WebIDL to generate our prototype enum
 # and generate information for subsequent phases.
 
 import os
 import cStringIO
 import WebIDL
 import cPickle
 from Configuration import *
-from Codegen import CGGlobalRoot
+from Codegen import CGGlobalRoot, replaceFileIfChanged
 
 def generate_prototype_list(config, outputprefix):
 
-    # Read a copy of the old file, so that we don't touch it if it hasn't changed.
     filename = outputprefix + '.h'
-    oldFileContents = ""
-    try:
-        oldFile = open(filename, 'r')
-        oldFileContents = ''.join(oldFile.readlines())
-        oldFile.close()
-    except:
-        pass
-
     root = CGGlobalRoot(config, outputprefix)
-    newFileContents = root.prototypeList_h()
-
-    if newFileContents == oldFileContents:
+    if replaceFileIfChanged(filename, root.prototypeList_h()):
+        print "Generating prototype list: %s" % (filename)
+    else:
         print "Prototype list hasn't changed - not touching %s" % (filename)
-        return
-
-    print "Generating prototype list: %s" % (filename)
-    f = open(filename, 'w')
-    f.write(newFileContents)
-    f.close()
 
 def main():
 
     # Parse arguments.
     from optparse import OptionParser
     usageString = "usage: %prog [options] webidldir [files]"
     o = OptionParser(usage=usageString)
     o.add_option("--verbose-errors", action='store_true', default=False,