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@12 | 53 | self.to_import = set() |
paul@16 | 54 | self.required = set(["__main__"]) |
paul@12 | 55 | |
paul@0 | 56 | self.modules = {} |
paul@12 | 57 | self.accessing_modules = {} |
paul@0 | 58 | self.invalidated = set() |
paul@0 | 59 | |
paul@0 | 60 | self.objects = {} |
paul@0 | 61 | self.classes = {} |
paul@0 | 62 | self.function_parameters = {} |
paul@0 | 63 | self.function_defaults = {} |
paul@0 | 64 | self.function_targets = {} |
paul@0 | 65 | self.function_arguments = {} |
paul@0 | 66 | |
paul@0 | 67 | # Derived information. |
paul@0 | 68 | |
paul@0 | 69 | self.subclasses = {} |
paul@0 | 70 | |
paul@0 | 71 | # Attributes of different object types. |
paul@0 | 72 | |
paul@0 | 73 | self.all_class_attrs = {} |
paul@0 | 74 | self.all_instance_attrs = {} |
paul@0 | 75 | self.all_instance_attr_constants = {} |
paul@0 | 76 | self.all_combined_attrs = {} |
paul@0 | 77 | self.all_module_attrs = {} |
paul@0 | 78 | self.all_shadowed_attrs = {} |
paul@0 | 79 | |
paul@0 | 80 | # References to external names and aliases within program units. |
paul@0 | 81 | |
paul@0 | 82 | self.all_name_references = {} |
paul@0 | 83 | self.all_initialised_names = {} |
paul@0 | 84 | self.all_aliased_names = {} |
paul@0 | 85 | |
paul@0 | 86 | # General attribute accesses. |
paul@0 | 87 | |
paul@0 | 88 | self.all_attr_accesses = {} |
paul@0 | 89 | self.all_const_accesses = {} |
paul@0 | 90 | self.all_attr_access_modifiers = {} |
paul@0 | 91 | |
paul@0 | 92 | # Constant literals and values. |
paul@0 | 93 | |
paul@0 | 94 | self.all_constants = {} |
paul@0 | 95 | self.all_constant_values = {} |
paul@0 | 96 | |
paul@0 | 97 | self.make_cache() |
paul@0 | 98 | |
paul@0 | 99 | def make_cache(self): |
paul@0 | 100 | if self.cache and not exists(self.cache): |
paul@0 | 101 | makedirs(self.cache) |
paul@0 | 102 | |
paul@0 | 103 | def check_cache(self, details): |
paul@0 | 104 | |
paul@0 | 105 | """ |
paul@0 | 106 | Check whether the cache applies for the given 'details', invalidating it |
paul@0 | 107 | if it does not. |
paul@0 | 108 | """ |
paul@0 | 109 | |
paul@0 | 110 | recorded_details = self.get_cache_details() |
paul@0 | 111 | |
paul@0 | 112 | if recorded_details != details: |
paul@0 | 113 | self.remove_cache() |
paul@0 | 114 | |
paul@0 | 115 | writefile(self.get_cache_details_filename(), details) |
paul@0 | 116 | |
paul@0 | 117 | def get_cache_details_filename(self): |
paul@0 | 118 | |
paul@0 | 119 | "Return the filename for the cache details." |
paul@0 | 120 | |
paul@0 | 121 | return join(self.cache, "$details") |
paul@0 | 122 | |
paul@0 | 123 | def get_cache_details(self): |
paul@0 | 124 | |
paul@0 | 125 | "Return details of the cache." |
paul@0 | 126 | |
paul@0 | 127 | details_filename = self.get_cache_details_filename() |
paul@0 | 128 | |
paul@0 | 129 | if not exists(details_filename): |
paul@0 | 130 | return None |
paul@0 | 131 | else: |
paul@0 | 132 | return readfile(details_filename) |
paul@0 | 133 | |
paul@0 | 134 | def remove_cache(self): |
paul@0 | 135 | |
paul@0 | 136 | "Remove the contents of the cache." |
paul@0 | 137 | |
paul@0 | 138 | for filename in listdir(self.cache): |
paul@0 | 139 | remove(join(self.cache, filename)) |
paul@0 | 140 | |
paul@0 | 141 | def to_cache(self): |
paul@0 | 142 | |
paul@0 | 143 | "Write modules to the cache." |
paul@0 | 144 | |
paul@0 | 145 | if self.cache: |
paul@0 | 146 | for module_name, module in self.modules.items(): |
paul@0 | 147 | module.to_cache(join(self.cache, module_name)) |
paul@0 | 148 | |
paul@0 | 149 | # Object retrieval and storage. |
paul@0 | 150 | |
paul@0 | 151 | def get_object(self, name): |
paul@0 | 152 | |
paul@0 | 153 | """ |
paul@0 | 154 | Return a reference for the given 'name' or None if no such object |
paul@0 | 155 | exists. |
paul@0 | 156 | """ |
paul@0 | 157 | |
paul@0 | 158 | return self.objects.get(name) |
paul@0 | 159 | |
paul@0 | 160 | def set_object(self, name, value=None): |
paul@0 | 161 | |
paul@0 | 162 | "Set the object with the given 'name' and the given 'value'." |
paul@0 | 163 | |
paul@0 | 164 | if isinstance(value, Reference): |
paul@0 | 165 | ref = value.alias(name) |
paul@0 | 166 | else: |
paul@0 | 167 | ref = Reference(value, name) |
paul@0 | 168 | |
paul@0 | 169 | self.objects[name] = ref |
paul@0 | 170 | |
paul@0 | 171 | # Indirect object retrieval. |
paul@0 | 172 | |
paul@0 | 173 | def get_attributes(self, ref, attrname): |
paul@0 | 174 | |
paul@0 | 175 | """ |
paul@0 | 176 | Return attributes provided by 'ref' for 'attrname'. Class attributes |
paul@0 | 177 | may be provided by instances. |
paul@0 | 178 | """ |
paul@0 | 179 | |
paul@0 | 180 | kind = ref.get_kind() |
paul@0 | 181 | if kind == "<class>": |
paul@0 | 182 | ref = self.get_class_attribute(ref.get_origin(), attrname) |
paul@0 | 183 | return ref and set([ref]) or set() |
paul@0 | 184 | elif kind == "<instance>": |
paul@0 | 185 | return self.get_combined_attributes(ref.get_origin(), attrname) |
paul@0 | 186 | elif kind == "<module>": |
paul@0 | 187 | ref = self.get_module_attribute(ref.get_origin(), attrname) |
paul@0 | 188 | return ref and set([ref]) or set() |
paul@0 | 189 | else: |
paul@0 | 190 | return set() |
paul@0 | 191 | |
paul@0 | 192 | def get_class_attribute(self, object_type, attrname): |
paul@0 | 193 | |
paul@0 | 194 | "Return from 'object_type' the details of class attribute 'attrname'." |
paul@0 | 195 | |
paul@0 | 196 | attr = self.all_class_attrs[object_type].get(attrname) |
paul@0 | 197 | return attr and self.get_object(attr) |
paul@0 | 198 | |
paul@0 | 199 | def get_instance_attributes(self, object_type, attrname): |
paul@0 | 200 | |
paul@0 | 201 | """ |
paul@0 | 202 | Return from 'object_type' the details of instance attribute 'attrname'. |
paul@0 | 203 | """ |
paul@0 | 204 | |
paul@0 | 205 | consts = self.all_instance_attr_constants.get(object_type) |
paul@0 | 206 | attrs = set() |
paul@0 | 207 | for attr in self.all_instance_attrs[object_type].get(attrname, []): |
paul@0 | 208 | attrs.add(consts and consts.get(attrname) or Reference("<var>", attr)) |
paul@0 | 209 | return attrs |
paul@0 | 210 | |
paul@0 | 211 | def get_combined_attributes(self, object_type, attrname): |
paul@0 | 212 | |
paul@0 | 213 | """ |
paul@0 | 214 | Return from 'object_type' the details of class or instance attribute |
paul@0 | 215 | 'attrname'. |
paul@0 | 216 | """ |
paul@0 | 217 | |
paul@0 | 218 | ref = self.get_class_attribute(object_type, attrname) |
paul@0 | 219 | refs = ref and set([ref]) or set() |
paul@0 | 220 | refs.update(self.get_instance_attributes(object_type, attrname)) |
paul@0 | 221 | return refs |
paul@0 | 222 | |
paul@0 | 223 | def get_module_attribute(self, object_type, attrname): |
paul@0 | 224 | |
paul@0 | 225 | "Return from 'object_type' the details of module attribute 'attrname'." |
paul@0 | 226 | |
paul@0 | 227 | if attrname in self.all_module_attrs[object_type]: |
paul@0 | 228 | return self.get_object("%s.%s" % (object_type, attrname)) |
paul@0 | 229 | else: |
paul@0 | 230 | return None |
paul@0 | 231 | |
paul@0 | 232 | # Module management. |
paul@0 | 233 | |
paul@16 | 234 | def queue_module(self, name, accessor, required=False): |
paul@12 | 235 | |
paul@12 | 236 | """ |
paul@12 | 237 | Queue the module with the given 'name' for import from the given |
paul@16 | 238 | 'accessor' module. If 'required' is true (it is false by default), the |
paul@16 | 239 | module will be required in the final program. |
paul@12 | 240 | """ |
paul@12 | 241 | |
paul@12 | 242 | if not self.modules.has_key(name): |
paul@12 | 243 | self.to_import.add(name) |
paul@12 | 244 | |
paul@16 | 245 | if required: |
paul@16 | 246 | self.required.add(name) |
paul@16 | 247 | |
paul@12 | 248 | init_item(self.accessing_modules, name, set) |
paul@16 | 249 | self.accessing_modules[name].add(accessor.name) |
paul@12 | 250 | |
paul@0 | 251 | def get_modules(self): |
paul@0 | 252 | |
paul@0 | 253 | "Return all modules known to the importer." |
paul@0 | 254 | |
paul@0 | 255 | return self.modules.values() |
paul@0 | 256 | |
paul@12 | 257 | def get_module(self, name): |
paul@0 | 258 | |
paul@0 | 259 | "Return the module with the given 'name'." |
paul@0 | 260 | |
paul@0 | 261 | if not self.modules.has_key(name): |
paul@0 | 262 | return None |
paul@0 | 263 | |
paul@12 | 264 | return self.modules[name] |
paul@0 | 265 | |
paul@0 | 266 | # Program operations. |
paul@0 | 267 | |
paul@0 | 268 | def initialise(self, filename, reset=False): |
paul@0 | 269 | |
paul@0 | 270 | """ |
paul@0 | 271 | Initialise a program whose main module is 'filename', resetting the |
paul@0 | 272 | cache if 'reset' is true. Return the main module. |
paul@0 | 273 | """ |
paul@0 | 274 | |
paul@0 | 275 | if reset: |
paul@0 | 276 | self.remove_cache() |
paul@0 | 277 | self.check_cache(filename) |
paul@0 | 278 | |
paul@0 | 279 | # Load the program itself. |
paul@0 | 280 | |
paul@0 | 281 | m = self.load_from_file(filename) |
paul@0 | 282 | |
paul@12 | 283 | # Load any queued modules. |
paul@12 | 284 | |
paul@12 | 285 | while self.to_import: |
paul@12 | 286 | for name in list(self.to_import): # avoid mutation issue |
paul@12 | 287 | self.load(name) |
paul@12 | 288 | |
paul@12 | 289 | # Resolve dependencies between modules. |
paul@12 | 290 | |
paul@12 | 291 | self.resolve() |
paul@12 | 292 | |
paul@16 | 293 | # Record the type of all classes. |
paul@16 | 294 | |
paul@16 | 295 | self.type_ref = self.get_object("__builtins__.type") |
paul@16 | 296 | |
paul@0 | 297 | # Resolve dependencies within the program. |
paul@0 | 298 | |
paul@12 | 299 | for module in self.modules.values(): |
paul@12 | 300 | module.complete() |
paul@0 | 301 | |
paul@16 | 302 | # Remove unneeded modules. |
paul@16 | 303 | |
paul@16 | 304 | all_modules = self.modules.items() |
paul@16 | 305 | |
paul@16 | 306 | for name, module in all_modules: |
paul@16 | 307 | if name not in self.required: |
paul@16 | 308 | module.unpropagate() |
paul@16 | 309 | del self.modules[name] |
paul@16 | 310 | |
paul@0 | 311 | return m |
paul@0 | 312 | |
paul@0 | 313 | def finalise(self): |
paul@0 | 314 | |
paul@0 | 315 | "Finalise the inspected program." |
paul@0 | 316 | |
paul@0 | 317 | self.finalise_classes() |
paul@0 | 318 | self.to_cache() |
paul@0 | 319 | self.set_class_types() |
paul@0 | 320 | self.define_instantiators() |
paul@0 | 321 | self.collect_constants() |
paul@0 | 322 | |
paul@12 | 323 | # Supporting operations. |
paul@12 | 324 | |
paul@12 | 325 | def resolve(self): |
paul@12 | 326 | |
paul@12 | 327 | "Resolve dependencies between modules." |
paul@12 | 328 | |
paul@12 | 329 | resolved = {} |
paul@12 | 330 | |
paul@12 | 331 | for name, ref in self.objects.items(): |
paul@12 | 332 | if ref.has_kind("<depends>"): |
paul@14 | 333 | found = self.find_dependency(ref) |
paul@14 | 334 | if found: |
paul@14 | 335 | resolved[name] = found |
paul@14 | 336 | else: |
paul@14 | 337 | print >>sys.stderr, "Name %s references an unknown object: %s" % (name, ref.get_origin()) |
paul@12 | 338 | |
paul@16 | 339 | # Record the resolved names and identify required modules. |
paul@16 | 340 | |
paul@12 | 341 | for name, ref in resolved.items(): |
paul@12 | 342 | self.objects[name] = ref |
paul@12 | 343 | |
paul@16 | 344 | module_name = self.get_module_provider(ref) |
paul@16 | 345 | if module_name: |
paul@16 | 346 | self.required.add(module_name) |
paul@16 | 347 | |
paul@12 | 348 | def find_dependency(self, ref): |
paul@12 | 349 | |
paul@12 | 350 | "Find the ultimate dependency for 'ref'." |
paul@12 | 351 | |
paul@12 | 352 | found = set() |
paul@12 | 353 | while ref and ref.has_kind("<depends>") and not ref in found: |
paul@12 | 354 | found.add(ref) |
paul@12 | 355 | ref = self.objects.get(ref.get_origin()) |
paul@12 | 356 | return ref |
paul@12 | 357 | |
paul@16 | 358 | def get_module_provider(self, ref): |
paul@16 | 359 | |
paul@16 | 360 | "Identify the provider of the given 'ref'." |
paul@16 | 361 | |
paul@16 | 362 | for ancestor in ref.ancestors(): |
paul@16 | 363 | if self.modules.has_key(ancestor): |
paul@16 | 364 | return ancestor |
paul@16 | 365 | return None |
paul@16 | 366 | |
paul@0 | 367 | def finalise_classes(self): |
paul@0 | 368 | |
paul@0 | 369 | "Finalise the class relationships and attributes." |
paul@0 | 370 | |
paul@0 | 371 | self.derive_inherited_attrs() |
paul@0 | 372 | self.derive_subclasses() |
paul@0 | 373 | self.derive_shadowed_attrs() |
paul@0 | 374 | |
paul@0 | 375 | def derive_inherited_attrs(self): |
paul@0 | 376 | |
paul@0 | 377 | "Derive inherited attributes for classes throughout the program." |
paul@0 | 378 | |
paul@0 | 379 | for name in self.classes.keys(): |
paul@0 | 380 | self.propagate_attrs_for_class(name) |
paul@0 | 381 | |
paul@0 | 382 | def propagate_attrs_for_class(self, name, visited=None): |
paul@0 | 383 | |
paul@0 | 384 | "Propagate inherited attributes for class 'name'." |
paul@0 | 385 | |
paul@0 | 386 | # Visit classes only once. |
paul@0 | 387 | |
paul@0 | 388 | if self.all_combined_attrs.has_key(name): |
paul@0 | 389 | return |
paul@0 | 390 | |
paul@0 | 391 | visited = visited or [] |
paul@0 | 392 | |
paul@0 | 393 | if name in visited: |
paul@0 | 394 | raise ProgramError, "Class %s may not inherit from itself: %s -> %s." % (name, " -> ".join(visited), name) |
paul@0 | 395 | |
paul@0 | 396 | visited.append(name) |
paul@0 | 397 | |
paul@0 | 398 | class_attrs = {} |
paul@0 | 399 | instance_attrs = {} |
paul@0 | 400 | |
paul@0 | 401 | # Aggregate the attributes from base classes, recording the origins of |
paul@0 | 402 | # applicable attributes. |
paul@0 | 403 | |
paul@0 | 404 | for base in self.classes[name][::-1]: |
paul@0 | 405 | |
paul@0 | 406 | # Get the identity of the class from the reference. |
paul@0 | 407 | |
paul@0 | 408 | base = base.get_origin() |
paul@0 | 409 | |
paul@0 | 410 | # Define the base class completely before continuing with this |
paul@0 | 411 | # class. |
paul@0 | 412 | |
paul@0 | 413 | self.propagate_attrs_for_class(base, visited) |
paul@0 | 414 | class_attrs.update(self.all_class_attrs[base]) |
paul@0 | 415 | |
paul@0 | 416 | # Instance attribute origins are combined if different. |
paul@0 | 417 | |
paul@0 | 418 | for key, values in self.all_instance_attrs[base].items(): |
paul@0 | 419 | init_item(instance_attrs, key, set) |
paul@0 | 420 | instance_attrs[key].update(values) |
paul@0 | 421 | |
paul@0 | 422 | # Class attributes override those defined earlier in the hierarchy. |
paul@0 | 423 | |
paul@0 | 424 | class_attrs.update(self.all_class_attrs.get(name, {})) |
paul@0 | 425 | |
paul@0 | 426 | # Instance attributes are merely added if not already defined. |
paul@0 | 427 | |
paul@0 | 428 | for key in self.all_instance_attrs.get(name, []): |
paul@0 | 429 | if not instance_attrs.has_key(key): |
paul@0 | 430 | instance_attrs[key] = set(["%s.%s" % (name, key)]) |
paul@0 | 431 | |
paul@0 | 432 | self.all_class_attrs[name] = class_attrs |
paul@0 | 433 | self.all_instance_attrs[name] = instance_attrs |
paul@0 | 434 | self.all_combined_attrs[name] = set(class_attrs.keys()).union(instance_attrs.keys()) |
paul@0 | 435 | |
paul@0 | 436 | def derive_subclasses(self): |
paul@0 | 437 | |
paul@0 | 438 | "Derive subclass details for classes." |
paul@0 | 439 | |
paul@0 | 440 | for name, bases in self.classes.items(): |
paul@0 | 441 | for base in bases: |
paul@0 | 442 | |
paul@0 | 443 | # Get the identity of the class from the reference. |
paul@0 | 444 | |
paul@0 | 445 | base = base.get_origin() |
paul@0 | 446 | self.subclasses[base].add(name) |
paul@0 | 447 | |
paul@0 | 448 | def derive_shadowed_attrs(self): |
paul@0 | 449 | |
paul@0 | 450 | "Derive shadowed attributes for classes." |
paul@0 | 451 | |
paul@0 | 452 | for name, attrs in self.all_instance_attrs.items(): |
paul@0 | 453 | attrs = set(attrs.keys()).intersection(self.all_class_attrs[name].keys()) |
paul@0 | 454 | if attrs: |
paul@0 | 455 | self.all_shadowed_attrs[name] = attrs |
paul@0 | 456 | |
paul@0 | 457 | def set_class_types(self): |
paul@0 | 458 | |
paul@0 | 459 | "Set the type of each class." |
paul@0 | 460 | |
paul@0 | 461 | for attrs in self.all_class_attrs.values(): |
paul@16 | 462 | attrs["__class__"] = self.type_ref.get_origin() |
paul@0 | 463 | |
paul@0 | 464 | def define_instantiators(self): |
paul@0 | 465 | |
paul@0 | 466 | """ |
paul@0 | 467 | Consolidate parameter and default details, incorporating initialiser |
paul@0 | 468 | details to define instantiator signatures. |
paul@0 | 469 | """ |
paul@0 | 470 | |
paul@0 | 471 | for cls, attrs in self.all_class_attrs.items(): |
paul@0 | 472 | initialiser = attrs["__init__"] |
paul@0 | 473 | self.function_parameters[cls] = self.function_parameters[initialiser][1:] |
paul@0 | 474 | self.function_defaults[cls] = self.function_defaults[initialiser] |
paul@0 | 475 | |
paul@0 | 476 | def collect_constants(self): |
paul@0 | 477 | |
paul@0 | 478 | "Get constants from all active modules." |
paul@0 | 479 | |
paul@0 | 480 | for module in self.modules.values(): |
paul@0 | 481 | self.all_constants.update(module.constants) |
paul@0 | 482 | |
paul@0 | 483 | # Import methods. |
paul@0 | 484 | |
paul@0 | 485 | def find_in_path(self, name): |
paul@0 | 486 | |
paul@0 | 487 | """ |
paul@0 | 488 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 489 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 490 | otherwise. |
paul@0 | 491 | """ |
paul@0 | 492 | |
paul@0 | 493 | for d in self.path: |
paul@0 | 494 | m = self.find(d, name) |
paul@0 | 495 | if m: return m |
paul@0 | 496 | return None |
paul@0 | 497 | |
paul@0 | 498 | def find(self, d, name): |
paul@0 | 499 | |
paul@0 | 500 | """ |
paul@0 | 501 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 502 | either refer to a single file module or to a package. Return None if the |
paul@0 | 503 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 504 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 505 | """ |
paul@0 | 506 | |
paul@0 | 507 | m = self._find_package(d, name) |
paul@0 | 508 | if m: return m |
paul@0 | 509 | m = self._find_module(d, name) |
paul@0 | 510 | if m: return m |
paul@0 | 511 | return None |
paul@0 | 512 | |
paul@0 | 513 | def _find_module(self, d, name): |
paul@0 | 514 | |
paul@0 | 515 | """ |
paul@0 | 516 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 517 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 518 | None (indicating that no package directory is involved) and a filename |
paul@0 | 519 | indicating the location of the module. |
paul@0 | 520 | """ |
paul@0 | 521 | |
paul@0 | 522 | name_py = name + extsep + "py" |
paul@0 | 523 | filename = self._find_file(d, name_py) |
paul@0 | 524 | if filename: |
paul@0 | 525 | return None, filename |
paul@0 | 526 | return None |
paul@0 | 527 | |
paul@0 | 528 | def _find_package(self, d, name): |
paul@0 | 529 | |
paul@0 | 530 | """ |
paul@0 | 531 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 532 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 533 | a directory (indicating the location of the package directory itself) |
paul@0 | 534 | and a filename indicating the location of the __init__.py module which |
paul@0 | 535 | declares the package's top-level contents. |
paul@0 | 536 | """ |
paul@0 | 537 | |
paul@0 | 538 | filename = self._find_file(d, name) |
paul@0 | 539 | if filename: |
paul@0 | 540 | init_py = "__init__" + extsep + "py" |
paul@0 | 541 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 542 | if init_py_filename: |
paul@0 | 543 | return filename, init_py_filename |
paul@0 | 544 | return None |
paul@0 | 545 | |
paul@0 | 546 | def _find_file(self, d, filename): |
paul@0 | 547 | |
paul@0 | 548 | """ |
paul@0 | 549 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 550 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 551 | """ |
paul@0 | 552 | |
paul@0 | 553 | filename = join(d, filename) |
paul@0 | 554 | if exists(filename): |
paul@0 | 555 | return filename |
paul@0 | 556 | else: |
paul@0 | 557 | return None |
paul@0 | 558 | |
paul@12 | 559 | def load(self, name): |
paul@0 | 560 | |
paul@0 | 561 | """ |
paul@0 | 562 | Load the module or package with the given 'name'. Return an object |
paul@0 | 563 | referencing the loaded module or package, or None if no such module or |
paul@0 | 564 | package exists. |
paul@0 | 565 | """ |
paul@0 | 566 | |
paul@0 | 567 | # Loaded modules are returned immediately. |
paul@0 | 568 | # Modules may be known but not yet loading (having been registered as |
paul@0 | 569 | # submodules), loading, loaded, or completely unknown. |
paul@0 | 570 | |
paul@12 | 571 | module = self.get_module(name) |
paul@0 | 572 | |
paul@0 | 573 | if module: |
paul@12 | 574 | return self.modules[name] |
paul@0 | 575 | |
paul@0 | 576 | # Otherwise, modules are loaded. |
paul@0 | 577 | |
paul@0 | 578 | if self.verbose: |
paul@0 | 579 | print >>sys.stderr, "Loading", name |
paul@0 | 580 | |
paul@0 | 581 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 582 | # the search path. |
paul@0 | 583 | |
paul@0 | 584 | path = name.split(".") |
paul@0 | 585 | path_so_far = [] |
paul@12 | 586 | module = None |
paul@0 | 587 | |
paul@0 | 588 | for p in path: |
paul@0 | 589 | |
paul@0 | 590 | # Get the module's filesystem details. |
paul@0 | 591 | |
paul@0 | 592 | if not path_so_far: |
paul@0 | 593 | m = self.find_in_path(p) |
paul@0 | 594 | elif d: |
paul@0 | 595 | m = self.find(d, p) |
paul@0 | 596 | else: |
paul@0 | 597 | m = None |
paul@0 | 598 | |
paul@0 | 599 | path_so_far.append(p) |
paul@0 | 600 | module_name = ".".join(path_so_far) |
paul@0 | 601 | |
paul@0 | 602 | if not m: |
paul@0 | 603 | if self.verbose: |
paul@0 | 604 | print >>sys.stderr, "Not found (%s)" % name |
paul@0 | 605 | |
paul@0 | 606 | return None # NOTE: Import error. |
paul@0 | 607 | |
paul@0 | 608 | # Get the module itself. |
paul@0 | 609 | |
paul@0 | 610 | d, filename = m |
paul@12 | 611 | module = self.load_from_file(filename, module_name) |
paul@0 | 612 | |
paul@12 | 613 | return module |
paul@0 | 614 | |
paul@12 | 615 | def load_from_file(self, filename, module_name=None): |
paul@0 | 616 | |
paul@0 | 617 | "Load the module from the given 'filename'." |
paul@0 | 618 | |
paul@0 | 619 | if module_name is None: |
paul@0 | 620 | module_name = "__main__" |
paul@0 | 621 | |
paul@0 | 622 | module = self.modules.get(module_name) |
paul@0 | 623 | |
paul@0 | 624 | if not module: |
paul@0 | 625 | |
paul@0 | 626 | # Try to load from cache. |
paul@0 | 627 | |
paul@12 | 628 | module = self.load_from_cache(filename, module_name) |
paul@0 | 629 | if module: |
paul@0 | 630 | return module |
paul@0 | 631 | |
paul@0 | 632 | # If no cache entry exists, load from file. |
paul@0 | 633 | |
paul@0 | 634 | module = inspector.InspectedModule(module_name, self) |
paul@0 | 635 | self.add_module(module_name, module) |
paul@0 | 636 | self.update_cache_validity(module) |
paul@0 | 637 | |
paul@12 | 638 | self._load(module, module_name, lambda m: m.parse, filename) |
paul@0 | 639 | |
paul@0 | 640 | return module |
paul@0 | 641 | |
paul@0 | 642 | def update_cache_validity(self, module): |
paul@0 | 643 | |
paul@0 | 644 | "Make 'module' valid in the cache, but invalidate accessing modules." |
paul@0 | 645 | |
paul@12 | 646 | accessing = self.accessing_modules.get(module.name) |
paul@12 | 647 | if accessing: |
paul@12 | 648 | self.invalidated.update(accessing) |
paul@0 | 649 | if module.name in self.invalidated: |
paul@0 | 650 | self.invalidated.remove(module.name) |
paul@0 | 651 | |
paul@0 | 652 | def source_is_new(self, filename, module_name): |
paul@0 | 653 | |
paul@0 | 654 | "Return whether 'filename' is newer than the cached 'module_name'." |
paul@0 | 655 | |
paul@0 | 656 | if self.cache: |
paul@0 | 657 | cache_filename = join(self.cache, module_name) |
paul@0 | 658 | return not exists(cache_filename) or \ |
paul@0 | 659 | getmtime(filename) > getmtime(cache_filename) or \ |
paul@0 | 660 | module_name in self.invalidated |
paul@0 | 661 | else: |
paul@0 | 662 | return True |
paul@0 | 663 | |
paul@12 | 664 | def load_from_cache(self, filename, module_name): |
paul@0 | 665 | |
paul@0 | 666 | "Return a module residing in the cache." |
paul@0 | 667 | |
paul@0 | 668 | module = self.modules.get(module_name) |
paul@0 | 669 | |
paul@12 | 670 | if not module and not self.source_is_new(filename, module_name): |
paul@13 | 671 | module = CachedModule(module_name, self) |
paul@12 | 672 | self.add_module(module_name, module) |
paul@0 | 673 | |
paul@12 | 674 | filename = join(self.cache, module_name) |
paul@12 | 675 | self._load(module, module_name, lambda m: m.from_cache, filename) |
paul@0 | 676 | |
paul@0 | 677 | return module |
paul@0 | 678 | |
paul@12 | 679 | def _load(self, module, module_name, fn, filename): |
paul@0 | 680 | |
paul@0 | 681 | """ |
paul@12 | 682 | Load 'module' for the given 'module_name', and with 'fn' performing an |
paul@12 | 683 | invocation on the module with the given 'filename'. |
paul@0 | 684 | """ |
paul@0 | 685 | |
paul@12 | 686 | # Load the module. |
paul@0 | 687 | |
paul@0 | 688 | if self.verbose: |
paul@0 | 689 | print >>sys.stderr, "Loading", filename |
paul@0 | 690 | fn(module)(filename) |
paul@0 | 691 | if self.verbose: |
paul@0 | 692 | print >>sys.stderr, "Loaded", filename |
paul@0 | 693 | |
paul@0 | 694 | def add_module(self, module_name, module): |
paul@0 | 695 | |
paul@0 | 696 | """ |
paul@0 | 697 | Return the module with the given 'module_name', adding a new module |
paul@0 | 698 | object if one does not already exist. |
paul@0 | 699 | """ |
paul@0 | 700 | |
paul@0 | 701 | self.modules[module_name] = module |
paul@0 | 702 | self.objects[module_name] = Reference("<module>", module_name) |
paul@12 | 703 | if module_name in self.to_import: |
paul@12 | 704 | self.to_import.remove(module_name) |
paul@0 | 705 | |
paul@0 | 706 | # vim: tabstop=4 expandtab shiftwidth=4 |