paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | import ihooks |
paul@39 | 4 | import os, glob |
paul@39 | 5 | from imp import PY_SOURCE, PKG_DIRECTORY, C_BUILTIN |
paul@39 | 6 | import classfile, bytecode |
paul@39 | 7 | import new |
paul@39 | 8 | |
paul@52 | 9 | JAVA_PACKAGE = 20041113 |
paul@52 | 10 | JAVA_CLASS = 20041114 |
paul@52 | 11 | |
paul@39 | 12 | class ClassHooks(ihooks.Hooks): |
paul@39 | 13 | |
paul@39 | 14 | "A filesystem hooks class providing information about supported files." |
paul@39 | 15 | |
paul@39 | 16 | def get_suffixes(self): |
paul@39 | 17 | |
paul@39 | 18 | "Return the recognised suffixes." |
paul@39 | 19 | |
paul@52 | 20 | return ihooks.Hooks.get_suffixes(self) + [("", "", JAVA_PACKAGE), (os.extsep + "class", "r", JAVA_CLASS)] |
paul@39 | 21 | |
paul@39 | 22 | class ClassLoader(ihooks.ModuleLoader): |
paul@39 | 23 | |
paul@39 | 24 | "A class providing support for searching directories for supported files." |
paul@39 | 25 | |
paul@49 | 26 | def find_module_in_dir(self, name, dir, allow_packages=1): |
paul@39 | 27 | |
paul@39 | 28 | """ |
paul@39 | 29 | Find the module with the given 'name' in the given directory 'dir'. |
paul@39 | 30 | Since Java packages/modules are directories containing class files, |
paul@39 | 31 | return the required information tuple only when the path constructed |
paul@39 | 32 | from 'dir' and 'name' refers to a directory containing class files. |
paul@39 | 33 | """ |
paul@39 | 34 | |
paul@49 | 35 | result = ihooks.ModuleLoader.find_module_in_dir(self, name, dir, allow_packages) |
paul@49 | 36 | if result is not None: |
paul@49 | 37 | return result |
paul@39 | 38 | |
paul@39 | 39 | # Provide a special name for the current directory. |
paul@39 | 40 | |
paul@39 | 41 | if name == "__this__": |
paul@49 | 42 | path = "." |
paul@49 | 43 | elif dir is None: |
paul@49 | 44 | return None |
paul@39 | 45 | else: |
paul@39 | 46 | path = os.path.join(dir, name) |
paul@39 | 47 | |
paul@52 | 48 | #print "Processing name", name, "in", dir, "producing", path |
paul@45 | 49 | |
paul@45 | 50 | if self._find_module_at_path(path): |
paul@52 | 51 | return (None, path, ("", "", JAVA_PACKAGE)) |
paul@45 | 52 | else: |
paul@45 | 53 | return None |
paul@45 | 54 | |
paul@45 | 55 | def _find_module_at_path(self, path): |
paul@39 | 56 | if os.path.isdir(path): |
paul@45 | 57 | |
paul@45 | 58 | # Look for classes in the directory. |
paul@45 | 59 | |
paul@39 | 60 | if len(glob.glob(os.path.join(path, "*" + os.extsep + "class"))) != 0: |
paul@45 | 61 | return 1 |
paul@45 | 62 | |
paul@45 | 63 | # Otherwise permit importing where directories containing classes exist. |
paul@45 | 64 | |
paul@45 | 65 | for filename in os.listdir(path): |
paul@45 | 66 | pathname = os.path.join(path, filename) |
paul@45 | 67 | result = self._find_module_at_path(pathname) |
paul@45 | 68 | if result is not None: |
paul@45 | 69 | return result |
paul@45 | 70 | |
paul@39 | 71 | return None |
paul@39 | 72 | |
paul@39 | 73 | def load_module(self, name, stuff): |
paul@39 | 74 | |
paul@39 | 75 | """ |
paul@39 | 76 | Load the module with the given 'name', whose 'stuff' which describes the |
paul@39 | 77 | location of the module is a tuple of the form (file, filename, (suffix, |
paul@39 | 78 | mode, data type)). Return a module object or raise an ImportError if a |
paul@39 | 79 | problem occurred in the import operation. |
paul@39 | 80 | """ |
paul@39 | 81 | |
paul@39 | 82 | # Just go into the directory and find the class files. |
paul@39 | 83 | |
paul@39 | 84 | file, filename, info = stuff |
paul@52 | 85 | suffix, mode, datatype = info |
paul@52 | 86 | if datatype != JAVA_PACKAGE: |
paul@52 | 87 | return ihooks.ModuleLoader.load_module(self, name, stuff) |
paul@49 | 88 | |
paul@49 | 89 | print "Loading", file, filename, info |
paul@39 | 90 | |
paul@56 | 91 | # Set up the module. |
paul@56 | 92 | |
paul@41 | 93 | module = self.hooks.add_module(name) |
paul@56 | 94 | module.__path__ = [filename] |
paul@39 | 95 | |
paul@59 | 96 | # Prepare a dictionary of globals. |
paul@59 | 97 | |
paul@59 | 98 | global_names = module.__dict__ |
paul@59 | 99 | global_names["__builtins__"] = __builtins__ |
paul@59 | 100 | |
paul@39 | 101 | # Process each class file, producing a genuine Python class. |
paul@39 | 102 | |
paul@39 | 103 | class_files = [] |
paul@56 | 104 | classes = [] |
paul@59 | 105 | |
paul@59 | 106 | # Load the class files. |
paul@59 | 107 | |
paul@59 | 108 | class_files = {} |
paul@39 | 109 | for class_filename in glob.glob(os.path.join(filename, "*" + os.extsep + "class")): |
paul@59 | 110 | print "Loading class", class_filename |
paul@39 | 111 | f = open(class_filename, "rb") |
paul@39 | 112 | s = f.read() |
paul@39 | 113 | f.close() |
paul@39 | 114 | class_file = classfile.ClassFile(s) |
paul@59 | 115 | class_files[str(class_file.this_class.get_name())] = class_file |
paul@59 | 116 | |
paul@59 | 117 | # Get an index of the class files. |
paul@59 | 118 | |
paul@59 | 119 | class_file_index = class_files.keys() |
paul@59 | 120 | |
paul@59 | 121 | # NOTE: Unnecessary sorting for test purposes. |
paul@59 | 122 | |
paul@59 | 123 | class_file_index.sort() |
paul@59 | 124 | |
paul@59 | 125 | # Now go through the classes arranging them in a safe loading order. |
paul@59 | 126 | |
paul@59 | 127 | position = 0 |
paul@59 | 128 | while position < len(class_file_index): |
paul@59 | 129 | class_name = class_file_index[position] |
paul@59 | 130 | super_class_name = str(class_files[class_name].super_class.get_name()) |
paul@59 | 131 | |
paul@59 | 132 | # Discover whether the superclass appears later. |
paul@59 | 133 | |
paul@59 | 134 | try: |
paul@59 | 135 | super_class_position = class_file_index.index(super_class_name) |
paul@59 | 136 | if super_class_position > position: |
paul@59 | 137 | |
paul@59 | 138 | # If the superclass appears later, swap this class and the |
paul@59 | 139 | # superclass, then process the superclass. |
paul@59 | 140 | |
paul@59 | 141 | class_file_index[position] = super_class_name |
paul@59 | 142 | class_file_index[super_class_position] = class_name |
paul@59 | 143 | continue |
paul@59 | 144 | |
paul@59 | 145 | except ValueError: |
paul@59 | 146 | pass |
paul@59 | 147 | |
paul@59 | 148 | position += 1 |
paul@59 | 149 | |
paul@59 | 150 | class_files = [class_files[class_name] for class_name in class_file_index] |
paul@59 | 151 | |
paul@59 | 152 | for class_file in class_files: |
paul@39 | 153 | translator = bytecode.ClassTranslator(class_file) |
paul@39 | 154 | cls = translator.process(global_names) |
paul@39 | 155 | module.__dict__[cls.__name__] = cls |
paul@62 | 156 | classes.append((cls, class_file)) |
paul@39 | 157 | |
paul@56 | 158 | # Finally, call __clinit__ methods for all relevant classes. |
paul@56 | 159 | |
paul@62 | 160 | for cls, class_file in classes: |
paul@64 | 161 | if hasattr(cls, "__clinit__"): |
paul@64 | 162 | cls.__clinit__() |
paul@56 | 163 | |
paul@39 | 164 | return module |
paul@39 | 165 | |
paul@52 | 166 | importer = ihooks.ModuleImporter(loader=ClassLoader(hooks=ClassHooks())) |
paul@39 | 167 | importer.install() |
paul@39 | 168 | |
paul@39 | 169 | # vim: tabstop=4 expandtab shiftwidth=4 |