bin/synchb.py
changeset 3 11d3954df52a
parent 0 16d8024aca5e
child 5 627c4a0fd0e7
--- a/bin/synchb.py	Fri May 14 16:09:54 2010 +0300
+++ b/bin/synchb.py	Thu May 27 13:10:59 2010 +0300
@@ -36,7 +36,8 @@
 # Globals
 # ============================================================================
 VERBOSE = False
-EXCLUDE = ["hbplugins", "hbservers", "3rdparty", "internal", "tsrc", "debug", "release"]
+EXCLUDE = ["hbapps", "hbplugins", "hbservers", "hbtools", "3rdparty", 
+           "internal", "tsrc", "debug", "release", "bwins", "eabi"]
 COLLECTIONS = {"hbcore": "HbCore",
                "hbfeedback": "HbFeedback",
                "hbinput": "HbInput",
@@ -87,8 +88,24 @@
     except IOError, e:
         print(e)
 
-def write_header(filepath, include):
-    write_file(filepath, "#include \"%s\"\n" % include)
+def include_directive(header):
+    return "#include \"%s\"\n" % header
+
+def write_header(header, include, path):
+    filename = os.path.basename(header)
+    filepath = os.path.join(path, filename)
+    relpath = os.path.relpath(header, path).replace("\\", "/")
+    skip = False
+    if os.path.exists(filepath):
+        directive = include_directive(include)
+        oldsize = os.path.getsize(filepath)
+        newsize = len(directive)
+        if oldsize == newsize and directive == read_file(filepath):
+            skip = True
+    if not skip:
+        if VERBOSE:
+            print("INFO:\t ==> %s" % os.path.basename(filepath))
+        write_file(filepath, include_directive(include))
 
 # ============================================================================
 # Component
@@ -96,8 +113,8 @@
 class Component:
     def __init__(self, name):
         self.name = name
-        self.headers = list()
-        self.privates = list()
+        self.headers = []
+        self.privates = []
 
     def read(self, path):
         entries = os.listdir(path)
@@ -114,28 +131,24 @@
                     self.headers.append(entrypath)
 
     def write(self, path):
+        written = []
         if len(self.headers) > 0:
             self._makedirs(path)
-            self._write(path, self.headers, True)
+            written += self._write(path, self.headers, True)
 
         if len(self.privates) > 0:
             privpath = os.path.join(path, "private")
             self._makedirs(privpath)
-            self._write(privpath, self.privates, False)
+            written += self._write(privpath, self.privates, False)
+        return written
 
     def _write(self, path, headers, convenience):
-        global VERBOSE
-        if VERBOSE:
-            print("INFO: Writing headers to '%s'" % path)
+        written = []
         for header in headers:
-            filename = os.path.basename(header)
-            filepath = os.path.join(path, filename)
-            relpath = os.path.relpath(header, path)
-            if VERBOSE:
-                print("INFO:\t ==> %s" % os.path.basename(filepath))
-            write_header(filepath, relpath.replace("\\", "/"))
+            write_header(header, header, path)
+            written.append(os.path.join(path, os.path.basename(header)))
             if convenience:
-                classes = list()
+                classes = []
                 content = read_file(header)
                 for match in re.finditer("(?:class|namespace)\s+(?:HB_[^_]+_EXPORT\s+)?(Hb\w*)(\s*;)?", content):
                     if not match.group(2):
@@ -143,14 +156,12 @@
                 for match in re.finditer("#pragma hb_header\((\w+)\)", content):
                     classes.append(match.group(1))
                 for cls in classes:
-                    filepath = os.path.join(path, cls)
-                    write_header(filepath, filename)
+                    write_header(cls, os.path.basename(header), path)
+                    written.append(os.path.join(path, cls))
+        return written
 
     def _makedirs(self, path):
-        global VERBOSE
         if not os.path.exists(path):
-            if VERBOSE:
-                print("INFO: Creating include dir '%s'" % path)
             os.makedirs(path)
 
 # ============================================================================
@@ -171,25 +182,55 @@
                 self.components.append(component)
 
     def write(self, path):
-        global COLLECTIONS
+        global COLLECTIONS, VERBOSE
+        path = os.path.join(os.path.abspath(path), self.name)
+        if VERBOSE:
+            print("INFO: Writing headers to '%s'..." % path)
+        # there's no set in python 2.3 so use a list
+        leftovers = []
+        for root, dirs, files in os.walk(path):
+            for file in files:
+                leftovers.append(os.path.abspath(os.path.join(root, file)))
+
         # include/hbcore
-        includes = list()
-        path = os.path.join(path, self.name)
+        includes = []
+        written = []
         for component in self.components:
-            component.write(path)
+            written += component.write(path)
             for header in component.headers:
                 includes.append("#include \"%s\"\n" % os.path.basename(header))
+
         if self.name in COLLECTIONS:
-            write_file(os.path.join(path, self.name + ".h"), "".join(includes))
-            write_header(os.path.join(path, COLLECTIONS[self.name]), self.name + ".h")
+            collectionheader = os.path.join(path, self.name + ".h")
+            write_file(collectionheader, "".join(includes))
+            written.append(collectionheader)
+            if collectionheader in leftovers:
+                leftovers.remove(collectionheader)
+            convenienceheader = os.path.join(path, COLLECTIONS[self.name])
+            write_file(convenienceheader, include_directive(self.name + ".h"))
+            written.append(convenienceheader)
+            if convenienceheader in leftovers:
+                leftovers.remove(convenienceheader)
+
+        for filepath in written:
+            filepath = os.path.abspath(filepath)
+            if filepath in leftovers:
+                leftovers.remove(filepath)
+
+        if VERBOSE and len(leftovers) > 0:
+            print("INFO: Removing obsolete headers from '%s'..." % path)
+        for leftover in leftovers:
+            if VERBOSE:
+                print("INFO:\t ==> %s" % leftover) # os.path.basename(leftover))
+            os.remove(leftover)
 
 # ============================================================================
 # Package
 # ============================================================================
 class Package:
     def __init__(self, name):
-        self.path = name
-        self.collections = list()
+        self.name = name
+        self.collections = []
 
     def read(self, path):
         global EXCLUDE
@@ -228,11 +269,6 @@
     if not os.path.basename(os.path.normpath(options.outputdir)) == "include":
         options.outputdir = os.path.join(options.outputdir, "include")
 
-    if os.path.exists(options.outputdir):
-        if VERBOSE:
-            print("INFO: Removing include dir '%s'" % options.outputdir)
-        shutil.rmtree(options.outputdir, ignore_errors=True)
-
     package = Package("hb")
     package.read(options.inputdir)
     package.write(options.outputdir)