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