bin/synchb.py
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 5 627c4a0fd0e7
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    34 
    34 
    35 # ============================================================================
    35 # ============================================================================
    36 # Globals
    36 # Globals
    37 # ============================================================================
    37 # ============================================================================
    38 VERBOSE = False
    38 VERBOSE = False
    39 EXCLUDE = ["hbplugins", "hbservers", "3rdparty", "internal", "tsrc", "debug", "release"]
    39 EXCLUDE = ["hbapps", "hbplugins", "hbservers", "hbtools", "3rdparty", 
       
    40            "internal", "tsrc", "debug", "release", "bwins", "eabi"]
    40 COLLECTIONS = {"hbcore": "HbCore",
    41 COLLECTIONS = {"hbcore": "HbCore",
    41                "hbfeedback": "HbFeedback",
    42                "hbfeedback": "HbFeedback",
    42                "hbinput": "HbInput",
    43                "hbinput": "HbInput",
    43                "hbutils": "HbUtils",
    44                "hbutils": "HbUtils",
    44                "hbwidgets": "HbWidgets"}
    45                "hbwidgets": "HbWidgets"}
    85         file.write(content)
    86         file.write(content)
    86         file.close()
    87         file.close()
    87     except IOError, e:
    88     except IOError, e:
    88         print(e)
    89         print(e)
    89 
    90 
    90 def write_header(filepath, include):
    91 def include_directive(header):
    91     write_file(filepath, "#include \"%s\"\n" % include)
    92     return "#include \"%s\"\n" % header.replace("\\", "/")
       
    93 
       
    94 def write_header(header, include, path):
       
    95     filename = os.path.basename(header)
       
    96     filepath = os.path.join(path, filename)
       
    97     skip = False
       
    98     if os.path.exists(filepath):
       
    99         directive = include_directive(include)
       
   100         oldsize = os.path.getsize(filepath)
       
   101         newsize = len(directive)
       
   102         if oldsize == newsize and directive == read_file(filepath):
       
   103             skip = True
       
   104     if not skip:
       
   105         if VERBOSE:
       
   106             print("INFO:\t ==> %s" % os.path.basename(filepath))
       
   107         write_file(filepath, include_directive(include))
    92 
   108 
    93 # ============================================================================
   109 # ============================================================================
    94 # Component
   110 # Component
    95 # ============================================================================
   111 # ============================================================================
    96 class Component:
   112 class Component:
    97     def __init__(self, name):
   113     def __init__(self, name):
    98         self.name = name
   114         self.name = name
    99         self.headers = list()
   115         self.headers = []
   100         self.privates = list()
   116         self.privates = []
       
   117         self.restricted = []
   101 
   118 
   102     def read(self, path):
   119     def read(self, path):
   103         entries = os.listdir(path)
   120         entries = os.listdir(path)
   104         for entry in entries:
   121         for entry in entries:
   105             entrypath = os.path.join(path, entry)
   122             entrypath = os.path.join(path, entry)
   106             if os.path.isdir(entrypath):
   123             if os.path.isdir(entrypath):
   107                 self.read(entrypath)
   124                 self.read(entrypath)
   108             elif os.path.isfile(entrypath):
   125             elif os.path.isfile(entrypath):
   109                 if entry.endswith("_p_p.h"):
   126                 if re.match(entry, ".*?_[pr]_[pr]\.h"):
   110                     continue
   127                     continue
   111                 elif entry.endswith("_p.h"):
   128                 elif entry.endswith("_p.h"):
   112                     self.privates.append(entrypath)
   129                     self.privates.append(entrypath)
       
   130                 elif entry.endswith("_r.h"):
       
   131                     self.restricted.append(entrypath)
   113                 elif entry.endswith(".h"):
   132                 elif entry.endswith(".h"):
   114                     self.headers.append(entrypath)
   133                     self.headers.append(entrypath)
   115 
   134 
   116     def write(self, path):
   135     def write(self, path):
       
   136         written = []
   117         if len(self.headers) > 0:
   137         if len(self.headers) > 0:
   118             self._makedirs(path)
   138             self._makedirs(path)
   119             self._write(path, self.headers, True)
   139             written += self._write(path, self.headers, True)
       
   140 
       
   141         if len(self.restricted) > 0:
       
   142             restpath = os.path.join(path, "restricted")
       
   143             self._makedirs(restpath)
       
   144             written += self._write(restpath, self.restricted, True)
   120 
   145 
   121         if len(self.privates) > 0:
   146         if len(self.privates) > 0:
   122             privpath = os.path.join(path, "private")
   147             privpath = os.path.join(path, "private")
   123             self._makedirs(privpath)
   148             self._makedirs(privpath)
   124             self._write(privpath, self.privates, False)
   149             written += self._write(privpath, self.privates, False)
       
   150         return written
   125 
   151 
   126     def _write(self, path, headers, convenience):
   152     def _write(self, path, headers, convenience):
   127         global VERBOSE
   153         written = []
   128         if VERBOSE:
       
   129             print("INFO: Writing headers to '%s'" % path)
       
   130         for header in headers:
   154         for header in headers:
   131             filename = os.path.basename(header)
   155             write_header(header, os.path.relpath(header, path), path)
   132             filepath = os.path.join(path, filename)
   156             written.append(os.path.join(path, os.path.basename(header)))
   133             relpath = os.path.relpath(header, path)
       
   134             if VERBOSE:
       
   135                 print("INFO:\t ==> %s" % os.path.basename(filepath))
       
   136             write_header(filepath, relpath.replace("\\", "/"))
       
   137             if convenience:
   157             if convenience:
   138                 classes = list()
   158                 classes = []
   139                 content = read_file(header)
   159                 content = read_file(header)
   140                 for match in re.finditer("(?:class|namespace)\s+(?:HB_[^_]+_EXPORT\s+)?(Hb\w*)(\s*;)?", content):
   160                 for match in re.finditer("(?:class|namespace)\s+(?:HB_[^_]+(?:_RESTRICTED)?_EXPORT\s+)?(Hb\w*)(\s*;)?", content):
   141                     if not match.group(2):
   161                     if not match.group(2):
   142                         classes.append(match.group(1))
   162                         classes.append(match.group(1))
   143                 for match in re.finditer("#pragma hb_header\((\w+)\)", content):
   163                 for match in re.finditer("#pragma hb_header\((\w+)\)", content):
   144                     classes.append(match.group(1))
   164                     classes.append(match.group(1))
   145                 for cls in classes:
   165                 for cls in classes:
   146                     filepath = os.path.join(path, cls)
   166                     write_header(cls, os.path.basename(header), path)
   147                     write_header(filepath, filename)
   167                     written.append(os.path.join(path, cls))
       
   168         return written
   148 
   169 
   149     def _makedirs(self, path):
   170     def _makedirs(self, path):
   150         global VERBOSE
       
   151         if not os.path.exists(path):
   171         if not os.path.exists(path):
   152             if VERBOSE:
       
   153                 print("INFO: Creating include dir '%s'" % path)
       
   154             os.makedirs(path)
   172             os.makedirs(path)
   155 
   173 
   156 # ============================================================================
   174 # ============================================================================
   157 # Collection
   175 # Collection
   158 # ============================================================================
   176 # ============================================================================
   169                 component = Component(entry)
   187                 component = Component(entry)
   170                 component.read(entrypath)
   188                 component.read(entrypath)
   171                 self.components.append(component)
   189                 self.components.append(component)
   172 
   190 
   173     def write(self, path):
   191     def write(self, path):
   174         global COLLECTIONS
   192         global COLLECTIONS, VERBOSE
       
   193         path = os.path.join(os.path.abspath(path), self.name)
       
   194         if VERBOSE:
       
   195             print("INFO: Writing headers to '%s'..." % path)
       
   196         # there's no set in python 2.3 so use a list
       
   197         leftovers = []
       
   198         for root, dirs, files in os.walk(path):
       
   199             for file in files:
       
   200                 leftovers.append(os.path.abspath(os.path.join(root, file)))
       
   201 
   175         # include/hbcore
   202         # include/hbcore
   176         includes = list()
   203         includes = []
   177         path = os.path.join(path, self.name)
   204         written = []
   178         for component in self.components:
   205         for component in self.components:
   179             component.write(path)
   206             written += component.write(path)
   180             for header in component.headers:
   207             for header in component.headers:
   181                 includes.append("#include \"%s\"\n" % os.path.basename(header))
   208                 includes.append("#include \"%s\"\n" % os.path.basename(header))
       
   209 
   182         if self.name in COLLECTIONS:
   210         if self.name in COLLECTIONS:
   183             write_file(os.path.join(path, self.name + ".h"), "".join(includes))
   211             collectionheader = os.path.join(path, self.name + ".h")
   184             write_header(os.path.join(path, COLLECTIONS[self.name]), self.name + ".h")
   212             write_file(collectionheader, "".join(includes))
       
   213             written.append(collectionheader)
       
   214             if collectionheader in leftovers:
       
   215                 leftovers.remove(collectionheader)
       
   216             convenienceheader = os.path.join(path, COLLECTIONS[self.name])
       
   217             write_file(convenienceheader, include_directive(self.name + ".h"))
       
   218             written.append(convenienceheader)
       
   219             if convenienceheader in leftovers:
       
   220                 leftovers.remove(convenienceheader)
       
   221 
       
   222         for filepath in written:
       
   223             filepath = os.path.abspath(filepath)
       
   224             if filepath in leftovers:
       
   225                 leftovers.remove(filepath)
       
   226 
       
   227         if VERBOSE and len(leftovers) > 0:
       
   228             print("INFO: Removing obsolete headers from '%s'..." % path)
       
   229         for leftover in leftovers:
       
   230             if VERBOSE:
       
   231                 print("INFO:\t ==> %s" % leftover) # os.path.basename(leftover))
       
   232             os.remove(leftover)
   185 
   233 
   186 # ============================================================================
   234 # ============================================================================
   187 # Package
   235 # Package
   188 # ============================================================================
   236 # ============================================================================
   189 class Package:
   237 class Package:
   190     def __init__(self, name):
   238     def __init__(self, name):
   191         self.path = name
   239         self.name = name
   192         self.collections = list()
   240         self.collections = []
   193 
   241 
   194     def read(self, path):
   242     def read(self, path):
   195         global EXCLUDE
   243         global EXCLUDE
   196         for entry in os.listdir(path):
   244         for entry in os.listdir(path):
   197             # hbcore, hbwidgets, hbutils...
   245             # hbcore, hbwidgets, hbutils...
   226     if not options.outputdir:
   274     if not options.outputdir:
   227         options.outputdir = os.getcwd()
   275         options.outputdir = os.getcwd()
   228     if not os.path.basename(os.path.normpath(options.outputdir)) == "include":
   276     if not os.path.basename(os.path.normpath(options.outputdir)) == "include":
   229         options.outputdir = os.path.join(options.outputdir, "include")
   277         options.outputdir = os.path.join(options.outputdir, "include")
   230 
   278 
   231     if os.path.exists(options.outputdir):
       
   232         if VERBOSE:
       
   233             print("INFO: Removing include dir '%s'" % options.outputdir)
       
   234         shutil.rmtree(options.outputdir, ignore_errors=True)
       
   235 
       
   236     package = Package("hb")
   279     package = Package("hb")
   237     package.read(options.inputdir)
   280     package.read(options.inputdir)
   238     package.write(options.outputdir)
   281     package.write(options.outputdir)
   239 
   282 
   240 if __name__ == "__main__":
   283 if __name__ == "__main__":