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