paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Import logic. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, |
paul@510 | 7 | 2014, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 8 | |
paul@0 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 12 | version. |
paul@0 | 13 | |
paul@0 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 17 | details. |
paul@0 | 18 | |
paul@0 | 19 | You should have received a copy of the GNU General Public License along with |
paul@0 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 21 | """ |
paul@0 | 22 | |
paul@0 | 23 | from errors import ProgramError |
paul@0 | 24 | from os.path import exists, extsep, getmtime, join |
paul@0 | 25 | from os import listdir, makedirs, remove |
paul@724 | 26 | from common import init_item, order_dependencies, readfile, writefile |
paul@13 | 27 | from modules import CachedModule |
paul@0 | 28 | from referencing import Reference |
paul@0 | 29 | import inspector |
paul@0 | 30 | import sys |
paul@0 | 31 | |
paul@0 | 32 | class Importer: |
paul@0 | 33 | |
paul@0 | 34 | "An import machine, searching for and loading modules." |
paul@0 | 35 | |
paul@526 | 36 | special_attributes = ("__args__", "__file__", "__fn__", "__name__", "__parent__") |
paul@526 | 37 | |
paul@558 | 38 | def __init__(self, path, cache=None, verbose=False, warnings=None): |
paul@0 | 39 | |
paul@0 | 40 | """ |
paul@0 | 41 | Initialise the importer with the given search 'path' - a list of |
paul@0 | 42 | directories to search for Python modules. |
paul@0 | 43 | |
paul@0 | 44 | The optional 'cache' should be the name of a directory used to store |
paul@0 | 45 | cached module information. |
paul@0 | 46 | |
paul@0 | 47 | The optional 'verbose' parameter causes output concerning the activities |
paul@0 | 48 | of the object to be produced if set to a true value (not the default). |
paul@558 | 49 | |
paul@558 | 50 | The optional 'warnings' parameter may indicate classes of warnings to be |
paul@558 | 51 | produced. |
paul@0 | 52 | """ |
paul@0 | 53 | |
paul@0 | 54 | self.path = path |
paul@0 | 55 | self.cache = cache |
paul@0 | 56 | self.verbose = verbose |
paul@558 | 57 | self.warnings = warnings |
paul@0 | 58 | |
paul@41 | 59 | # Module importing queue, required modules, removed modules and active |
paul@41 | 60 | # modules in the final program. |
paul@41 | 61 | |
paul@12 | 62 | self.to_import = set() |
paul@16 | 63 | self.required = set(["__main__"]) |
paul@24 | 64 | self.removed = {} |
paul@41 | 65 | self.modules = {} |
paul@12 | 66 | |
paul@41 | 67 | # Module relationships and invalidated cached modules. |
paul@41 | 68 | |
paul@12 | 69 | self.accessing_modules = {} |
paul@0 | 70 | self.invalidated = set() |
paul@0 | 71 | |
paul@425 | 72 | # Object relationships and dependencies. |
paul@425 | 73 | |
paul@425 | 74 | self.depends = {} |
paul@427 | 75 | self.module_depends = {} |
paul@425 | 76 | |
paul@41 | 77 | # Basic program information. |
paul@41 | 78 | |
paul@0 | 79 | self.objects = {} |
paul@0 | 80 | self.classes = {} |
paul@0 | 81 | self.function_parameters = {} |
paul@0 | 82 | self.function_defaults = {} |
paul@109 | 83 | self.function_locals = {} |
paul@0 | 84 | |
paul@41 | 85 | # Unresolved names. |
paul@41 | 86 | |
paul@41 | 87 | self.missing = set() |
paul@41 | 88 | |
paul@0 | 89 | # Derived information. |
paul@0 | 90 | |
paul@0 | 91 | self.subclasses = {} |
paul@0 | 92 | |
paul@0 | 93 | # Attributes of different object types. |
paul@0 | 94 | |
paul@0 | 95 | self.all_class_attrs = {} |
paul@0 | 96 | self.all_instance_attrs = {} |
paul@0 | 97 | self.all_instance_attr_constants = {} |
paul@0 | 98 | self.all_combined_attrs = {} |
paul@0 | 99 | self.all_module_attrs = {} |
paul@0 | 100 | self.all_shadowed_attrs = {} |
paul@0 | 101 | |
paul@0 | 102 | # References to external names and aliases within program units. |
paul@0 | 103 | |
paul@0 | 104 | self.all_name_references = {} |
paul@0 | 105 | self.all_initialised_names = {} |
paul@0 | 106 | self.all_aliased_names = {} |
paul@0 | 107 | |
paul@0 | 108 | # General attribute accesses. |
paul@0 | 109 | |
paul@0 | 110 | self.all_attr_accesses = {} |
paul@0 | 111 | self.all_const_accesses = {} |
paul@0 | 112 | self.all_attr_access_modifiers = {} |
paul@0 | 113 | |
paul@0 | 114 | # Constant literals and values. |
paul@0 | 115 | |
paul@0 | 116 | self.all_constants = {} |
paul@0 | 117 | self.all_constant_values = {} |
paul@0 | 118 | |
paul@712 | 119 | # Return values. |
paul@712 | 120 | |
paul@712 | 121 | self.all_return_values = {} |
paul@712 | 122 | |
paul@0 | 123 | self.make_cache() |
paul@0 | 124 | |
paul@558 | 125 | def give_warning(self, name): |
paul@558 | 126 | |
paul@558 | 127 | "Return whether the indicated warning 'name' should be given." |
paul@558 | 128 | |
paul@558 | 129 | return self.warnings and (name in self.warnings or "all" in self.warnings) |
paul@558 | 130 | |
paul@0 | 131 | def make_cache(self): |
paul@441 | 132 | |
paul@441 | 133 | "Make a cache directory if it does not already exist." |
paul@441 | 134 | |
paul@0 | 135 | if self.cache and not exists(self.cache): |
paul@0 | 136 | makedirs(self.cache) |
paul@0 | 137 | |
paul@0 | 138 | def check_cache(self, details): |
paul@0 | 139 | |
paul@0 | 140 | """ |
paul@0 | 141 | Check whether the cache applies for the given 'details', invalidating it |
paul@0 | 142 | if it does not. |
paul@0 | 143 | """ |
paul@0 | 144 | |
paul@0 | 145 | recorded_details = self.get_cache_details() |
paul@0 | 146 | |
paul@0 | 147 | if recorded_details != details: |
paul@0 | 148 | self.remove_cache() |
paul@0 | 149 | |
paul@0 | 150 | writefile(self.get_cache_details_filename(), details) |
paul@0 | 151 | |
paul@0 | 152 | def get_cache_details_filename(self): |
paul@0 | 153 | |
paul@0 | 154 | "Return the filename for the cache details." |
paul@0 | 155 | |
paul@0 | 156 | return join(self.cache, "$details") |
paul@0 | 157 | |
paul@0 | 158 | def get_cache_details(self): |
paul@0 | 159 | |
paul@0 | 160 | "Return details of the cache." |
paul@0 | 161 | |
paul@0 | 162 | details_filename = self.get_cache_details_filename() |
paul@0 | 163 | |
paul@0 | 164 | if not exists(details_filename): |
paul@0 | 165 | return None |
paul@0 | 166 | else: |
paul@0 | 167 | return readfile(details_filename) |
paul@0 | 168 | |
paul@0 | 169 | def remove_cache(self): |
paul@0 | 170 | |
paul@0 | 171 | "Remove the contents of the cache." |
paul@0 | 172 | |
paul@0 | 173 | for filename in listdir(self.cache): |
paul@0 | 174 | remove(join(self.cache, filename)) |
paul@0 | 175 | |
paul@0 | 176 | def to_cache(self): |
paul@0 | 177 | |
paul@0 | 178 | "Write modules to the cache." |
paul@0 | 179 | |
paul@0 | 180 | if self.cache: |
paul@0 | 181 | for module_name, module in self.modules.items(): |
paul@0 | 182 | module.to_cache(join(self.cache, module_name)) |
paul@0 | 183 | |
paul@0 | 184 | # Object retrieval and storage. |
paul@0 | 185 | |
paul@0 | 186 | def get_object(self, name): |
paul@0 | 187 | |
paul@0 | 188 | """ |
paul@0 | 189 | Return a reference for the given 'name' or None if no such object |
paul@0 | 190 | exists. |
paul@0 | 191 | """ |
paul@0 | 192 | |
paul@0 | 193 | return self.objects.get(name) |
paul@0 | 194 | |
paul@0 | 195 | def set_object(self, name, value=None): |
paul@0 | 196 | |
paul@0 | 197 | "Set the object with the given 'name' and the given 'value'." |
paul@0 | 198 | |
paul@0 | 199 | if isinstance(value, Reference): |
paul@0 | 200 | ref = value.alias(name) |
paul@0 | 201 | else: |
paul@0 | 202 | ref = Reference(value, name) |
paul@0 | 203 | |
paul@0 | 204 | self.objects[name] = ref |
paul@0 | 205 | |
paul@27 | 206 | # Identification of both stored object names and name references. |
paul@27 | 207 | |
paul@27 | 208 | def identify(self, name): |
paul@27 | 209 | |
paul@27 | 210 | "Identify 'name' using stored object and external name records." |
paul@27 | 211 | |
paul@27 | 212 | return self.objects.get(name) or self.all_name_references.get(name) |
paul@27 | 213 | |
paul@0 | 214 | # Indirect object retrieval. |
paul@0 | 215 | |
paul@0 | 216 | def get_attributes(self, ref, attrname): |
paul@0 | 217 | |
paul@0 | 218 | """ |
paul@0 | 219 | Return attributes provided by 'ref' for 'attrname'. Class attributes |
paul@0 | 220 | may be provided by instances. |
paul@0 | 221 | """ |
paul@0 | 222 | |
paul@0 | 223 | kind = ref.get_kind() |
paul@0 | 224 | if kind == "<class>": |
paul@0 | 225 | ref = self.get_class_attribute(ref.get_origin(), attrname) |
paul@0 | 226 | return ref and set([ref]) or set() |
paul@0 | 227 | elif kind == "<instance>": |
paul@0 | 228 | return self.get_combined_attributes(ref.get_origin(), attrname) |
paul@0 | 229 | elif kind == "<module>": |
paul@0 | 230 | ref = self.get_module_attribute(ref.get_origin(), attrname) |
paul@0 | 231 | return ref and set([ref]) or set() |
paul@0 | 232 | else: |
paul@0 | 233 | return set() |
paul@0 | 234 | |
paul@0 | 235 | def get_class_attribute(self, object_type, attrname): |
paul@0 | 236 | |
paul@0 | 237 | "Return from 'object_type' the details of class attribute 'attrname'." |
paul@0 | 238 | |
paul@324 | 239 | attrs = self.all_class_attrs.get(object_type) |
paul@324 | 240 | attr = attrs and attrs.get(attrname) |
paul@0 | 241 | return attr and self.get_object(attr) |
paul@0 | 242 | |
paul@0 | 243 | def get_instance_attributes(self, object_type, attrname): |
paul@0 | 244 | |
paul@0 | 245 | """ |
paul@0 | 246 | Return from 'object_type' the details of instance attribute 'attrname'. |
paul@0 | 247 | """ |
paul@0 | 248 | |
paul@0 | 249 | consts = self.all_instance_attr_constants.get(object_type) |
paul@0 | 250 | attrs = set() |
paul@0 | 251 | for attr in self.all_instance_attrs[object_type].get(attrname, []): |
paul@0 | 252 | attrs.add(consts and consts.get(attrname) or Reference("<var>", attr)) |
paul@0 | 253 | return attrs |
paul@0 | 254 | |
paul@0 | 255 | def get_combined_attributes(self, object_type, attrname): |
paul@0 | 256 | |
paul@0 | 257 | """ |
paul@0 | 258 | Return from 'object_type' the details of class or instance attribute |
paul@0 | 259 | 'attrname'. |
paul@0 | 260 | """ |
paul@0 | 261 | |
paul@0 | 262 | ref = self.get_class_attribute(object_type, attrname) |
paul@0 | 263 | refs = ref and set([ref]) or set() |
paul@0 | 264 | refs.update(self.get_instance_attributes(object_type, attrname)) |
paul@0 | 265 | return refs |
paul@0 | 266 | |
paul@0 | 267 | def get_module_attribute(self, object_type, attrname): |
paul@0 | 268 | |
paul@0 | 269 | "Return from 'object_type' the details of module attribute 'attrname'." |
paul@0 | 270 | |
paul@0 | 271 | if attrname in self.all_module_attrs[object_type]: |
paul@0 | 272 | return self.get_object("%s.%s" % (object_type, attrname)) |
paul@0 | 273 | else: |
paul@0 | 274 | return None |
paul@0 | 275 | |
paul@96 | 276 | # Convenience methods for deducing which kind of object provided an |
paul@96 | 277 | # attribute. |
paul@96 | 278 | |
paul@96 | 279 | def get_attribute_provider(self, ref, attrname): |
paul@96 | 280 | |
paul@96 | 281 | """ |
paul@96 | 282 | Return the kind of provider of the attribute accessed via 'ref' using |
paul@96 | 283 | 'attrname'. |
paul@96 | 284 | """ |
paul@96 | 285 | |
paul@96 | 286 | kind = ref.get_kind() |
paul@96 | 287 | |
paul@96 | 288 | if kind in ["<class>", "<module>"]: |
paul@96 | 289 | return kind |
paul@96 | 290 | else: |
paul@96 | 291 | return self.get_instance_attribute_provider(ref.get_origin(), attrname) |
paul@96 | 292 | |
paul@96 | 293 | def get_instance_attribute_provider(self, object_type, attrname): |
paul@96 | 294 | |
paul@96 | 295 | """ |
paul@96 | 296 | Return the kind of provider of the attribute accessed via an instance of |
paul@96 | 297 | 'object_type' using 'attrname'. |
paul@96 | 298 | """ |
paul@96 | 299 | |
paul@96 | 300 | if self.get_class_attribute(object_type, attrname): |
paul@96 | 301 | return "<class>" |
paul@96 | 302 | else: |
paul@96 | 303 | return "<instance>" |
paul@96 | 304 | |
paul@0 | 305 | # Module management. |
paul@0 | 306 | |
paul@16 | 307 | def queue_module(self, name, accessor, required=False): |
paul@12 | 308 | |
paul@12 | 309 | """ |
paul@12 | 310 | Queue the module with the given 'name' for import from the given |
paul@16 | 311 | 'accessor' module. If 'required' is true (it is false by default), the |
paul@16 | 312 | module will be required in the final program. |
paul@12 | 313 | """ |
paul@12 | 314 | |
paul@12 | 315 | if not self.modules.has_key(name): |
paul@12 | 316 | self.to_import.add(name) |
paul@12 | 317 | |
paul@16 | 318 | if required: |
paul@16 | 319 | self.required.add(name) |
paul@16 | 320 | |
paul@12 | 321 | init_item(self.accessing_modules, name, set) |
paul@16 | 322 | self.accessing_modules[name].add(accessor.name) |
paul@12 | 323 | |
paul@0 | 324 | def get_modules(self): |
paul@0 | 325 | |
paul@0 | 326 | "Return all modules known to the importer." |
paul@0 | 327 | |
paul@0 | 328 | return self.modules.values() |
paul@0 | 329 | |
paul@12 | 330 | def get_module(self, name): |
paul@0 | 331 | |
paul@0 | 332 | "Return the module with the given 'name'." |
paul@0 | 333 | |
paul@0 | 334 | if not self.modules.has_key(name): |
paul@0 | 335 | return None |
paul@0 | 336 | |
paul@12 | 337 | return self.modules[name] |
paul@0 | 338 | |
paul@0 | 339 | # Program operations. |
paul@0 | 340 | |
paul@0 | 341 | def initialise(self, filename, reset=False): |
paul@0 | 342 | |
paul@0 | 343 | """ |
paul@0 | 344 | Initialise a program whose main module is 'filename', resetting the |
paul@0 | 345 | cache if 'reset' is true. Return the main module. |
paul@0 | 346 | """ |
paul@0 | 347 | |
paul@0 | 348 | if reset: |
paul@0 | 349 | self.remove_cache() |
paul@0 | 350 | self.check_cache(filename) |
paul@0 | 351 | |
paul@0 | 352 | # Load the program itself. |
paul@0 | 353 | |
paul@0 | 354 | m = self.load_from_file(filename) |
paul@0 | 355 | |
paul@12 | 356 | # Load any queued modules. |
paul@12 | 357 | |
paul@12 | 358 | while self.to_import: |
paul@12 | 359 | for name in list(self.to_import): # avoid mutation issue |
paul@12 | 360 | self.load(name) |
paul@12 | 361 | |
paul@12 | 362 | # Resolve dependencies between modules. |
paul@12 | 363 | |
paul@12 | 364 | self.resolve() |
paul@12 | 365 | |
paul@16 | 366 | # Record the type of all classes. |
paul@16 | 367 | |
paul@16 | 368 | self.type_ref = self.get_object("__builtins__.type") |
paul@16 | 369 | |
paul@0 | 370 | # Resolve dependencies within the program. |
paul@0 | 371 | |
paul@12 | 372 | for module in self.modules.values(): |
paul@12 | 373 | module.complete() |
paul@0 | 374 | |
paul@16 | 375 | # Remove unneeded modules. |
paul@16 | 376 | |
paul@16 | 377 | all_modules = self.modules.items() |
paul@16 | 378 | |
paul@16 | 379 | for name, module in all_modules: |
paul@16 | 380 | if name not in self.required: |
paul@16 | 381 | module.unpropagate() |
paul@16 | 382 | del self.modules[name] |
paul@24 | 383 | self.removed[name] = module |
paul@16 | 384 | |
paul@68 | 385 | # Collect redundant objects. |
paul@68 | 386 | |
paul@68 | 387 | for module in self.removed.values(): |
paul@68 | 388 | module.collect() |
paul@68 | 389 | |
paul@68 | 390 | # Assert module objects where aliases have been removed. |
paul@68 | 391 | |
paul@68 | 392 | for name in self.required: |
paul@68 | 393 | if not self.objects.has_key(name): |
paul@68 | 394 | self.objects[name] = Reference("<module>", name) |
paul@68 | 395 | |
paul@0 | 396 | return m |
paul@0 | 397 | |
paul@0 | 398 | def finalise(self): |
paul@0 | 399 | |
paul@41 | 400 | """ |
paul@41 | 401 | Finalise the inspected program, returning whether the program could be |
paul@41 | 402 | finalised. |
paul@41 | 403 | """ |
paul@41 | 404 | |
paul@358 | 405 | self.finalise_classes() |
paul@423 | 406 | self.add_init_dependencies() |
paul@358 | 407 | self.to_cache() |
paul@358 | 408 | |
paul@41 | 409 | if self.missing: |
paul@41 | 410 | return False |
paul@0 | 411 | |
paul@0 | 412 | self.set_class_types() |
paul@0 | 413 | self.define_instantiators() |
paul@0 | 414 | self.collect_constants() |
paul@0 | 415 | |
paul@41 | 416 | return True |
paul@41 | 417 | |
paul@12 | 418 | # Supporting operations. |
paul@12 | 419 | |
paul@12 | 420 | def resolve(self): |
paul@12 | 421 | |
paul@12 | 422 | "Resolve dependencies between modules." |
paul@12 | 423 | |
paul@35 | 424 | self.waiting = {} |
paul@35 | 425 | |
paul@35 | 426 | for module in self.modules.values(): |
paul@35 | 427 | |
paul@35 | 428 | # Resolve all deferred references in each module. |
paul@12 | 429 | |
paul@391 | 430 | original_deferred = [] |
paul@391 | 431 | |
paul@35 | 432 | for ref in module.deferred: |
paul@391 | 433 | |
paul@391 | 434 | # Retain original references for caching. |
paul@391 | 435 | |
paul@391 | 436 | original_deferred.append(ref.copy()) |
paul@391 | 437 | |
paul@391 | 438 | # Update references throughout the program. |
paul@391 | 439 | |
paul@35 | 440 | found = self.find_dependency(ref) |
paul@35 | 441 | if not found: |
paul@41 | 442 | self.missing.add((module.name, ref.get_origin())) |
paul@35 | 443 | |
paul@35 | 444 | # Record the resolved names and identify required modules. |
paul@12 | 445 | |
paul@35 | 446 | else: |
paul@170 | 447 | # Find the providing module of this reference. |
paul@170 | 448 | # Where definitive details of the origin cannot be found, |
paul@170 | 449 | # identify the provider using the deferred reference. |
paul@170 | 450 | # NOTE: This may need to test for static origins. |
paul@170 | 451 | |
paul@170 | 452 | provider = self.get_module_provider(found.unresolved() and ref or found) |
paul@35 | 453 | ref.mutate(found) |
paul@35 | 454 | |
paul@186 | 455 | # Record any external dependency. |
paul@186 | 456 | |
paul@186 | 457 | if provider and provider != module.name: |
paul@186 | 458 | |
paul@543 | 459 | # Handle built-in modules accidentally referenced by |
paul@543 | 460 | # names. |
paul@543 | 461 | |
paul@543 | 462 | if provider == "__builtins__" and found.has_kind("<module>"): |
paul@543 | 463 | raise ProgramError("Name %s, used by %s, refers to module %s." % |
paul@543 | 464 | (found.leaf(), module.name, found.get_origin())) |
paul@543 | 465 | |
paul@186 | 466 | # Record the provider dependency. |
paul@16 | 467 | |
paul@35 | 468 | module.required.add(provider) |
paul@35 | 469 | self.accessing_modules[provider].add(module.name) |
paul@35 | 470 | |
paul@35 | 471 | # Postpone any inclusion of the provider until this |
paul@35 | 472 | # module becomes required. |
paul@12 | 473 | |
paul@35 | 474 | if module.name not in self.required: |
paul@35 | 475 | init_item(self.waiting, module.name, set) |
paul@35 | 476 | self.waiting[module.name].add(provider) |
paul@418 | 477 | if self.verbose: |
paul@418 | 478 | print >>sys.stderr, "Noting", provider, "for", ref, "from", module.name |
paul@35 | 479 | |
paul@35 | 480 | # Make this module required in the accessing module. |
paul@32 | 481 | |
paul@53 | 482 | elif provider not in self.required: |
paul@35 | 483 | self.required.add(provider) |
paul@53 | 484 | if self.verbose: |
paul@418 | 485 | print >>sys.stderr, "Requiring", provider, "for", ref, "from", module.name |
paul@35 | 486 | |
paul@425 | 487 | # Record a module ordering dependency. |
paul@425 | 488 | |
paul@429 | 489 | if not found.static() or self.is_dynamic_class(found) or self.is_dynamic_callable(found): |
paul@427 | 490 | self.add_module_dependency(module.name, provider) |
paul@425 | 491 | |
paul@425 | 492 | # Restore the original references so that they may be read back in |
paul@425 | 493 | # and produce the same results. |
paul@425 | 494 | |
paul@391 | 495 | module.deferred = original_deferred |
paul@391 | 496 | |
paul@38 | 497 | # Check modules again to see if they are now required and should now |
paul@38 | 498 | # cause the inclusion of other modules providing objects to the program. |
paul@38 | 499 | |
paul@35 | 500 | for module_name in self.waiting.keys(): |
paul@35 | 501 | self.require_providers(module_name) |
paul@16 | 502 | |
paul@423 | 503 | self.add_special_dependencies() |
paul@427 | 504 | self.add_module_dependencies() |
paul@419 | 505 | |
paul@35 | 506 | def require_providers(self, module_name): |
paul@38 | 507 | |
paul@38 | 508 | """ |
paul@38 | 509 | Test if 'module_name' is itself required and, if so, require modules |
paul@38 | 510 | containing objects provided to the module. |
paul@38 | 511 | """ |
paul@38 | 512 | |
paul@35 | 513 | if module_name in self.required and self.waiting.has_key(module_name): |
paul@35 | 514 | for provider in self.waiting[module_name]: |
paul@35 | 515 | if provider not in self.required: |
paul@35 | 516 | self.required.add(provider) |
paul@53 | 517 | if self.verbose: |
paul@53 | 518 | print >>sys.stderr, "Requiring", provider |
paul@35 | 519 | self.require_providers(provider) |
paul@32 | 520 | |
paul@423 | 521 | def add_special_dependencies(self): |
paul@423 | 522 | |
paul@423 | 523 | "Add dependencies due to the use of special names in namespaces." |
paul@423 | 524 | |
paul@423 | 525 | for module in self.modules.values(): |
paul@423 | 526 | for ref, paths in module.special.values(): |
paul@423 | 527 | for path in paths: |
paul@423 | 528 | self.add_dependency(path, ref.get_origin()) |
paul@423 | 529 | |
paul@423 | 530 | def add_init_dependencies(self): |
paul@423 | 531 | |
paul@423 | 532 | "Add dependencies related to object initialisation." |
paul@418 | 533 | |
paul@423 | 534 | for name in self.classes.keys(): |
paul@423 | 535 | if self.is_dynamic_class(name): |
paul@423 | 536 | |
paul@423 | 537 | # Make subclasses depend on any class with non-static |
paul@423 | 538 | # attributes, plus its module for the initialisation. |
paul@418 | 539 | |
paul@423 | 540 | for subclass in self.subclasses[name]: |
paul@423 | 541 | ref = Reference("<class>", subclass) |
paul@423 | 542 | self.add_dependency(subclass, name) |
paul@423 | 543 | self.add_dependency(subclass, self.get_module_provider(ref)) |
paul@423 | 544 | |
paul@423 | 545 | # Also make the class dependent on its module for |
paul@423 | 546 | # initialisation. |
paul@423 | 547 | |
paul@423 | 548 | ref = Reference("<class>", name) |
paul@423 | 549 | self.add_dependency(name, self.get_module_provider(ref)) |
paul@418 | 550 | |
paul@423 | 551 | for name in self.function_defaults.keys(): |
paul@423 | 552 | if self.is_dynamic_callable(name): |
paul@423 | 553 | |
paul@423 | 554 | # Make functions with defaults requiring initialisation depend |
paul@428 | 555 | # on the parent scope, if a function, or the module scope. |
paul@423 | 556 | |
paul@423 | 557 | ref = Reference("<function>", name) |
paul@428 | 558 | parent_ref = self.get_object(ref.parent()) |
paul@428 | 559 | |
paul@428 | 560 | # Function no longer present in the program. |
paul@428 | 561 | |
paul@428 | 562 | if not parent_ref: |
paul@428 | 563 | continue |
paul@428 | 564 | |
paul@428 | 565 | if parent_ref.has_kind("<class>"): |
paul@428 | 566 | parent = self.get_module_provider(parent_ref) |
paul@428 | 567 | else: |
paul@428 | 568 | parent = parent_ref.get_origin() |
paul@428 | 569 | |
paul@428 | 570 | self.add_dependency(name, parent) |
paul@423 | 571 | |
paul@427 | 572 | def add_module_dependencies(self): |
paul@427 | 573 | |
paul@427 | 574 | "Record module-based dependencies." |
paul@427 | 575 | |
paul@427 | 576 | for module_name, providers in self.module_depends.items(): |
paul@427 | 577 | if self.modules.has_key(module_name): |
paul@427 | 578 | for provider in providers: |
paul@427 | 579 | if self.modules.has_key(provider): |
paul@427 | 580 | self.add_dependency(module_name, provider) |
paul@427 | 581 | |
paul@423 | 582 | def add_dependency(self, path, origin): |
paul@423 | 583 | |
paul@423 | 584 | "Add dependency details for 'path' involving 'origin'." |
paul@423 | 585 | |
paul@427 | 586 | if origin and not origin.startswith("%s." % path): |
paul@423 | 587 | init_item(self.depends, path, set) |
paul@423 | 588 | self.depends[path].add(origin) |
paul@423 | 589 | |
paul@427 | 590 | def add_module_dependency(self, module_name, provider): |
paul@427 | 591 | |
paul@427 | 592 | "Add dependency details for 'module_name' involving 'provider'." |
paul@427 | 593 | |
paul@427 | 594 | if provider: |
paul@427 | 595 | init_item(self.module_depends, module_name, set) |
paul@427 | 596 | self.module_depends[module_name].add(provider) |
paul@427 | 597 | |
paul@427 | 598 | def condense_dependencies(self): |
paul@427 | 599 | |
paul@427 | 600 | """ |
paul@427 | 601 | Condense the dependencies by removing all functions that do not need |
paul@427 | 602 | initialisation. |
paul@427 | 603 | """ |
paul@427 | 604 | |
paul@427 | 605 | d = {} |
paul@427 | 606 | for path, depends in self.depends.items(): |
paul@427 | 607 | d[path] = {} |
paul@427 | 608 | d[path] = self.condense_dependency_entry(depends, d) |
paul@427 | 609 | |
paul@427 | 610 | self.depends = {} |
paul@427 | 611 | |
paul@427 | 612 | for path, depends in d.items(): |
paul@427 | 613 | if depends: |
paul@427 | 614 | self.depends[path] = depends |
paul@427 | 615 | |
paul@427 | 616 | def condense_dependency_entry(self, depends, d): |
paul@427 | 617 | l = set() |
paul@427 | 618 | for depend in depends: |
paul@427 | 619 | if self.modules.has_key(depend) or self.classes.has_key(depend) or \ |
paul@427 | 620 | self.is_dynamic_callable(depend): |
paul@427 | 621 | |
paul@427 | 622 | l.add(depend) |
paul@427 | 623 | else: |
paul@427 | 624 | deps = d.get(depend) |
paul@427 | 625 | if deps: |
paul@427 | 626 | l.update(self.condense_dependency_entry(deps, d)) |
paul@427 | 627 | return l |
paul@427 | 628 | |
paul@428 | 629 | def is_dynamic(self, ref): |
paul@428 | 630 | return not ref or not ref.static() and not ref.is_constant_alias() and not ref.is_predefined_value() |
paul@428 | 631 | |
paul@423 | 632 | def is_dynamic_class(self, name): |
paul@423 | 633 | |
paul@423 | 634 | """ |
paul@423 | 635 | Return whether 'name' refers to a class with members that must be |
paul@423 | 636 | initialised dynamically. |
paul@423 | 637 | """ |
paul@423 | 638 | |
paul@423 | 639 | attrs = self.all_class_attrs.get(name) |
paul@423 | 640 | |
paul@423 | 641 | if not attrs: |
paul@418 | 642 | return False |
paul@418 | 643 | |
paul@423 | 644 | for attrname, attr in attrs.items(): |
paul@423 | 645 | if attrname in self.special_attributes: |
paul@423 | 646 | continue |
paul@428 | 647 | ref = attr and self.get_object(attr) |
paul@428 | 648 | if self.is_dynamic(ref): |
paul@423 | 649 | return True |
paul@423 | 650 | |
paul@423 | 651 | return False |
paul@423 | 652 | |
paul@423 | 653 | def is_dynamic_callable(self, name): |
paul@334 | 654 | |
paul@334 | 655 | """ |
paul@423 | 656 | Return whether 'name' refers to a callable employing defaults that may |
paul@334 | 657 | need initialising before the callable can be used. |
paul@334 | 658 | """ |
paul@334 | 659 | |
paul@338 | 660 | # Find any defaults for the function or method. |
paul@338 | 661 | |
paul@423 | 662 | defaults = self.function_defaults.get(name) |
paul@338 | 663 | if not defaults: |
paul@338 | 664 | return False |
paul@338 | 665 | |
paul@338 | 666 | # Identify non-constant defaults. |
paul@338 | 667 | |
paul@338 | 668 | for name, ref in defaults: |
paul@428 | 669 | if self.is_dynamic(ref): |
paul@338 | 670 | return True |
paul@338 | 671 | |
paul@338 | 672 | return False |
paul@334 | 673 | |
paul@423 | 674 | def order_objects(self): |
paul@186 | 675 | |
paul@423 | 676 | "Produce an object initialisation ordering." |
paul@186 | 677 | |
paul@427 | 678 | self.condense_dependencies() |
paul@427 | 679 | |
paul@724 | 680 | try: |
paul@724 | 681 | ordered = order_dependencies(self.depends) |
paul@724 | 682 | except ValueError, exc: |
paul@724 | 683 | raise ProgramError("Modules with unresolvable dependencies exist: %s" % ", ".join(exc.args[0].keys())) |
paul@313 | 684 | |
paul@463 | 685 | if "__main__" in ordered: |
paul@463 | 686 | ordered.remove("__main__") |
paul@463 | 687 | |
paul@313 | 688 | ordered.append("__main__") |
paul@186 | 689 | return ordered |
paul@186 | 690 | |
paul@423 | 691 | def order_modules(self): |
paul@418 | 692 | |
paul@423 | 693 | "Produce a module initialisation ordering." |
paul@418 | 694 | |
paul@423 | 695 | ordered = self.order_objects() |
paul@423 | 696 | filtered = [] |
paul@418 | 697 | |
paul@423 | 698 | for module_name in self.modules.keys(): |
paul@423 | 699 | if module_name not in ordered: |
paul@423 | 700 | filtered.append(module_name) |
paul@418 | 701 | |
paul@423 | 702 | for path in ordered: |
paul@423 | 703 | if self.modules.has_key(path): |
paul@423 | 704 | filtered.append(path) |
paul@418 | 705 | |
paul@423 | 706 | return filtered |
paul@186 | 707 | |
paul@12 | 708 | def find_dependency(self, ref): |
paul@12 | 709 | |
paul@12 | 710 | "Find the ultimate dependency for 'ref'." |
paul@12 | 711 | |
paul@12 | 712 | found = set() |
paul@12 | 713 | while ref and ref.has_kind("<depends>") and not ref in found: |
paul@12 | 714 | found.add(ref) |
paul@35 | 715 | ref = self.identify(ref.get_origin()) |
paul@12 | 716 | return ref |
paul@12 | 717 | |
paul@16 | 718 | def get_module_provider(self, ref): |
paul@16 | 719 | |
paul@16 | 720 | "Identify the provider of the given 'ref'." |
paul@16 | 721 | |
paul@16 | 722 | for ancestor in ref.ancestors(): |
paul@16 | 723 | if self.modules.has_key(ancestor): |
paul@16 | 724 | return ancestor |
paul@16 | 725 | return None |
paul@16 | 726 | |
paul@0 | 727 | def finalise_classes(self): |
paul@0 | 728 | |
paul@0 | 729 | "Finalise the class relationships and attributes." |
paul@0 | 730 | |
paul@0 | 731 | self.derive_inherited_attrs() |
paul@0 | 732 | self.derive_subclasses() |
paul@0 | 733 | self.derive_shadowed_attrs() |
paul@0 | 734 | |
paul@0 | 735 | def derive_inherited_attrs(self): |
paul@0 | 736 | |
paul@0 | 737 | "Derive inherited attributes for classes throughout the program." |
paul@0 | 738 | |
paul@0 | 739 | for name in self.classes.keys(): |
paul@0 | 740 | self.propagate_attrs_for_class(name) |
paul@0 | 741 | |
paul@0 | 742 | def propagate_attrs_for_class(self, name, visited=None): |
paul@0 | 743 | |
paul@0 | 744 | "Propagate inherited attributes for class 'name'." |
paul@0 | 745 | |
paul@0 | 746 | # Visit classes only once. |
paul@0 | 747 | |
paul@0 | 748 | if self.all_combined_attrs.has_key(name): |
paul@0 | 749 | return |
paul@0 | 750 | |
paul@0 | 751 | visited = visited or [] |
paul@0 | 752 | |
paul@0 | 753 | if name in visited: |
paul@0 | 754 | raise ProgramError, "Class %s may not inherit from itself: %s -> %s." % (name, " -> ".join(visited), name) |
paul@0 | 755 | |
paul@0 | 756 | visited.append(name) |
paul@0 | 757 | |
paul@0 | 758 | class_attrs = {} |
paul@0 | 759 | instance_attrs = {} |
paul@0 | 760 | |
paul@0 | 761 | # Aggregate the attributes from base classes, recording the origins of |
paul@0 | 762 | # applicable attributes. |
paul@0 | 763 | |
paul@0 | 764 | for base in self.classes[name][::-1]: |
paul@0 | 765 | |
paul@0 | 766 | # Get the identity of the class from the reference. |
paul@0 | 767 | |
paul@0 | 768 | base = base.get_origin() |
paul@0 | 769 | |
paul@0 | 770 | # Define the base class completely before continuing with this |
paul@0 | 771 | # class. |
paul@0 | 772 | |
paul@0 | 773 | self.propagate_attrs_for_class(base, visited) |
paul@0 | 774 | class_attrs.update(self.all_class_attrs[base]) |
paul@0 | 775 | |
paul@0 | 776 | # Instance attribute origins are combined if different. |
paul@0 | 777 | |
paul@0 | 778 | for key, values in self.all_instance_attrs[base].items(): |
paul@0 | 779 | init_item(instance_attrs, key, set) |
paul@0 | 780 | instance_attrs[key].update(values) |
paul@0 | 781 | |
paul@0 | 782 | # Class attributes override those defined earlier in the hierarchy. |
paul@0 | 783 | |
paul@0 | 784 | class_attrs.update(self.all_class_attrs.get(name, {})) |
paul@0 | 785 | |
paul@0 | 786 | # Instance attributes are merely added if not already defined. |
paul@0 | 787 | |
paul@0 | 788 | for key in self.all_instance_attrs.get(name, []): |
paul@0 | 789 | if not instance_attrs.has_key(key): |
paul@0 | 790 | instance_attrs[key] = set(["%s.%s" % (name, key)]) |
paul@0 | 791 | |
paul@0 | 792 | self.all_class_attrs[name] = class_attrs |
paul@0 | 793 | self.all_instance_attrs[name] = instance_attrs |
paul@0 | 794 | self.all_combined_attrs[name] = set(class_attrs.keys()).union(instance_attrs.keys()) |
paul@0 | 795 | |
paul@0 | 796 | def derive_subclasses(self): |
paul@0 | 797 | |
paul@0 | 798 | "Derive subclass details for classes." |
paul@0 | 799 | |
paul@0 | 800 | for name, bases in self.classes.items(): |
paul@0 | 801 | for base in bases: |
paul@0 | 802 | |
paul@0 | 803 | # Get the identity of the class from the reference. |
paul@0 | 804 | |
paul@0 | 805 | base = base.get_origin() |
paul@0 | 806 | self.subclasses[base].add(name) |
paul@0 | 807 | |
paul@0 | 808 | def derive_shadowed_attrs(self): |
paul@0 | 809 | |
paul@0 | 810 | "Derive shadowed attributes for classes." |
paul@0 | 811 | |
paul@0 | 812 | for name, attrs in self.all_instance_attrs.items(): |
paul@0 | 813 | attrs = set(attrs.keys()).intersection(self.all_class_attrs[name].keys()) |
paul@0 | 814 | if attrs: |
paul@0 | 815 | self.all_shadowed_attrs[name] = attrs |
paul@0 | 816 | |
paul@0 | 817 | def set_class_types(self): |
paul@0 | 818 | |
paul@0 | 819 | "Set the type of each class." |
paul@0 | 820 | |
paul@0 | 821 | for attrs in self.all_class_attrs.values(): |
paul@16 | 822 | attrs["__class__"] = self.type_ref.get_origin() |
paul@0 | 823 | |
paul@0 | 824 | def define_instantiators(self): |
paul@0 | 825 | |
paul@0 | 826 | """ |
paul@0 | 827 | Consolidate parameter and default details, incorporating initialiser |
paul@0 | 828 | details to define instantiator signatures. |
paul@0 | 829 | """ |
paul@0 | 830 | |
paul@0 | 831 | for cls, attrs in self.all_class_attrs.items(): |
paul@0 | 832 | initialiser = attrs["__init__"] |
paul@119 | 833 | self.function_parameters[cls] = self.function_parameters[initialiser] |
paul@0 | 834 | self.function_defaults[cls] = self.function_defaults[initialiser] |
paul@0 | 835 | |
paul@0 | 836 | def collect_constants(self): |
paul@0 | 837 | |
paul@0 | 838 | "Get constants from all active modules." |
paul@0 | 839 | |
paul@0 | 840 | for module in self.modules.values(): |
paul@0 | 841 | self.all_constants.update(module.constants) |
paul@0 | 842 | |
paul@0 | 843 | # Import methods. |
paul@0 | 844 | |
paul@0 | 845 | def find_in_path(self, name): |
paul@0 | 846 | |
paul@0 | 847 | """ |
paul@0 | 848 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 849 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 850 | otherwise. |
paul@0 | 851 | """ |
paul@0 | 852 | |
paul@0 | 853 | for d in self.path: |
paul@0 | 854 | m = self.find(d, name) |
paul@0 | 855 | if m: return m |
paul@0 | 856 | return None |
paul@0 | 857 | |
paul@0 | 858 | def find(self, d, name): |
paul@0 | 859 | |
paul@0 | 860 | """ |
paul@0 | 861 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 862 | either refer to a single file module or to a package. Return None if the |
paul@0 | 863 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 864 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 865 | """ |
paul@0 | 866 | |
paul@0 | 867 | m = self._find_package(d, name) |
paul@0 | 868 | if m: return m |
paul@0 | 869 | m = self._find_module(d, name) |
paul@0 | 870 | if m: return m |
paul@0 | 871 | return None |
paul@0 | 872 | |
paul@0 | 873 | def _find_module(self, d, name): |
paul@0 | 874 | |
paul@0 | 875 | """ |
paul@0 | 876 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 877 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 878 | None (indicating that no package directory is involved) and a filename |
paul@0 | 879 | indicating the location of the module. |
paul@0 | 880 | """ |
paul@0 | 881 | |
paul@0 | 882 | name_py = name + extsep + "py" |
paul@0 | 883 | filename = self._find_file(d, name_py) |
paul@0 | 884 | if filename: |
paul@0 | 885 | return None, filename |
paul@0 | 886 | return None |
paul@0 | 887 | |
paul@0 | 888 | def _find_package(self, d, name): |
paul@0 | 889 | |
paul@0 | 890 | """ |
paul@0 | 891 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 892 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 893 | a directory (indicating the location of the package directory itself) |
paul@0 | 894 | and a filename indicating the location of the __init__.py module which |
paul@0 | 895 | declares the package's top-level contents. |
paul@0 | 896 | """ |
paul@0 | 897 | |
paul@0 | 898 | filename = self._find_file(d, name) |
paul@0 | 899 | if filename: |
paul@0 | 900 | init_py = "__init__" + extsep + "py" |
paul@0 | 901 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 902 | if init_py_filename: |
paul@0 | 903 | return filename, init_py_filename |
paul@0 | 904 | return None |
paul@0 | 905 | |
paul@0 | 906 | def _find_file(self, d, filename): |
paul@0 | 907 | |
paul@0 | 908 | """ |
paul@0 | 909 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 910 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 911 | """ |
paul@0 | 912 | |
paul@0 | 913 | filename = join(d, filename) |
paul@0 | 914 | if exists(filename): |
paul@0 | 915 | return filename |
paul@0 | 916 | else: |
paul@0 | 917 | return None |
paul@0 | 918 | |
paul@12 | 919 | def load(self, name): |
paul@0 | 920 | |
paul@0 | 921 | """ |
paul@0 | 922 | Load the module or package with the given 'name'. Return an object |
paul@0 | 923 | referencing the loaded module or package, or None if no such module or |
paul@0 | 924 | package exists. |
paul@0 | 925 | """ |
paul@0 | 926 | |
paul@0 | 927 | # Loaded modules are returned immediately. |
paul@0 | 928 | # Modules may be known but not yet loading (having been registered as |
paul@0 | 929 | # submodules), loading, loaded, or completely unknown. |
paul@0 | 930 | |
paul@12 | 931 | module = self.get_module(name) |
paul@0 | 932 | |
paul@0 | 933 | if module: |
paul@12 | 934 | return self.modules[name] |
paul@0 | 935 | |
paul@0 | 936 | # Otherwise, modules are loaded. |
paul@0 | 937 | |
paul@0 | 938 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 939 | # the search path. |
paul@0 | 940 | |
paul@0 | 941 | path = name.split(".") |
paul@0 | 942 | path_so_far = [] |
paul@12 | 943 | module = None |
paul@0 | 944 | |
paul@0 | 945 | for p in path: |
paul@0 | 946 | |
paul@0 | 947 | # Get the module's filesystem details. |
paul@0 | 948 | |
paul@0 | 949 | if not path_so_far: |
paul@0 | 950 | m = self.find_in_path(p) |
paul@0 | 951 | elif d: |
paul@0 | 952 | m = self.find(d, p) |
paul@0 | 953 | else: |
paul@0 | 954 | m = None |
paul@0 | 955 | |
paul@0 | 956 | path_so_far.append(p) |
paul@0 | 957 | module_name = ".".join(path_so_far) |
paul@0 | 958 | |
paul@469 | 959 | # Raise an exception if the module could not be located. |
paul@329 | 960 | |
paul@0 | 961 | if not m: |
paul@469 | 962 | raise ProgramError("Module not found: %s" % name) |
paul@0 | 963 | |
paul@329 | 964 | # Get the directory and module filename. |
paul@0 | 965 | |
paul@0 | 966 | d, filename = m |
paul@0 | 967 | |
paul@329 | 968 | # Get the module itself. |
paul@329 | 969 | |
paul@329 | 970 | return self.load_from_file(filename, module_name) |
paul@0 | 971 | |
paul@12 | 972 | def load_from_file(self, filename, module_name=None): |
paul@0 | 973 | |
paul@0 | 974 | "Load the module from the given 'filename'." |
paul@0 | 975 | |
paul@0 | 976 | if module_name is None: |
paul@0 | 977 | module_name = "__main__" |
paul@0 | 978 | |
paul@0 | 979 | module = self.modules.get(module_name) |
paul@0 | 980 | |
paul@0 | 981 | if not module: |
paul@0 | 982 | |
paul@0 | 983 | # Try to load from cache. |
paul@0 | 984 | |
paul@12 | 985 | module = self.load_from_cache(filename, module_name) |
paul@0 | 986 | if module: |
paul@0 | 987 | return module |
paul@0 | 988 | |
paul@0 | 989 | # If no cache entry exists, load from file. |
paul@0 | 990 | |
paul@0 | 991 | module = inspector.InspectedModule(module_name, self) |
paul@0 | 992 | self.add_module(module_name, module) |
paul@0 | 993 | self.update_cache_validity(module) |
paul@0 | 994 | |
paul@12 | 995 | self._load(module, module_name, lambda m: m.parse, filename) |
paul@0 | 996 | |
paul@0 | 997 | return module |
paul@0 | 998 | |
paul@0 | 999 | def update_cache_validity(self, module): |
paul@0 | 1000 | |
paul@0 | 1001 | "Make 'module' valid in the cache, but invalidate accessing modules." |
paul@0 | 1002 | |
paul@12 | 1003 | accessing = self.accessing_modules.get(module.name) |
paul@12 | 1004 | if accessing: |
paul@12 | 1005 | self.invalidated.update(accessing) |
paul@0 | 1006 | if module.name in self.invalidated: |
paul@0 | 1007 | self.invalidated.remove(module.name) |
paul@0 | 1008 | |
paul@0 | 1009 | def source_is_new(self, filename, module_name): |
paul@0 | 1010 | |
paul@0 | 1011 | "Return whether 'filename' is newer than the cached 'module_name'." |
paul@0 | 1012 | |
paul@0 | 1013 | if self.cache: |
paul@0 | 1014 | cache_filename = join(self.cache, module_name) |
paul@0 | 1015 | return not exists(cache_filename) or \ |
paul@0 | 1016 | getmtime(filename) > getmtime(cache_filename) or \ |
paul@0 | 1017 | module_name in self.invalidated |
paul@0 | 1018 | else: |
paul@0 | 1019 | return True |
paul@0 | 1020 | |
paul@12 | 1021 | def load_from_cache(self, filename, module_name): |
paul@0 | 1022 | |
paul@0 | 1023 | "Return a module residing in the cache." |
paul@0 | 1024 | |
paul@0 | 1025 | module = self.modules.get(module_name) |
paul@0 | 1026 | |
paul@12 | 1027 | if not module and not self.source_is_new(filename, module_name): |
paul@13 | 1028 | module = CachedModule(module_name, self) |
paul@12 | 1029 | self.add_module(module_name, module) |
paul@0 | 1030 | |
paul@12 | 1031 | filename = join(self.cache, module_name) |
paul@12 | 1032 | self._load(module, module_name, lambda m: m.from_cache, filename) |
paul@0 | 1033 | |
paul@0 | 1034 | return module |
paul@0 | 1035 | |
paul@12 | 1036 | def _load(self, module, module_name, fn, filename): |
paul@0 | 1037 | |
paul@0 | 1038 | """ |
paul@12 | 1039 | Load 'module' for the given 'module_name', and with 'fn' performing an |
paul@12 | 1040 | invocation on the module with the given 'filename'. |
paul@0 | 1041 | """ |
paul@0 | 1042 | |
paul@12 | 1043 | # Load the module. |
paul@0 | 1044 | |
paul@0 | 1045 | if self.verbose: |
paul@53 | 1046 | print >>sys.stderr, module_name in self.required and "Required" or "Loading", module_name, "from", filename |
paul@0 | 1047 | fn(module)(filename) |
paul@0 | 1048 | |
paul@54 | 1049 | # Add the module object if not already defined. |
paul@54 | 1050 | |
paul@54 | 1051 | if not self.objects.has_key(module_name): |
paul@54 | 1052 | self.objects[module_name] = Reference("<module>", module_name) |
paul@54 | 1053 | |
paul@0 | 1054 | def add_module(self, module_name, module): |
paul@0 | 1055 | |
paul@0 | 1056 | """ |
paul@0 | 1057 | Return the module with the given 'module_name', adding a new module |
paul@0 | 1058 | object if one does not already exist. |
paul@0 | 1059 | """ |
paul@0 | 1060 | |
paul@0 | 1061 | self.modules[module_name] = module |
paul@12 | 1062 | if module_name in self.to_import: |
paul@12 | 1063 | self.to_import.remove(module_name) |
paul@0 | 1064 | |
paul@0 | 1065 | # vim: tabstop=4 expandtab shiftwidth=4 |