Go to the documentation of this file.00001 import sys
00002 import __builtin__
00003
00004 rollbackImporter = None
00005
00006 class RollbackImporter:
00007 def __init__(self):
00008 self.previousModules = []
00009 for k in sys.modules.keys():
00010 self.previousModules.append(k)
00011 self.realImport = __builtin__.__import__
00012 __builtin__.__import__ = self._import
00013 self.newModules = {}
00014
00015 def _import(self, *args):
00016 n = args[0]
00017 f = args[3]
00018 try:
00019 g = args[1]
00020 except:
00021 g = {}
00022 try:
00023 l = args[2]
00024 except:
00025 l = {}
00026 result = apply(self.realImport, (n, g, l, f))
00027 self.newModules[n] = 1
00028 return result
00029
00030 def uninstall(self):
00031 for m in self.newModules.keys():
00032 if m not in self.previousModules:
00033 del(sys.modules[m])
00034 __builtin__.__import__ = self.realImport
00035
00036 def refresh():
00037 global rollbackImporter
00038 if rollbackImporter:
00039 rollbackImporter.uninstall()
00040 rollbackImporter = RollbackImporter()
00041
00042 refresh()