Checks to see if any loaded modules have changed on disk and, if so, reloads them.
| 763 | |
| 764 | |
| 765 | class Reloader: |
| 766 | """Checks to see if any loaded modules have changed on disk and, |
| 767 | if so, reloads them. |
| 768 | """ |
| 769 | |
| 770 | """File suffix of compiled modules.""" |
| 771 | if sys.platform.startswith("java"): |
| 772 | SUFFIX = "$py.class" |
| 773 | else: |
| 774 | SUFFIX = ".pyc" |
| 775 | |
| 776 | def __init__(self): |
| 777 | self.mtimes = {} |
| 778 | |
| 779 | def __call__(self): |
| 780 | sys_modules = list(sys.modules.values()) |
| 781 | for mod in sys_modules: |
| 782 | self.check(mod) |
| 783 | |
| 784 | def check(self, mod): |
| 785 | # jython registers java packages as modules but they either |
| 786 | # don't have a __file__ attribute or its value is None |
| 787 | if not (mod and hasattr(mod, "__file__") and mod.__file__): |
| 788 | return |
| 789 | |
| 790 | try: |
| 791 | mtime = os.stat(mod.__file__).st_mtime |
| 792 | except OSError: |
| 793 | return |
| 794 | if mod.__file__.endswith(self.__class__.SUFFIX) and os.path.exists( |
| 795 | mod.__file__[:-1] |
| 796 | ): |
| 797 | mtime = max(os.stat(mod.__file__[:-1]).st_mtime, mtime) |
| 798 | |
| 799 | if mod not in self.mtimes: |
| 800 | self.mtimes[mod] = mtime |
| 801 | elif self.mtimes[mod] < mtime: |
| 802 | try: |
| 803 | reload(mod) |
| 804 | self.mtimes[mod] = mtime |
| 805 | except ImportError: |
| 806 | pass |
| 807 | |
| 808 | |
| 809 | if __name__ == "__main__": |