Changeset 492

Show
Ignore:
Timestamp:
Fri Jul 25 05:34:23 2008
Author:
htgoebel
Message:

Code cleanup for Build.py: unified check_guts, removed spaghetti code.

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/Build.py

    r490 r492  
    38 38 TUPLETYPE = type((None,))  
    39 39  
      40 # todo: use pkg_resources here  
    40 41 HOMEPATH = os.path.dirname(sys.argv[0])  
    41 42 SPECPATH = None  
     
    116 117     return os.path.abspath(os.path.normpath(apath))  
    117 118  
      119 #--- functons for checking guts ---  
      120  
      121 def _check_guts_eq(attr, old, new, last_build):  
      122     """  
      123     rebuild is required if values differ  
      124     """  
      125     if old != new:  
      126         print "building because %s changed" % attr  
      127         return True  
      128     return False  
      129  
      130 def _check_guts_toc_mtime(attr, old, toc, last_build, pyc=0):  
      131     """  
      132     rebuild is required if mtimes of files listed in old toc are newer  
      133     than ast_build  
      134  
      135     if pyc=1, check for .py files, too  
      136     """  
      137     for (nm, fnm, typ) in old:  
      138         if mtime(fnm) > last_build:  
      139             print "building because %s changed" % fnm  
      140             return True  
      141         elif pyc and mtime(fnm[:-1]) > last_build:  
      142             print "building because %s changed" % fnm[:-1]  
      143             return True  
      144     return False  
      145  
      146 def _check_guts_toc(attr, old, toc, last_build, pyc=0):  
      147     """  
      148     rebuild is required if either toc content changed if mtimes of  
      149     files listed in old toc are newer than ast_build  
      150  
      151     if pyc=1, check for .py files, too  
      152     """  
      153     return    _check_guts_eq       (attr, old, toc, last_build) \  
      154            or _check_guts_toc_mtime(attr, old, toc, last_build, pyc=pyc)  
      155  
      156 #--  
      157  
    118 158 class Target:  
    119 159     invcnum = 0  
     
    129 169             self.assemble()  
    130 170  
      171     GUTS = []  
      172  
      173     def check_guts(self, last_build):  
      174         pass  
      175  
      176     def get_guts(self, last_build, missing ='missing or bad'):  
      177         """  
      178         returns None if guts have changed  
      179         """  
      180         try:  
      181             data = _load_data(self.out)  
      182         except:  
      183             print "building because", os.path.basename(self.out), missing  
      184             return None  
      185  
      186         if len(data) != len(self.GUTS):  
      187             print "building because %s is bad" % outnm  
      188             return None  
      189         for i in range(len(self.GUTS)):  
      190             attr, func = self.GUTS[i]  
      191             if func is None:  
      192                 # no check for this value  
      193                 continue  
      194             if func(attr, data[i], getattr(self, attr), last_build):  
      195                 return None  
      196         return data  
      197  
      198  
    131 199 class Analysis(Target):  
    132 200     def __init__(self, scripts=None, pathex=None, hookspath=None, excludes=None):  
     
    147 215         self.zipfiles = TOC()  
    148 216         self.__postinit__()  
      217    
      218     GUTS = (('inputs',    _check_guts_eq),  
      219             ('pathex',    _check_guts_eq),  
      220             ('hookspath', _check_guts_eq),  
      221             ('excludes',  _check_guts_eq),  
      222             ('scripts',   _check_guts_toc_mtime),  
      223             ('pure',      lambda *args: apply(_check_guts_toc_mtime,  
      224                                               args, {'pyc': 1 }   )),  
      225             ('binaries',  _check_guts_toc_mtime),  
      226             ('zipfiles',  _check_guts_toc_mtime),  
      227             )  
      228    
    149 229     def check_guts(self, last_build):  
    150 230         outnm = os.path.basename(self.out)  
     
    156 236                 print "building because %s changed" % fnm  
    157 237                 return True  
    158           try:  
    159               inputs, pathex, hookspath, excludes, scripts, pure, binaries, zipfiles = _load_data(self.out)  
    160           except:  
    161               print "building because %s disappeared" % outnm  
    162               return True  
    163           if inputs != self.inputs:  
    164               print "building %s because inputs changed" % outnm  
    165               return True  
    166           if pathex != self.pathex:  
    167               print "building %s because pathex changed" % outnm  
    168               return True  
    169           if hookspath != self.hookspath:  
    170               print "building %s because hookspath changed" % outnm  
    171               return True  
    172           if excludes != self.excludes:  
    173               print "building %s because excludes changed" % outnm  
      238  
      239         data = Target.get_guts(self, last_build)  
      240         if not data:  
    174 241             return True  
    175           for (nm, fnm, typ) in scripts:  
    176               if mtime(fnm) > last_build:  
    177                   print "building because %s changed" % fnm  
    178                   return True  
    179           for (nm, fnm, typ) in pure:  
    180               if mtime(fnm) > last_build:  
    181                   print "building because %s changed" % fnm  
    182                   return True  
    183               elif mtime(fnm[:-1]) > last_build:  
    184                   print "building because %s changed" % fnm[:-1]  
    185                   return True  
    186           for (nm, fnm, typ) in binaries + zipfiles:  
    187               if mtime(fnm) > last_build:  
    188                   print "building because %s changed" % fnm  
    189                   return True  
      242         scripts, pure, binaries, zipfiles = data[-4:]  
    190 243         self.scripts = TOC(scripts)  
    191 244         self.pure = TOC(pure)  
    192 245         self.binaries = TOC(binaries)  
      246         self.zipfiles = TOC(zipfiles)  
    193 247         return False  
      248  
    194 249     def assemble(self):  
    195 250         print "running Analysis", os.path.basename(self.out)  
     
    325 380         self.dependencies = config['PYZ_dependencies']  
    326 381         self.__postinit__()  
      382  
      383     GUTS = (('name',   _check_guts_eq),  
      384             ('level',  _check_guts_eq),  
      385             ('toc',    _check_guts_toc), # todo: pyc=1  
      386             )  
      387  
    327 388     def check_guts(self, last_build):  
    328 389         outnm = os.path.basename(self.out)  
    329 390         if not os.path.exists(self.name):  
    330 391             print "rebuilding %s because %s is missing" % (outnm, os.path.basename(self.name))  
    331               return 1  
    332           try:  
    333               name, level, toc = _load_data(self.out)  
    334           except:  
    335               print "rebuilding %s because missing" % outnm  
    336               return 1  
    337           if name != self.name:  
    338               print "rebuilding %s because name changed" % outnm  
    339               return 1  
    340           if level != self.level:  
    341               print "rebuilding %s because level changed" % outnm  
    342               return 1  
    343           if toc != self.toc:  
    344               print "rebuilding %s because toc changed" % outnm  
    345               return 1  
    346           for (nm, fnm, typ) in toc:  
    347               if mtime(fnm) > last_build:  
    348                   print "rebuilding %s because %s changed" % (outnm, fnm)  
    349                   return 1  
    350               if fnm[-1] in ('c', 'o'):  
    351                   if mtime(fnm[:-1]) > last_build:  
    352                       print "rebuilding %s because %s changed" % (outnm, fnm[:-1])  
    353                       return 1  
    354           return 0  
      392             return True  
      393  
      394         data = Target.get_guts(self, last_build)  
      395         if not data:  
      396             return True  
      397         return False  
      398      
    355 399     def assemble(self):  
    356 400         print "building PYZ", os.path.basename(self.out)  
     
    457 501                 self.cdict = { 'PYSOURCE':UNCOMPRESSED }  
    458 502         self.__postinit__()  
      503  
      504     GUTS = (('name',   _check_guts_eq),  
      505             ('cdict',  _check_guts_eq),  
      506             ('toc',    _check_guts_toc_mtime),  
      507             ('exclude_binaries',  _check_guts_eq),  
      508             ('strip_binaries',  _check_guts_eq),  
      509             ('upx_binaries',  _check_guts_eq),  
      510             )  
      511  
    459 512     def check_guts(self, last_build):  
    460 513         outnm = os.path.basename(self.out)  
     
    462 515             print "rebuilding %s because %s is missing" % (outnm, os.path.basename(self.name))  
    463 516             return 1  
    464           try:  
    465               name, cdict, toc, exclude_binaries, strip_binaries, upx_binaries = _load_data(self.out)  
    466           except:  
    467               print "rebuilding %s because %s is missing" % (outnm, outnm)  
    468               return 1  
    469           if name != self.name:  
    470               print "rebuilding %s because name changed" % outnm  
    471               return 1  
    472           if cdict != self.cdict:  
    473               print "rebuilding %s because cdict changed" % outnm  
    474               return 1  
    475           if toc != self.toc:  
    476               print "rebuilding %s because toc changed" % outnm  
    477               return 1  
    478           if exclude_binaries != self.exclude_binaries:  
    479               print "rebuilding %s because exclude_binaries changed" % outnm  
    480               return 1  
    481           if strip_binaries != self.strip_binaries:  
    482               print "rebuilding %s because strip_binaries changed" % outnm  
    483               return 1  
    484           if upx_binaries != self.upx_binaries:  
    485               print "rebuilding %s because upx_binaries changed" % outnm  
    486               return 1  
    487           for (nm, fnm, typ) in toc:  
    488               if mtime(fnm) > last_build:  
    489                   print "rebuilding %s because %s changed" % (outnm, fnm)  
    490                   return 1  
    491           return 0  
      517          
      518         data = Target.get_guts(self, last_build)  
      519         if not data:  
      520             return True  
      521         # todo: toc equal  
      522         return False  
      523  
      524  
    492 525     def assemble(self):  
    493 526         print "building PKG", os.path.basename(self.name)  
     
    569 602         self.dependencies = self.pkg.dependencies  
    570 603         self.__postinit__()  
      604  
      605     GUTS = (('name',     _check_guts_eq),  
      606             ('console',  _check_guts_eq),  
      607             ('debug',    _check_guts_eq),  
      608             ('icon',     _check_guts_eq),  
      609             ('versrsrc', _check_guts_eq),  
      610             ('strip',    _check_guts_eq),  
      611             ('upx',      _check_guts_eq),  
      612             ('mtm',      None,), # checked bellow  
      613             )  
      614  
    571 615     def check_guts(self, last_build):  
    572 616         outnm = os.path.basename(self.out)  
     
    578 622                 os.path.basename(self.pkgname),)  
    579 623             return 1  
    580           try:  
    581               name, console, debug, icon, versrsrc, strip, upx, mtm = _load_data(self.out)  
    582           except:  
    583               print "rebuilding %s because %s missing or bad" % (outnm, outnm)  
    584               return 1  
    585           if name != self.name:  
    586               print "rebuilding %s because name changed" % outnm  
    587               return 1  
    588           if console != self.console:  
    589               print "rebuilding %s because console option changed" % outnm  
    590               return 1  
    591           if debug != self.debug:  
    592               print "rebuilding %s because debug option changed" % outnm  
    593               return 1  
    594           if config['hasRsrcUpdate']:  
    595               if icon != self.icon:  
    596                   print "rebuilding %s because icon option changed" % outnm  
    597                   return 1  
    598               if versrsrc != self.versrsrc:  
    599                   print "rebuilding %s because versrsrc option changed" % outnm  
    600                   return 1  
    601           else:  
    602               if icon or versrsrc:  
    603                   print "ignoring icon and version resources = platform not capable"  
    604           if strip != self.strip:  
    605               print "rebuilding %s because strip option changed" % outnm  
    606               return 1  
    607           if upx != self.upx:  
    608               print "rebuilding %s because upx option changed" % outnm  
    609               return 1  
      624  
      625         data = Target.get_guts(self, last_build)  
      626         if not data:  
      627             return True  
      628  
      629         icon, versrsrc = data[3:5]  
      630         if (icon or versrsrc) and not config['hasRsrcUpdate']:  
      631             # todo: really ignore :-)  
      632             print "ignoring icon and version resources = platform not capable"  
      633  
      634         mtm = data[-1]  
    610 635         if mtm != mtime(self.name):  
    611               print "rebuilding %s because mtimes don't match" % outnm  
    612               return 1  
      636             print "rebuilding", outnm, "because mtimes don't match"  
      637             return True  
    613 638         if mtm < mtime(self.pkg.out):  
    614               print "rebuilding %s because pkg is more recent" % outnm  
    615               return 1  
    616           return 0  
      639             print "rebuilding", outnm, "because pkg is more recent"  
      640             return True  
      641  
      642         return False  
      643  
    617 644     def _bootloader_postfix(self, exe):  
    618 645         if target_iswin:  
     
    628 655                 exe = exe + '_d'  
    629 656         return exe  
      657      
    630 658     def assemble(self):  
    631 659         print "building EXE from", os.path.basename(self.out)  
     
    716 744                 self.toc.extend(arg)  
    717 745         self.__postinit__()  
      746  
      747     GUTS = (('name',            _check_guts_eq),  
      748             ('strip_binaries',  _check_guts_eq),  
      749             ('upx_binaries',    _check_guts_eq),  
      750             ('toc',             _check_guts_eq), # additional check below  
      751             )  
      752          
    718 753     def check_guts(self, last_build):  
    719           outnm = os.path.basename(self.out)  
    720           try:  
    721               name, strip_binaries, upx_binaries, toc = _load_data(self.out)  
    722           except:  
    723               print "building %s because %s missing" % (outnm, outnm)  
    724               return 1  
    725           if name != self.name:  
    726               print "building %s because name changed" % outnm  
    727               return 1  
    728           if strip_binaries != self.strip_binaries:  
    729               print "building %s because strip_binaries option changed" % outnm  
    730               return 1  
    731           if upx_binaries != self.upx_binaries:  
    732               print "building %s because upx_binaries option changed" % outnm  
    733               return 1  
    734           if toc != self.toc:  
    735               print "building %s because toc changed" % outnm  
    736               return 1  
      754         data = Target.get_guts(self, last_build)  
      755         if not data:  
      756             return True  
      757         toc = data[-1]  
    737 758         for inm, fnm, typ in self.toc:  
    738 759             if typ == 'EXTENSION':  
     
    748 769                 return 1  
    749 770         return 0  
      771  
    750 772     def assemble(self):  
    751 773         print "building COLLECT", os.path.basename(self.out)  
     
    848 870             self.excludes = []  
    849 871         self.__postinit__()  
      872  
      873     GUTS = (('root',     _check_guts_eq),  
      874             ('prefix',   _check_guts_eq),  
      875             ('excludes', _check_guts_eq),  
      876             ('toc',      None),  
      877             )  
      878  
    850 879     def check_guts(self, last_build):  
    851           outnm = os.path.basename(self.out)  
    852           try:  
    853               root, prefix, excludes, toc = _load_data(self.out)  
    854           except:  
    855               print "building %s because %s is missing / bad" % (outnm, outnm)  
    856               return 1  
    857           if root != self.root:  
    858               print "building %s because root changed" % outnm  
    859               return 1  
    860           if prefix != self.prefix:  
    861               print "building %s because prefix changed" % outnm  
    862               return 1  
    863           if excludes != self.excludes:  
    864               print "building %s because excludes changed" % outnm  
    865               return 1  
    866           stack = [root]  
      880         data = Target.get_guts(self, last_build)  
      881         if not data:  
      882             return True  
      883         stack = [ data[0] ] # root  
      884         toc = data[3] # toc  
    867 885         while stack:  
    868 886             d = stack.pop()  
    869 887             if mtime(d) > last_build:  
    870 888                 print "building %s because directory %s changed" % (outnm, d)  
    871                   return 1  
      889                 return True  
    871 889             for nm in os.listdir(d):  
    872 890                 path = os.path.join(d, nm)  
     
    875 893                     stack.append(path)  
    876 894         self.data = toc  
    877           return 0  
      895         return False  
      896  
    878 897     def assemble(self):  
    879 898         print "building Tree", os.path.basename(self.out)