Changeset 475

Show
Ignore:
Timestamp:
Wed Jul 23 13:32:35 2008
Author:
htgoebel
Message:

implemented zipimport

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/Build.py

    r449 r475  
    127 127         self.pure = TOC()  
    128 128         self.binaries = TOC()  
      129         self.zipfiles = TOC()  
    129 130         self.__postinit__()  
    130 131     def check_guts(self, last_build):  
     
    165 166                 print "building because %s changed" % fnm[:-1]  
    166 167                 return True  
      168         # todo: add zipfiles  
    167 169         for (nm, fnm, typ) in binaries:  
    168 170             if mtime(fnm) > last_build:  
     
    208 210         pure = []     # pure python modules  
    209 211         binaries = [] # binaries to bundle  
      212         zipfiles = [] # zipfiles to bundle  
    210 213         rthooks = []  # rthooks if needed  
    211 214         for modnm, mod in analyzer.modules.items():  
     
    221 224                     if isinstance(mod, mf.ExtensionModule):  
    222 225                         binaries.append((mod.__name__, fnm, 'EXTENSION'))  
      226                     elif isinstance(mod, mf.PkgInZipModule):  
      227                         zipfiles.append((os.path.basename(str(mod.owner)),  
      228                                          str(mod.owner), 'ZIPFILE'))  
    223 229                     elif modnm == '__main__':  
    224 230                         pass  
     
    231 237         self.pure = TOC(pure)  
    232 238         self.binaries = TOC(binaries)  
      239         self.zipfiles = TOC(zipfiles)  
    233 240         try: # read .toc  
    234 241             oldstuff = eval(open(self.out, 'r').read())  
    235 242         except:  
    236 243             oldstuff = None  
      244         # todo: add zipfiles  
    237 245         if oldstuff != (self.inputs, self.pathex, self.hookspath, self.excludes, scripts, pure, binaries):  
    238 246             outf = open(self.out, 'w')  
     
    413 421                  'DATA': 'x',  
    414 422                  'BINARY': 'b',  
      423                  'ZIPFILE': 'Z',  
    415 424                  'EXECUTABLE': 'b'}  
    416 425     def __init__(self, toc, name=None, cdict=None, exclude_binaries=0,  
     
    608 617         return exe  
    609 618     def assemble(self):  
    610           print "building EXE", os.path.basename(self.out)  
      619         print "building EXE from", os.path.basename(self.out)  
    610 619         trash = []  
    611 620         outf = open(self.name, 'wb')  
  • trunk/mf.py

    r474 r475  
    16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA  
    17 17 import sys, string, os, imp, marshal, dircache  
      18 try:  
      19     # zipimport is supported starting with Python 2.3  
      20     import zipimport  
      21 except ImportError:  
      22     zipimport = None  
    18 23  
    19 24 #=======================Owners==========================#  
     
    138 143         return PyModule(nm, self.path, co)  
    139 144  
    140   _globalownertypes = [  
      145 ZipOwner = None  
      146 if zipimport:  
      147     class ZipOwner(Owner):  
      148         def __init__(self, path):  
      149             print 'ZipOwner', path  
      150             self.__zip = zipimport.zipimporter(path)  
      151             Owner.__init__(self, path)  
      152  
      153         def getmod(self, nm):  
      154             try:  
      155                 co = self.__zip.get_code(nm)  
      156                 return PkgInZipModule(nm, co, self)  
      157             except zipimport.ZipImportError:  
      158                 return None  
      159  
      160 _globalownertypes = filter(None, [  
    141 161     DirOwner,  
      162     ZipOwner,  
    142 163     PYZOwner,  
    143 164     Owner,  
    144   ]  
      165 ])  
    144 165  
    145 166 #===================Import Directors====================================#  
     
    218 239                 co = loadco(stuff[8:])  
    219 240             return PyModule(nm, fnm, co)  
    220           return None  
      241         return None             
      242  
    221 243 class PathImportDirector(ImportDirector):  
    222 244     def __init__(self, pathlist=None, importers=None, ownertypes=None):  
     
    597 619         return mod  
    598 620  
      621 class PkgInZipModule(PyModule):  
      622     typ = 'ZIPFILE'  
      623     def __init__(self, nm, co, pyzowner):  
      624         PyModule.__init__(self, nm, co.co_filename, co)  
      625         self._ispkg = 0  
      626         self.__path__ = [ str(pyzowner) ]  
      627         self.owner = pyzowner  
      628     def doimport(self, nm):  
      629         mod = self.owner.getmod(self.__name__ + '.' + nm)  
      630         return mod  
      631  
    599 632 #======================== Utility ================================#  
    600 633 # Scan the code object for imports, __all__ and wierd stuff  
  • trunk/source/common/launch.c

    r449 r475  
    695 695 }  
    696 696  
      697 /* Add a zipfile to sys.path from a toc entry  
      698  * Return non zero on failure  
      699  */  
      700 int installZipfile(TOC *ptoc)  
      701 {  
      702         int rc;  
      703         char *tmpl = "sys.path.append(r\"%s" SEP "%s\")\n";  
      704         char *cmd = (char *) malloc(strlen(tmpl) + strlen(f_workpath)  
      705                                     + strlen(ptoc->name)+ 10);  
      706         sprintf(cmd, tmpl, f_workpath, ptoc->name);  
      707         /*VS(cmd);*/  
      708         rc = PI_PyRun_SimpleString(cmd);  
      709         if (rc != 0)  
      710         {  
      711                 FATALERROR("Error in command: %s\n", cmd);  
      712                 free(cmd);  
      713                 return -1;  
      714         }  
      715  
      716         free(cmd);  
      717         return 0;  
      718 }  
    697 719  
    698 720 /*  
     
    705 727         VS("Installing import hooks\n");  
    706 728  
    707           /* Iterate through toc looking for zlibs (type 'z') */  
      729         /* Iterate through toc looking for zlibs (type 'z') or  
      730          *  zipfiles (type 'Z')  
      731          */  
    708 732         ptoc = f_tocbuff;  
    709 733         while (ptoc < f_tocend) {  
    710                   if (ptoc->typcd == 'z')   
      734                 if (ptoc->typcd == 'z')  
    710 734                 {  
    711 735                         VS("%s\n", ptoc->name);  
    712 736                         installZlib(ptoc);  
    713 737                 }  
      738                 else if (ptoc->typcd == 'Z')  
      739                 {  
      740                         VS("zipfile: %s\n", ptoc->name);  
      741                         installZipfile(ptoc);  
      742                 }  
    714 743  
    715 744                 ptoc = incrementTocPtr(ptoc);  
     
    856 885 }  
    857 886 /*  
    858    * extract all binaries (type 'b') to the filesystem  
      887  * extract all binaries (type 'b') and zipfiles (type 'Z')  
      888  * to the filesystem  
    859 889  */  
    860 890 int extractBinaries(char **workpath)  
     
    864 894         VS("Extracting binaries\n");  
    865 895         while (ptoc < f_tocend) {  
    866                   if (ptoc->typcd == 'b')  
      896                 if (ptoc->typcd == 'b' || ptoc->typcd == 'Z')  
    866 896                 if (extract2fs(ptoc))  
    867 897                 return -1;  
  • trunk/iu.py

    r474 r475  
    34 34     py_version = (1,5)  
    35 35  
      36 try:  
      37     # zipimport is supported starting with Python 2.3  
      38     import zipimport  
      39 except ImportError:  
      40     zipimport = None  
      41  
    36 42 #=======================Owners==========================#  
    37 43 # An Owner does imports from a particular piece of turf  
     
    119 125         return mod  
    120 126  
    121   _globalownertypes = [  
      127 ZipOwner = None  
      128 if zipimport:  
      129     class ZipOwner(Owner):  
      130         def __init__(self, path):  
      131             try:  
      132                 self.__zip = zipimport.zipimporter(path)  
      133             except zipimport.ZipImportError, e:  
      134                 raise OwnerError('%s: %s' % (e.message, path))  
      135             Owner.__init__(self, path)  
      136  
      137         def getmod(self, nm, newmod=imp.new_module):  
      138             try:  
      139                 return self.__zip.load_module(nm)  
      140             except zipimport.ZipImportError:  
      141                 return None  
      142  
      143 # _mountzlib.py will insert archive.PYZOwner in front later  
      144 _globalownertypes = filter(None, [  
      145     ZipOwner,  
    122 146     DirOwner,  
    123 147     Owner,  
    124   ]  
      148 ])  
    124 148  
    125 149 #===================Import Directors====================================#  
     
    429 453         else:  
    430 454             sys.modules[fqname] = None  
    431           #print "..found %s" % mod  
      455         #print "..found %s" % mod, 'when looking for', fqname  
    431 455         return mod  
    432 456     def reloadHook(self, mod):  
  • trunk/archive.py

    r449 r475  
    72 72     _verbose = 1  
    73 73  
      74 class ArchiveReadError(RuntimeError): pass  
      75  
    74 76 class Archive:  
    75 77     """ A base class for a repository of python code objects.  
     
    107 109         self.lib.seek(self.start)       #default - magic is at start of file  
    108 110         if self.lib.read(len(self.MAGIC)) != self.MAGIC:  
    109               raise RuntimeError, "%s is not a valid %s archive file" \  
      111             raise ArchiveReadError, "%s is not a valid %s archive file" \  
    109 111               % (self.path, self.__class__.__name__)  
    110 112         if self.lib.read(len(self.pymagic)) != self.pymagic:  
    111               raise RuntimeError, "%s has version mismatch to dll" % (self.path)  
      113             raise ArchiveReadError, "%s has version mismatch to dll" % (self.path)  
    111 113         self.lib.read(4)  
    112 114  
     
    373 375         try:  
    374 376             self.pyz = ZlibArchive(path)  
    375           except IOError, e:  
      377             self.pyz.checkmagic()  
      378         except (IOError, ArchiveReadError), e:  
    376 379             raise iu.OwnerError(e)  
    377 380         iu.Owner.__init__(self, path)