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