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