-
Notifications
You must be signed in to change notification settings - Fork 0
/
resetSession.py
50 lines (39 loc) · 1.87 KB
/
resetSession.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import inspect
from os.path import dirname
# I'm going to define this little function to make this cleaner
# It's going to have a flag to let you specify the userPath you want to clear out
# But otherwise I'd going to assume that it's the userPath you're running the script from (__file__)
def resetSessionForScript(userPath=None):
if userPath is None:
userPath = dirname(__file__)
# Convert this to lower just for a clean comparison later
userPath = userPath.lower()
toDelete = []
# Iterate over all the modules that are currently loaded
for key, module in sys.modules.iteritems():
# There's a few modules that are going to complain if you try to query them
# so I've popped this into a try/except to keep it safe
try:
# Use the "inspect" library to get the moduleFilePath that the current module was loaded from
moduleFilePath = inspect.getfile(module).lower()
# Don't try and remove the startup script, that will break everything
if moduleFilePath == __file__.lower():
continue
# If the module's filepath contains the userPath, add it to the list of modules to delete
if moduleFilePath.startswith(userPath):
print "Removing %s" % key
toDelete.append(key)
except:
pass
# If we'd deleted the module in the loop above, it would have changed the size of the dictionary and
# broken the loop. So now we go over the list we made and delete all the modules
for module in toDelete:
del (sys.modules[module])
#########################################
# So now you can either put this at the top of your script
# Or just
#resetSessionForScript()
# Or just for anyone running the tool from an IDE
if __name__ == "__main__":
resetSessionForScript()