/[wryebash]/branches/upstream-master/scripts/zipextimporter.py
ViewVC logotype

Contents of /branches/upstream-master/scripts/zipextimporter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations) (download) (as text)
Mon Sep 19 03:20:26 2016 UTC (3 years, 5 months ago) by william
File MIME type: text/x-python
File size: 4423 byte(s)
add upstream git branches for dev and master
1 r"""zipextimporter - an importer which can import extension modules from zipfiles
2
3 This file and also _memimporter.pyd is part of the py2exe package.
4
5 Overview
6 ========
7
8 zipextimporter.py contains the ZipExtImporter class which allows to
9 load Python binary extension modules contained in a zip.archive,
10 without unpacking them to the file system.
11
12 Call the zipextimporter.install() function to install the import hook,
13 add a zip-file containing .pyd or .dll extension modules to sys.path,
14 and import them.
15
16 It uses the _memimporter extension which uses code from Joachim
17 Bauch's MemoryModule library. This library emulates the win32 api
18 function LoadLibrary.
19
20 Sample usage
21 ============
22
23 You have to prepare a zip-archive 'lib.zip' containing
24 your Python's _socket.pyd for this example to work.
25
26 >>> import zipextimporter
27 >>> zipextimporter.install()
28 >>> import sys
29 >>> sys.path.insert(0, "lib.zip")
30 >>> import _socket
31 >>> print _socket
32 <module '_socket' from 'lib.zip\_socket.pyd'>
33 >>> _socket.__file__
34 'lib.zip\\_socket.pyd'
35 >>> _socket.__loader__
36 <ZipExtensionImporter object 'lib.zip'>
37 >>> # Reloading also works correctly:
38 >>> _socket is reload(_socket)
39 True
40 >>>
41
42 """
43 import imp, sys
44 import zipimport
45 import _memimporter
46
47 class ZipExtensionImporter(zipimport.zipimporter):
48 _suffixes = [s[0] for s in imp.get_suffixes() if s[2] == imp.C_EXTENSION]
49
50 def find_module(self, fullname, path=None):
51 result = zipimport.zipimporter.find_module(self, fullname, path)
52 if result:
53 return result
54 if fullname in ("pywintypes", "pythoncom"):
55 fullname = fullname + "%d%d" % sys.version_info[:2]
56 fullname = fullname.replace(".", "\\") + ".dll"
57 if fullname in self._files:
58 return self
59 else:
60 fullname = fullname.replace(".", "\\")
61 for s in self._suffixes:
62 if (fullname + s) in self._files:
63 return self
64 return None
65
66 def locate_dll_image(self, name):
67 # A callback function for_memimporter.import_module. Tries to
68 # locate additional dlls. Returns the image as Python string,
69 # or None if not found.
70 if name in self._files:
71 return self.get_data(name)
72 return None
73
74 def load_module(self, fullname):
75 if sys.modules.has_key(fullname):
76 mod = sys.modules[fullname]
77 if _memimporter.get_verbose_flag():
78 sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
79 return mod
80 _memimporter.set_find_proc(self.locate_dll_image)
81 filename = fullname.replace(".","\\")
82 try:
83 for s in ('.pyd','.pyc','.py'):
84 try:
85 with open(filename+s,'U') as fp:
86 return imp.load_module(fullname,fp,filename+s,(s,'U',1))
87 except:
88 continue
89 except:
90 pass
91 try:
92 return zipimport.zipimporter.load_module(self, fullname)
93 except zipimport.ZipImportError:
94 pass
95 initname = "init" + fullname.split(".")[-1] # name of initfunction
96 if filename in ("pywintypes", "pythoncom"):
97 filename = filename + "%d%d" % sys.version_info[:2]
98 suffixes = ('.dll',)
99 else:
100 suffixes = self._suffixes
101 for s in suffixes:
102 path = filename + s
103 if path in self._files:
104 if _memimporter.get_verbose_flag():
105 sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
106 code = self.get_data(path)
107 mod = _memimporter.import_module(code, initname, fullname, path)
108 mod.__file__ = "%s\\%s" % (self.archive, path)
109 mod.__loader__ = self
110 if _memimporter.get_verbose_flag():
111 sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
112 return mod
113 raise zipimport.ZipImportError, "can't find module %s" % fullname
114
115 def __repr__(self):
116 return "<%s object %r>" % (self.__class__.__name__, self.archive)
117
118 def install():
119 "Install the zipextimporter"
120 sys.path_hooks.insert(0, ZipExtensionImporter)
121 sys.path_importer_cache.clear()
122
123 ##if __name__ == "__main__":
124 ## import doctest
125 ## doctest.testmod()

  ViewVC Help
Powered by ViewVC 1.1.22