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