javaclass

Change of classhook.py

136:754d36821fc8
classhook.py
     1.1 --- a/classhook.py	Fri Jan 21 17:04:41 2005 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,384 +0,0 @@
     1.4 -#!/usr/bin/env python
     1.5 -
     1.6 -import ihooks # for the import machinery
     1.7 -import os, glob # for getting suitably-named files
     1.8 -from imp import PY_SOURCE, PKG_DIRECTORY, C_BUILTIN # import machinery magic
     1.9 -import classfile, bytecode # Java class support
    1.10 -import zipfile # for Java archive inspection
    1.11 -
    1.12 -# NOTE: Arbitrary constants pulled from thin air.
    1.13 -
    1.14 -JAVA_PACKAGE = 20041113
    1.15 -JAVA_CLASS = 20041114
    1.16 -JAVA_ARCHIVE = 20041115
    1.17 -
    1.18 -class ClassHooks(ihooks.Hooks):
    1.19 -
    1.20 -    "A filesystem hooks class providing information about supported files."
    1.21 -
    1.22 -    def get_suffixes(self):
    1.23 -
    1.24 -        "Return the recognised suffixes."
    1.25 -
    1.26 -        return [("", "", JAVA_PACKAGE), (os.extsep + "jar", "r", JAVA_ARCHIVE)] + ihooks.Hooks.get_suffixes(self)
    1.27 -
    1.28 -    def path_isdir(self, x, archive=None):
    1.29 -
    1.30 -        "Return whether 'x' is a directory in the given 'archive'."
    1.31 -
    1.32 -        if archive is None:
    1.33 -            return ihooks.Hooks.path_isdir(self, x)
    1.34 -
    1.35 -        return self._get_dirname(x) in archive.namelist()
    1.36 -
    1.37 -    def _get_dirname(self, x):
    1.38 -
    1.39 -        """
    1.40 -        Return the directory name for 'x'.
    1.41 -        In zip files, the presence of "/" seems to indicate a directory.
    1.42 -        """
    1.43 -
    1.44 -        if x.endswith("/"):
    1.45 -            return x
    1.46 -        else:
    1.47 -            return x + "/"
    1.48 -
    1.49 -    def listdir(self, x, archive=None):
    1.50 -
    1.51 -        "Return the contents of the directory 'x' in the given 'archive'."
    1.52 -
    1.53 -        if archive is None:
    1.54 -            return ihooks.Hooks.listdir(self, x)
    1.55 -
    1.56 -        x = self._get_dirname(x)
    1.57 -        l = []
    1.58 -        for path in archive.namelist():
    1.59 -
    1.60 -            # Find out if the path is within the given directory.
    1.61 -
    1.62 -            if path != x and path.startswith(x):
    1.63 -
    1.64 -                # Get the path below the given directory.
    1.65 -
    1.66 -                subpath = path[len(x):]
    1.67 -
    1.68 -                # Find out whether the path is an object in the current directory.
    1.69 -
    1.70 -                if subpath.count("/") == 0 or subpath.count("/") == 1 and subpath.endswith("/"):
    1.71 -                    l.append(subpath)
    1.72 -
    1.73 -        return l
    1.74 -
    1.75 -    def matching(self, dir, extension, archive=None):
    1.76 -
    1.77 -        """
    1.78 -        Return the matching files in the given directory 'dir' having the given
    1.79 -        'extension' within the given 'archive'. Produce a list containing full
    1.80 -        paths as opposed to simple filenames.
    1.81 -        """
    1.82 -
    1.83 -        if archive is None:
    1.84 -            return glob.glob(self.path_join(dir, "*" + extension))
    1.85 -
    1.86 -        dir = self._get_dirname(dir)
    1.87 -        l = []
    1.88 -        for path in self.listdir(dir, archive):
    1.89 -            if path.endswith(extension):
    1.90 -                l.append(self.path_join(dir, path))
    1.91 -        return l
    1.92 -
    1.93 -    def read(self, filename, archive=None):
    1.94 -
    1.95 -        """
    1.96 -        Return the contents of the file with the given 'filename' in the given
    1.97 -        'archive'.
    1.98 -        """
    1.99 -
   1.100 -        if archive is None:
   1.101 -            f = open(filename, "rb")
   1.102 -            s = f.read()
   1.103 -            f.close()
   1.104 -            return s
   1.105 -        return archive.read(filename)
   1.106 -
   1.107 -class ClassLoader(ihooks.ModuleLoader):
   1.108 -
   1.109 -    "A class providing support for searching directories for supported files."
   1.110 -
   1.111 -    def find_module(self, name, path=None):
   1.112 -
   1.113 -        """
   1.114 -        Find the module with the given 'name', using the given 'path' to locate
   1.115 -        it. Note that ModuleLoader.find_module is almost sufficient, but does
   1.116 -        not provide enough support for "package unions" where the root of a
   1.117 -        package hierarchy may appear in several places.
   1.118 -
   1.119 -        Return a list of locations (each being the "stuff" data structure used
   1.120 -        by load_module); this replaces the single "stuff" value or None returned
   1.121 -        by ModuleLoader.find_module.
   1.122 -        """
   1.123 -
   1.124 -        if path is None:
   1.125 -            path = [None] + self.default_path()
   1.126 -
   1.127 -        found_locations = []
   1.128 -
   1.129 -        for dir in path:
   1.130 -            stuff = self.find_module_in_dir(name, dir)
   1.131 -            if stuff:
   1.132 -                found_locations.append(stuff)
   1.133 -
   1.134 -        return found_locations
   1.135 -
   1.136 -    def find_module_in_dir(self, name, dir, allow_packages=1):
   1.137 -
   1.138 -        """
   1.139 -        Find the module with the given 'name' in the given directory 'dir'.
   1.140 -        Since Java packages/modules are directories containing class files,
   1.141 -        return the required information tuple only when the path constructed
   1.142 -        from 'dir' and 'name' refers to a directory containing class files.
   1.143 -        """
   1.144 -
   1.145 -        result = ihooks.ModuleLoader.find_module_in_dir(self, name, dir, allow_packages)
   1.146 -        if result is not None:
   1.147 -            return result
   1.148 -
   1.149 -        # An archive may be opened.
   1.150 -
   1.151 -        archive = None
   1.152 -
   1.153 -        # Provide a special name for the current directory.
   1.154 -
   1.155 -        if name == "__this__":
   1.156 -            if dir == None:
   1.157 -                return (None, ".", ("", "", JAVA_PACKAGE))
   1.158 -            else:
   1.159 -                return None
   1.160 -
   1.161 -        # Where no directory is given, return failure immediately.
   1.162 -
   1.163 -        elif dir is None:
   1.164 -            return None
   1.165 -
   1.166 -        # Detect archives.
   1.167 -
   1.168 -        else:
   1.169 -            archive, archive_path, path = self._get_archive_and_path(dir, name)
   1.170 -
   1.171 -        #print "Processing name", name, "in", dir, "producing", path, "within archive", archive
   1.172 -
   1.173 -        if self._find_module_at_path(path, archive):
   1.174 -            if archive is not None:
   1.175 -                return (archive, archive_path + ":" + path, (os.extsep + "jar", "r", JAVA_ARCHIVE))
   1.176 -            else:
   1.177 -                return (None, path, ("", "", JAVA_PACKAGE))
   1.178 -        else:
   1.179 -            return None
   1.180 -
   1.181 -    def _get_archive_and_path(self, dir, name):
   1.182 -        parts = dir.split(":")
   1.183 -        archive_path = parts[0]
   1.184 -
   1.185 -        # Archives may include an internal path, but will in any case have
   1.186 -        # a primary part ending in .jar.
   1.187 -
   1.188 -        if archive_path.endswith(os.extsep + "jar"):
   1.189 -            archive = zipfile.ZipFile(archive_path, "r")
   1.190 -            path = self.hooks.path_join(":".join(parts[1:]), name)
   1.191 -
   1.192 -        # Otherwise, produce a filesystem-based path.
   1.193 -
   1.194 -        else:
   1.195 -            archive = None
   1.196 -            path = self.hooks.path_join(dir, name)
   1.197 -
   1.198 -        return archive, archive_path, path
   1.199 -
   1.200 -    def _get_path_in_archive(self, path):
   1.201 -        parts = path.split(":")
   1.202 -        if len(parts) == 1:
   1.203 -            return parts[0]
   1.204 -        else:
   1.205 -            return ":".join(parts[1:])
   1.206 -
   1.207 -    def _find_module_at_path(self, path, archive):
   1.208 -        if self.hooks.path_isdir(path, archive):
   1.209 -            #print "Looking in", path, "using archive", archive
   1.210 -
   1.211 -            # Look for classes in the directory.
   1.212 -
   1.213 -            if len(self.hooks.matching(path, os.extsep + "class", archive)) != 0:
   1.214 -                return 1
   1.215 -
   1.216 -            # Otherwise permit importing where directories containing classes exist.
   1.217 -
   1.218 -            #print "Filenames are", self.hooks.listdir(path, archive)
   1.219 -            for filename in self.hooks.listdir(path, archive):
   1.220 -                pathname = self.hooks.path_join(path, filename)
   1.221 -                result = self._find_module_at_path(pathname, archive)
   1.222 -                if result is not None:
   1.223 -                    return result
   1.224 -
   1.225 -        return 0
   1.226 -
   1.227 -    def load_module(self, name, stuff):
   1.228 -
   1.229 -        """
   1.230 -        Load the module with the given 'name', with a list of 'stuff' items,
   1.231 -        each of which describes the location of the module and is a tuple of the
   1.232 -        form (file, filename, (suffix, mode, data type)).
   1.233 -
   1.234 -        Return a module object or raise an ImportError if a problem occurred in
   1.235 -        the import operation.
   1.236 -
   1.237 -        Note that the 'stuff' parameter is a list and not a single item as in
   1.238 -        ModuleLoader.load_module. This should still work, however, since the
   1.239 -        find_module method produces such a list.
   1.240 -        """
   1.241 -
   1.242 -        # Set up the module.
   1.243 -        # A union of all locations is placed in the module's path.
   1.244 -
   1.245 -        module = self.hooks.add_module(name)
   1.246 -        module.__path__ = [item_filename for (item_archive, item_filename, item_info) in stuff]
   1.247 -
   1.248 -        # Just go into each package and find the class files.
   1.249 -
   1.250 -        for stuff_item in stuff:
   1.251 -
   1.252 -            # Extract the details, delegating loading responsibility to the
   1.253 -            # default loader where appropriate.
   1.254 -            # NOTE: Should we not be using some saved loader remembered upon
   1.255 -            # NOTE: installation?
   1.256 -
   1.257 -            archive, filename, info = stuff_item
   1.258 -            suffix, mode, datatype = info
   1.259 -            if datatype not in (JAVA_PACKAGE, JAVA_ARCHIVE):
   1.260 -                return ihooks.ModuleLoader.load_module(self, name, stuff_item)
   1.261 -
   1.262 -            #print "Loading", archive, filename, info
   1.263 -
   1.264 -            # Prepare a dictionary of globals.
   1.265 -
   1.266 -            global_names = module.__dict__
   1.267 -            global_names["__builtins__"] = __builtins__
   1.268 -
   1.269 -            # Get the real filename.
   1.270 -
   1.271 -            filename = self._get_path_in_archive(filename)
   1.272 -            #print "Real filename", filename
   1.273 -
   1.274 -            # Load the class files.
   1.275 -
   1.276 -            class_files = {}
   1.277 -            for class_filename in self.hooks.matching(filename, os.extsep + "class", archive):
   1.278 -                #print "Loading class", class_filename
   1.279 -                s = self.hooks.read(class_filename, archive)
   1.280 -                class_file = classfile.ClassFile(s)
   1.281 -                class_files[str(class_file.this_class.get_name())] = class_file
   1.282 -
   1.283 -            # Get an index of the class files.
   1.284 -
   1.285 -            class_file_index = class_files.keys()
   1.286 -
   1.287 -            # NOTE: Unnecessary sorting for test purposes.
   1.288 -
   1.289 -            class_file_index.sort()
   1.290 -
   1.291 -            # Now go through the classes arranging them in a safe loading order.
   1.292 -
   1.293 -            position = 0
   1.294 -            while position < len(class_file_index):
   1.295 -                class_name = class_file_index[position]
   1.296 -                super_class_name = str(class_files[class_name].super_class.get_name())
   1.297 -
   1.298 -                # Discover whether the superclass appears later.
   1.299 -
   1.300 -                try:
   1.301 -                    super_class_position = class_file_index.index(super_class_name)
   1.302 -                    if super_class_position > position:
   1.303 -
   1.304 -                        # If the superclass appears later, swap this class and the
   1.305 -                        # superclass, then process the superclass.
   1.306 -
   1.307 -                        class_file_index[position] = super_class_name
   1.308 -                        class_file_index[super_class_position] = class_name
   1.309 -                        continue
   1.310 -
   1.311 -                except ValueError:
   1.312 -                    pass
   1.313 -
   1.314 -                position += 1
   1.315 -
   1.316 -            # Process each class file, producing a genuine Python class.
   1.317 -            # Create the classes, but establish a proper initialisation order.
   1.318 -
   1.319 -            class_file_init_index = []
   1.320 -            class_file_init = {}
   1.321 -
   1.322 -            for class_name in class_file_index:
   1.323 -                #print "* Class", class_name
   1.324 -                class_file = class_files[class_name]
   1.325 -                translator = bytecode.ClassTranslator(class_file)
   1.326 -                cls, external_names = translator.process(global_names)
   1.327 -                module.__dict__[cls.__name__] = cls
   1.328 -
   1.329 -                # Process external names.
   1.330 -
   1.331 -                this_class_name_parts = class_file.this_class.get_python_name().split(".")
   1.332 -                this_class_module, this_class_name = this_class_name_parts[:-1], this_class_name_parts[-1]
   1.333 -
   1.334 -                for external_name in external_names:
   1.335 -                    #print "* Name", external_name
   1.336 -                    external_name_parts = external_name.split(".")
   1.337 -                    external_class_module, external_class_name = external_name_parts[:-1], external_name_parts[-1]
   1.338 -
   1.339 -                    # Names not local to this package need importing.
   1.340 -
   1.341 -                    if len(external_name_parts) > 1 and this_class_module != external_class_module:
   1.342 -
   1.343 -                        external_module_name = ".".join(external_class_module)
   1.344 -                        #print "* Importing", external_module_name
   1.345 -                        obj = __import__(external_module_name, global_names, {}, [])
   1.346 -                        global_names[external_name_parts[0]] = obj
   1.347 -
   1.348 -                    # Names local to this package may affect initialisation order.
   1.349 -
   1.350 -                    elif external_class_name not in class_file_init_index:
   1.351 -                        try:
   1.352 -                            this_class_name_index = class_file_init_index.index(this_class_name)
   1.353 -
   1.354 -                            # Either insert this name before the current class's
   1.355 -                            # name.
   1.356 -
   1.357 -                            #print "* Inserting", external_class_name
   1.358 -                            class_file_init_index.insert(this_class_name_index, external_class_name)
   1.359 -
   1.360 -                        except ValueError:
   1.361 -
   1.362 -                            # Or add this name in anticipation of the current
   1.363 -                            # class's name appearing.
   1.364 -
   1.365 -                            #print "* Including", external_class_name
   1.366 -                            class_file_init_index.append(external_class_name)
   1.367 -
   1.368 -                # Add this class name to the initialisation index.
   1.369 -
   1.370 -                if class_name not in class_file_init_index:
   1.371 -                    class_file_init_index.append(this_class_name)
   1.372 -                class_file_init[this_class_name] = (cls, class_file)
   1.373 -
   1.374 -            # Finally, call __clinit__ methods for all relevant classes.
   1.375 -
   1.376 -            #print "** Initialisation order", class_file_init_index
   1.377 -            for class_name in class_file_init_index:
   1.378 -                cls, class_file = class_file_init[class_name]
   1.379 -                #print "**", cls, class_file
   1.380 -                if hasattr(cls, "__clinit__"):
   1.381 -                    eval(cls.__clinit__.func_code, global_names)
   1.382 -
   1.383 -        return module
   1.384 -
   1.385 -ihooks.ModuleImporter(loader=ClassLoader(hooks=ClassHooks())).install()
   1.386 -
   1.387 -# vim: tabstop=4 expandtab shiftwidth=4