1 #!/usr/bin/env python 2 3 """ 4 An import hook for class files. 5 6 Copyright (C) 2004, 2005, 2006, 2011 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU Lesser General Public License as published by the Free 10 Software Foundation; either version 3 of the License, or (at your option) any 11 later version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 details. 17 18 You should have received a copy of the GNU Lesser General Public License along 19 with this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import ihooks # for the import machinery 23 import os, glob # for getting suitably-named files 24 from imp import PY_SOURCE, PKG_DIRECTORY, C_BUILTIN # import machinery magic 25 import classfile, bytecode # Java class support 26 import zipfile # for Java archive inspection 27 import sys 28 29 # NOTE: Arbitrary constants pulled from thin air. 30 31 JAVA_PACKAGE = 20041113 32 JAVA_CLASS = 20041114 33 JAVA_ARCHIVE = 20041115 34 35 class ClassHooks(ihooks.Hooks): 36 37 "A filesystem hooks class providing information about supported files." 38 39 def get_suffixes(self): 40 41 "Return the recognised suffixes." 42 43 return [("", "", JAVA_PACKAGE), (os.extsep + "jar", "r", JAVA_ARCHIVE)] + ihooks.Hooks.get_suffixes(self) 44 45 def path_isdir(self, x, archive=None): 46 47 "Return whether 'x' is a directory in the given 'archive'." 48 49 if archive is None: 50 return ihooks.Hooks.path_isdir(self, x) 51 52 return self._get_dirname(x) in archive.namelist() 53 54 def _get_dirname(self, x): 55 56 """ 57 Return the directory name for 'x'. 58 In zip files, the presence of "/" seems to indicate a directory. 59 """ 60 61 if x.endswith("/"): 62 return x 63 else: 64 return x + "/" 65 66 def listdir(self, x, archive=None): 67 68 "Return the contents of the directory 'x' in the given 'archive'." 69 70 if archive is None: 71 return ihooks.Hooks.listdir(self, x) 72 73 x = self._get_dirname(x) 74 l = [] 75 for path in archive.namelist(): 76 77 # Find out if the path is within the given directory. 78 79 if path != x and path.startswith(x): 80 81 # Get the path below the given directory. 82 83 subpath = path[len(x):] 84 85 # Find out whether the path is an object in the current directory. 86 87 if subpath.count("/") == 0 or subpath.count("/") == 1 and subpath.endswith("/"): 88 l.append(subpath) 89 90 return l 91 92 def matching(self, dir, extension, archive=None): 93 94 """ 95 Return the matching files in the given directory 'dir' having the given 96 'extension' within the given 'archive'. Produce a list containing full 97 paths as opposed to simple filenames. 98 """ 99 100 if archive is None: 101 return glob.glob(self.path_join(dir, "*" + extension)) 102 103 dir = self._get_dirname(dir) 104 l = [] 105 for path in self.listdir(dir, archive): 106 if path.endswith(extension): 107 l.append(self.path_join(dir, path)) 108 return l 109 110 def read(self, filename, archive=None): 111 112 """ 113 Return the contents of the file with the given 'filename' in the given 114 'archive'. 115 """ 116 117 if archive is None: 118 f = open(filename, "rb") 119 s = f.read() 120 f.close() 121 return s 122 return archive.read(filename) 123 124 class ClassLoader(ihooks.ModuleLoader): 125 126 "A class providing support for searching directories for supported files." 127 128 def find_module(self, name, path=None): 129 130 """ 131 Find the module with the given 'name', using the given 'path' to locate 132 it. Note that ModuleLoader.find_module is almost sufficient, but does 133 not provide enough support for "package unions" where the root of a 134 package hierarchy may appear in several places. 135 136 Return a list of locations (each being the "stuff" data structure used 137 by load_module); this replaces the single "stuff" value or None returned 138 by ModuleLoader.find_module. 139 """ 140 141 if path is None: 142 path = [None] + self.default_path() 143 144 found_locations = [] 145 146 for dir in path: 147 stuff = self.find_module_in_dir(name, dir) 148 if stuff: 149 found_locations.append(stuff) 150 151 return found_locations 152 153 def find_module_in_dir(self, name, dir, allow_packages=1): 154 155 """ 156 Find the module with the given 'name' in the given directory 'dir'. 157 Since Java packages/modules are directories containing class files, 158 return the required information tuple only when the path constructed 159 from 'dir' and 'name' refers to a directory containing class files. 160 """ 161 162 result = ihooks.ModuleLoader.find_module_in_dir(self, name, dir, allow_packages) 163 if result is not None: 164 return result 165 166 # An archive may be opened. 167 168 archive = None 169 170 # Provide a special name for the current directory. 171 172 if name == "__this__": 173 if dir == None: 174 return (None, ".", ("", "", JAVA_PACKAGE)) 175 else: 176 return None 177 178 # Where no directory is given, return failure immediately. 179 180 elif dir is None: 181 return None 182 183 # Detect archives. 184 185 else: 186 archive, archive_path, path = self._get_archive_and_path(dir, name) 187 188 189 if self._find_module_at_path(path, archive): 190 if archive is not None: 191 return (archive, archive_path + ":" + path, (os.extsep + "jar", "r", JAVA_ARCHIVE)) 192 else: 193 return (None, path, ("", "", JAVA_PACKAGE)) 194 else: 195 return None 196 197 def _get_archive_and_path(self, dir, name): 198 parts = dir.split(":") 199 archive_path = parts[0] 200 201 # Archives may include an internal path, but will in any case have 202 # a primary part ending in .jar. 203 204 if archive_path.endswith(os.extsep + "jar"): 205 archive = zipfile.ZipFile(archive_path, "r") 206 path = self.hooks.path_join(":".join(parts[1:]), name) 207 208 # Otherwise, produce a filesystem-based path. 209 210 else: 211 archive = None 212 path = self.hooks.path_join(dir, name) 213 214 return archive, archive_path, path 215 216 def _get_path_in_archive(self, path): 217 parts = path.split(":") 218 if len(parts) == 1: 219 return parts[0] 220 else: 221 return ":".join(parts[1:]) 222 223 def _find_module_at_path(self, path, archive): 224 if self.hooks.path_isdir(path, archive): 225 226 # Look for classes in the directory. 227 228 if len(self.hooks.matching(path, os.extsep + "class", archive)) != 0: 229 return 1 230 231 # Otherwise permit importing where directories containing classes exist. 232 233 for filename in self.hooks.listdir(path, archive): 234 pathname = self.hooks.path_join(path, filename) 235 result = self._find_module_at_path(pathname, archive) 236 if result is not None: 237 return result 238 239 return 0 240 241 def load_module(self, name, stuff): 242 243 """ 244 Load the module with the given 'name', with a list of 'stuff' items, 245 each of which describes the location of the module and is a tuple of the 246 form (file, filename, (suffix, mode, data type)). 247 248 Return a module object or raise an ImportError if a problem occurred in 249 the import operation. 250 251 Note that the 'stuff' parameter is a list and not a single item as in 252 ModuleLoader.load_module. This should still work, however, since the 253 find_module method produces such a list. 254 """ 255 256 #print "load_module", name 257 module = self._not_java_module(name, stuff) 258 if module is not None: 259 return module 260 261 if not hasattr(self, "loaded_classes"): 262 self.loaded_classes = {} 263 top_level = 1 264 else: 265 top_level = 0 266 267 main_module = self._load_module(name, stuff) 268 269 # Initialise the loaded classes. 270 271 if top_level: 272 self._init_classes() 273 delattr(self, "loaded_classes") 274 275 return main_module 276 277 def _not_java_module(self, name, stuff): 278 279 "Detect non-Java modules." 280 281 for stuff_item in stuff: 282 archive, filename, info = stuff_item 283 suffix, mode, datatype = info 284 if datatype not in (JAVA_PACKAGE, JAVA_ARCHIVE): 285 return ihooks.ModuleLoader.load_module(self, name, stuff_item) 286 287 return None 288 289 def _load_module(self, name, stuff): 290 291 # Set up the module. 292 # A union of all locations is placed in the module's path. 293 294 external_names = [] 295 module = self.hooks.add_module(name) 296 module.__path__ = [item_filename for (item_archive, item_filename, item_info) in stuff] 297 298 # Prepare a dictionary of globals. 299 300 global_names = module.__dict__ 301 global_names["__builtins__"] = __builtins__ 302 303 # Just go into each package and find the class files. 304 305 classes = {} 306 for stuff_item in stuff: 307 308 # Extract the details, delegating loading responsibility to the 309 # default loader where appropriate. 310 # NOTE: Should we not be using some saved loader remembered upon 311 # NOTE: installation? 312 313 archive, filename, info = stuff_item 314 suffix, mode, datatype = info 315 316 # Get the real filename. 317 318 filename = self._get_path_in_archive(filename) 319 320 # Load the class files. 321 322 for class_filename in self.hooks.matching(filename, os.extsep + "class", archive): 323 s = self.hooks.read(class_filename, archive) 324 class_file = classfile.ClassFile(s) 325 #print "Translating", str(class_file.this_class.get_name()) 326 translator = bytecode.ClassTranslator(class_file) 327 external_names += translator.process(global_names) 328 329 # Record the classes found under the current module. 330 331 self.loaded_classes[str(class_file.this_class.get_name())] = module, translator 332 333 # Return modules used by external names. 334 335 external_module_names = self._get_external_module_names(external_names, name) 336 337 # Repeatedly load classes from referenced modules. 338 339 for module_name in external_module_names: 340 new_module = __import__(module_name, global_names) 341 global_names[module_name.split(".")[0]] = new_module 342 343 return module 344 345 def _get_external_module_names(self, names, current_module_name): 346 groups = self._get_names_grouped_by_module(names) 347 if groups.has_key(""): 348 del groups[""] 349 350 # NOTE: Could filter out the current module and all parent modules. 351 # NOTE: 352 # NOTE: current_module_parts = current_module_name.split(".") 353 # NOTE: while len(current_module_parts) > 0: 354 # NOTE: try: 355 # NOTE: del groups[".".join(current_module_parts)] 356 # NOTE: except KeyError: 357 # NOTE: pass 358 # NOTE: del current_module_parts[-1] 359 360 try: 361 del groups[".".join(current_module_name)] 362 except KeyError: 363 pass 364 365 return groups.keys() 366 367 def _get_names_grouped_by_module(self, names): 368 groups = {} 369 for name in names: 370 module_name, class_name = self._get_module_and_class_names(name) 371 if not groups.has_key(module_name): 372 groups[module_name] = [] 373 groups[module_name].append(class_name) 374 return groups 375 376 def _get_module_and_class_names(self, full_name): 377 full_name_parts = full_name.split(".") 378 class_name = full_name_parts[-1] 379 module_name = ".".join(full_name_parts[:-1]) 380 return module_name, class_name 381 382 def _init_classes(self): 383 384 # Order the classes according to inheritance. 385 386 init_order = [] 387 for class_name, (module, translator) in self.loaded_classes.items(): 388 389 # Insert the base classes before any mention of the current class. 390 391 for base_class in translator.get_base_class_references(): 392 base_class_name = str(base_class.get_name()) 393 if base_class_name not in init_order: 394 if class_name not in init_order: 395 init_order.append(base_class_name) 396 else: 397 index = init_order.index(class_name) 398 init_order.insert(index, base_class_name) 399 400 if class_name not in init_order: 401 init_order.append(class_name) 402 403 # Create the classes. 404 405 real_classes = {} 406 real_classes_index = [] 407 for class_name in init_order: 408 try: 409 module, translator = self.loaded_classes[class_name] 410 global_names = module.__dict__ 411 if not real_classes.has_key(module): 412 real_classes[module] = [] 413 real_class = translator.get_class(global_names, real_classes) 414 real_classes[class_name].append(real_class) 415 real_classes_index.append((module, real_class)) 416 except KeyError: 417 # NOTE: Should be a non-Java class. 418 pass 419 420 # Finally, call __clinit__ methods for all relevant classes. 421 422 for module, cls in real_classes_index: 423 if hasattr(cls, "__clinit__"): 424 global_names = module.__dict__ 425 eval(cls.__clinit__.func_code, global_names) 426 427 ihooks.ModuleImporter(loader=ClassLoader(hooks=ClassHooks())).install() 428 429 # vim: tabstop=4 expandtab shiftwidth=4