paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@78 | 3 | import ihooks # for the import machinery |
paul@78 | 4 | import os, glob # for getting suitably-named files |
paul@78 | 5 | from imp import PY_SOURCE, PKG_DIRECTORY, C_BUILTIN # import machinery magic |
paul@78 | 6 | import classfile, bytecode # Java class support |
paul@78 | 7 | import zipfile # for Java archive inspection |
paul@78 | 8 | |
paul@78 | 9 | # NOTE: Arbitrary constants pulled from thin air. |
paul@39 | 10 | |
paul@52 | 11 | JAVA_PACKAGE = 20041113 |
paul@52 | 12 | JAVA_CLASS = 20041114 |
paul@78 | 13 | JAVA_ARCHIVE = 20041115 |
paul@52 | 14 | |
paul@39 | 15 | class ClassHooks(ihooks.Hooks): |
paul@39 | 16 | |
paul@39 | 17 | "A filesystem hooks class providing information about supported files." |
paul@39 | 18 | |
paul@39 | 19 | def get_suffixes(self): |
paul@39 | 20 | |
paul@39 | 21 | "Return the recognised suffixes." |
paul@39 | 22 | |
paul@78 | 23 | return [("", "", JAVA_PACKAGE), (os.extsep + "jar", "r", JAVA_ARCHIVE)] + ihooks.Hooks.get_suffixes(self) |
paul@78 | 24 | |
paul@78 | 25 | def path_isdir(self, x, archive=None): |
paul@78 | 26 | |
paul@78 | 27 | "Return whether 'x' is a directory in the given 'archive'." |
paul@78 | 28 | |
paul@78 | 29 | if archive is None: |
paul@78 | 30 | return ihooks.Hooks.path_isdir(self, x) |
paul@78 | 31 | |
paul@78 | 32 | return self._get_dirname(x) in archive.namelist() |
paul@78 | 33 | |
paul@78 | 34 | def _get_dirname(self, x): |
paul@78 | 35 | |
paul@78 | 36 | """ |
paul@78 | 37 | Return the directory name for 'x'. |
paul@78 | 38 | In zip files, the presence of "/" seems to indicate a directory. |
paul@78 | 39 | """ |
paul@78 | 40 | |
paul@78 | 41 | if x.endswith("/"): |
paul@78 | 42 | return x |
paul@78 | 43 | else: |
paul@78 | 44 | return x + "/" |
paul@78 | 45 | |
paul@78 | 46 | def listdir(self, x, archive=None): |
paul@78 | 47 | |
paul@78 | 48 | "Return the contents of the directory 'x' in the given 'archive'." |
paul@78 | 49 | |
paul@78 | 50 | if archive is None: |
paul@78 | 51 | return ihooks.Hooks.listdir(self, x) |
paul@78 | 52 | |
paul@78 | 53 | x = self._get_dirname(x) |
paul@78 | 54 | l = [] |
paul@78 | 55 | for path in archive.namelist(): |
paul@78 | 56 | |
paul@78 | 57 | # Find out if the path is within the given directory. |
paul@78 | 58 | |
paul@78 | 59 | if path != x and path.startswith(x): |
paul@78 | 60 | |
paul@78 | 61 | # Get the path below the given directory. |
paul@78 | 62 | |
paul@78 | 63 | subpath = path[len(x):] |
paul@78 | 64 | |
paul@78 | 65 | # Find out whether the path is an object in the current directory. |
paul@78 | 66 | |
paul@78 | 67 | if subpath.count("/") == 0 or subpath.count("/") == 1 and subpath.endswith("/"): |
paul@78 | 68 | l.append(subpath) |
paul@78 | 69 | |
paul@78 | 70 | return l |
paul@78 | 71 | |
paul@78 | 72 | def matching(self, dir, extension, archive=None): |
paul@78 | 73 | |
paul@78 | 74 | """ |
paul@78 | 75 | Return the matching files in the given directory 'dir' having the given |
paul@78 | 76 | 'extension' within the given 'archive'. Produce a list containing full |
paul@78 | 77 | paths as opposed to simple filenames. |
paul@78 | 78 | """ |
paul@78 | 79 | |
paul@78 | 80 | if archive is None: |
paul@78 | 81 | return glob.glob(self.path_join(dir, "*" + extension)) |
paul@78 | 82 | |
paul@78 | 83 | dir = self._get_dirname(dir) |
paul@78 | 84 | l = [] |
paul@78 | 85 | for path in self.listdir(dir, archive): |
paul@78 | 86 | if path.endswith(extension): |
paul@78 | 87 | l.append(self.path_join(dir, path)) |
paul@78 | 88 | return l |
paul@78 | 89 | |
paul@78 | 90 | def read(self, filename, archive=None): |
paul@78 | 91 | |
paul@78 | 92 | """ |
paul@78 | 93 | Return the contents of the file with the given 'filename' in the given |
paul@78 | 94 | 'archive'. |
paul@78 | 95 | """ |
paul@78 | 96 | |
paul@78 | 97 | if archive is None: |
paul@78 | 98 | f = open(filename, "rb") |
paul@78 | 99 | s = f.read() |
paul@78 | 100 | f.close() |
paul@78 | 101 | return s |
paul@78 | 102 | return archive.read(filename) |
paul@39 | 103 | |
paul@39 | 104 | class ClassLoader(ihooks.ModuleLoader): |
paul@39 | 105 | |
paul@39 | 106 | "A class providing support for searching directories for supported files." |
paul@39 | 107 | |
paul@49 | 108 | def find_module_in_dir(self, name, dir, allow_packages=1): |
paul@39 | 109 | |
paul@39 | 110 | """ |
paul@39 | 111 | Find the module with the given 'name' in the given directory 'dir'. |
paul@39 | 112 | Since Java packages/modules are directories containing class files, |
paul@39 | 113 | return the required information tuple only when the path constructed |
paul@39 | 114 | from 'dir' and 'name' refers to a directory containing class files. |
paul@39 | 115 | """ |
paul@39 | 116 | |
paul@49 | 117 | result = ihooks.ModuleLoader.find_module_in_dir(self, name, dir, allow_packages) |
paul@49 | 118 | if result is not None: |
paul@49 | 119 | return result |
paul@39 | 120 | |
paul@78 | 121 | # An archive may be opened. |
paul@78 | 122 | |
paul@78 | 123 | archive = None |
paul@78 | 124 | |
paul@39 | 125 | # Provide a special name for the current directory. |
paul@39 | 126 | |
paul@39 | 127 | if name == "__this__": |
paul@49 | 128 | path = "." |
paul@78 | 129 | |
paul@78 | 130 | # Where no directory is given, return failure immediately. |
paul@78 | 131 | |
paul@49 | 132 | elif dir is None: |
paul@49 | 133 | return None |
paul@78 | 134 | |
paul@78 | 135 | # Detect archives. |
paul@78 | 136 | |
paul@39 | 137 | else: |
paul@78 | 138 | archive, archive_path, path = self._get_archive_and_path(dir, name) |
paul@39 | 139 | |
paul@79 | 140 | #print "Processing name", name, "in", dir, "producing", path, "within archive", archive |
paul@45 | 141 | |
paul@78 | 142 | if self._find_module_at_path(path, archive): |
paul@78 | 143 | if archive is not None: |
paul@78 | 144 | return (archive, archive_path + ":" + path, (os.extsep + "jar", "r", JAVA_ARCHIVE)) |
paul@78 | 145 | else: |
paul@78 | 146 | return (None, path, ("", "", JAVA_PACKAGE)) |
paul@45 | 147 | else: |
paul@45 | 148 | return None |
paul@45 | 149 | |
paul@78 | 150 | def _get_archive_and_path(self, dir, name): |
paul@78 | 151 | parts = dir.split(":") |
paul@78 | 152 | archive_path = parts[0] |
paul@78 | 153 | |
paul@78 | 154 | # Archives may include an internal path, but will in any case have |
paul@78 | 155 | # a primary part ending in .jar. |
paul@78 | 156 | |
paul@78 | 157 | if archive_path.endswith(os.extsep + "jar"): |
paul@78 | 158 | archive = zipfile.ZipFile(archive_path, "r") |
paul@78 | 159 | path = self.hooks.path_join(":".join(parts[1:]), name) |
paul@78 | 160 | |
paul@78 | 161 | # Otherwise, produce a filesystem-based path. |
paul@78 | 162 | |
paul@78 | 163 | else: |
paul@78 | 164 | archive = None |
paul@78 | 165 | path = self.hooks.path_join(dir, name) |
paul@78 | 166 | |
paul@78 | 167 | return archive, archive_path, path |
paul@78 | 168 | |
paul@78 | 169 | def _get_path_in_archive(self, path): |
paul@78 | 170 | parts = path.split(":") |
paul@78 | 171 | if len(parts) == 1: |
paul@78 | 172 | return parts[0] |
paul@78 | 173 | else: |
paul@78 | 174 | return ":".join(parts[1:]) |
paul@78 | 175 | |
paul@78 | 176 | def _find_module_at_path(self, path, archive): |
paul@78 | 177 | if self.hooks.path_isdir(path, archive): |
paul@79 | 178 | #print "Looking in", path, "using archive", archive |
paul@45 | 179 | |
paul@45 | 180 | # Look for classes in the directory. |
paul@45 | 181 | |
paul@78 | 182 | if len(self.hooks.matching(path, os.extsep + "class", archive)) != 0: |
paul@45 | 183 | return 1 |
paul@45 | 184 | |
paul@45 | 185 | # Otherwise permit importing where directories containing classes exist. |
paul@45 | 186 | |
paul@79 | 187 | #print "Filenames are", self.hooks.listdir(path, archive) |
paul@78 | 188 | for filename in self.hooks.listdir(path, archive): |
paul@78 | 189 | pathname = self.hooks.path_join(path, filename) |
paul@78 | 190 | result = self._find_module_at_path(pathname, archive) |
paul@45 | 191 | if result is not None: |
paul@45 | 192 | return result |
paul@45 | 193 | |
paul@78 | 194 | return 0 |
paul@39 | 195 | |
paul@39 | 196 | def load_module(self, name, stuff): |
paul@39 | 197 | |
paul@39 | 198 | """ |
paul@39 | 199 | Load the module with the given 'name', whose 'stuff' which describes the |
paul@39 | 200 | location of the module is a tuple of the form (file, filename, (suffix, |
paul@39 | 201 | mode, data type)). Return a module object or raise an ImportError if a |
paul@39 | 202 | problem occurred in the import operation. |
paul@39 | 203 | """ |
paul@39 | 204 | |
paul@39 | 205 | # Just go into the directory and find the class files. |
paul@39 | 206 | |
paul@78 | 207 | archive, filename, info = stuff |
paul@52 | 208 | suffix, mode, datatype = info |
paul@78 | 209 | if datatype not in (JAVA_PACKAGE, JAVA_ARCHIVE): |
paul@52 | 210 | return ihooks.ModuleLoader.load_module(self, name, stuff) |
paul@49 | 211 | |
paul@79 | 212 | #print "Loading", archive, filename, info |
paul@39 | 213 | |
paul@56 | 214 | # Set up the module. |
paul@56 | 215 | |
paul@41 | 216 | module = self.hooks.add_module(name) |
paul@56 | 217 | module.__path__ = [filename] |
paul@39 | 218 | |
paul@59 | 219 | # Prepare a dictionary of globals. |
paul@59 | 220 | |
paul@59 | 221 | global_names = module.__dict__ |
paul@59 | 222 | global_names["__builtins__"] = __builtins__ |
paul@59 | 223 | |
paul@39 | 224 | # Process each class file, producing a genuine Python class. |
paul@39 | 225 | |
paul@39 | 226 | class_files = [] |
paul@56 | 227 | classes = [] |
paul@59 | 228 | |
paul@78 | 229 | # Get the real filename. |
paul@78 | 230 | |
paul@78 | 231 | filename = self._get_path_in_archive(filename) |
paul@79 | 232 | #print "Real filename", filename |
paul@78 | 233 | |
paul@59 | 234 | # Load the class files. |
paul@59 | 235 | |
paul@59 | 236 | class_files = {} |
paul@78 | 237 | for class_filename in self.hooks.matching(filename, os.extsep + "class", archive): |
paul@79 | 238 | #print "Loading class", class_filename |
paul@78 | 239 | s = self.hooks.read(class_filename, archive) |
paul@39 | 240 | class_file = classfile.ClassFile(s) |
paul@59 | 241 | class_files[str(class_file.this_class.get_name())] = class_file |
paul@59 | 242 | |
paul@59 | 243 | # Get an index of the class files. |
paul@59 | 244 | |
paul@59 | 245 | class_file_index = class_files.keys() |
paul@59 | 246 | |
paul@59 | 247 | # NOTE: Unnecessary sorting for test purposes. |
paul@59 | 248 | |
paul@59 | 249 | class_file_index.sort() |
paul@59 | 250 | |
paul@59 | 251 | # Now go through the classes arranging them in a safe loading order. |
paul@59 | 252 | |
paul@59 | 253 | position = 0 |
paul@59 | 254 | while position < len(class_file_index): |
paul@59 | 255 | class_name = class_file_index[position] |
paul@59 | 256 | super_class_name = str(class_files[class_name].super_class.get_name()) |
paul@59 | 257 | |
paul@59 | 258 | # Discover whether the superclass appears later. |
paul@59 | 259 | |
paul@59 | 260 | try: |
paul@59 | 261 | super_class_position = class_file_index.index(super_class_name) |
paul@59 | 262 | if super_class_position > position: |
paul@59 | 263 | |
paul@59 | 264 | # If the superclass appears later, swap this class and the |
paul@59 | 265 | # superclass, then process the superclass. |
paul@59 | 266 | |
paul@59 | 267 | class_file_index[position] = super_class_name |
paul@59 | 268 | class_file_index[super_class_position] = class_name |
paul@59 | 269 | continue |
paul@59 | 270 | |
paul@59 | 271 | except ValueError: |
paul@59 | 272 | pass |
paul@59 | 273 | |
paul@59 | 274 | position += 1 |
paul@59 | 275 | |
paul@59 | 276 | class_files = [class_files[class_name] for class_name in class_file_index] |
paul@59 | 277 | |
paul@59 | 278 | for class_file in class_files: |
paul@39 | 279 | translator = bytecode.ClassTranslator(class_file) |
paul@70 | 280 | cls, external_names = translator.process(global_names) |
paul@39 | 281 | module.__dict__[cls.__name__] = cls |
paul@62 | 282 | classes.append((cls, class_file)) |
paul@39 | 283 | |
paul@70 | 284 | # Import the local names. |
paul@70 | 285 | |
paul@70 | 286 | for external_name in external_names: |
paul@70 | 287 | external_name_parts = external_name.split(".") |
paul@70 | 288 | if len(external_name_parts) > 1: |
paul@70 | 289 | external_module_name = ".".join(external_name_parts[:-1]) |
paul@70 | 290 | print "* Importing", external_module_name |
paul@70 | 291 | obj = __import__(external_module_name, global_names, {}, []) |
paul@70 | 292 | global_names[external_name_parts[0]] = obj |
paul@70 | 293 | |
paul@56 | 294 | # Finally, call __clinit__ methods for all relevant classes. |
paul@56 | 295 | |
paul@62 | 296 | for cls, class_file in classes: |
paul@70 | 297 | print "**", cls, class_file |
paul@64 | 298 | if hasattr(cls, "__clinit__"): |
paul@70 | 299 | eval(cls.__clinit__.func_code, global_names) |
paul@56 | 300 | |
paul@39 | 301 | return module |
paul@39 | 302 | |
paul@78 | 303 | ihooks.ModuleImporter(loader=ClassLoader(hooks=ClassHooks())).install() |
paul@39 | 304 | |
paul@39 | 305 | # vim: tabstop=4 expandtab shiftwidth=4 |