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