-
Notifications
You must be signed in to change notification settings - Fork 6
/
ReloadLogic.py
48 lines (38 loc) · 1.28 KB
/
ReloadLogic.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
"""
For some fun.
"""
import os
import sys
import imp
import Logging
def getLocalModChangeTime(filename):
return os.path.getmtime(myDir + "/" + filename)
def normModDir(filename):
return os.path.normpath(os.path.dirname(os.path.abspath(filename)))
myDir = normModDir(__file__)
modChangeTimes = {} # modName -> time
def listLocalModuleNames():
from glob import glob
for fn in glob(myDir + "/*.py"):
fn = os.path.basename(fn)
modName, _ = os.path.splitext(fn)
yield modName
def initModChangeTimes():
for modName in listLocalModuleNames():
if modName not in modChangeTimes:
modChangeTimes[modName] = getLocalModChangeTime(modName)
def checkReloadModules():
initModChangeTimes() # update maybe not-yet-known modules
for modName in listLocalModuleNames():
if modName not in modChangeTimes: continue
if modName not in sys.modules: continue
lastMtime = modChangeTimes[modName]
curMtime = getLocalModChangeTime(modName)
if curMtime > lastMtime:
mod = sys.modules[modName]
Logging.log("reload %s" % mod)
try:
imp.reload(mod)
except Exception:
Logging.log_exception("reloadHandler", *sys.exc_info())
initModChangeTimes()