]> jfr.im git - erebus.git/blame - ctlmod.py
control - show ignored status in whois
[erebus.git] / ctlmod.py
CommitLineData
931c88a4 1# Erebus IRC bot - Author: John Runyon
4477123d 2# vim: fileencoding=utf-8
931c88a4 3# module loading/unloading/tracking code
4
a28e2ae9 5from __future__ import print_function
6
9221e6f6 7import sys, time, importlib
e4255e70 8import modlib
db50981b 9
a28e2ae9 10if sys.version_info.major >= 3:
68dff4aa 11 from importlib import reload # reload is only available as a global in Py2, only in importlib in Py3
9221e6f6 12else:
68dff4aa 13 importlib.invalidate_caches = lambda: None # invalidate_caches doesn't exist in Py2
a28e2ae9 14
db50981b 15modules = {}
e4255e70 16dependents = {}
a8553c45 17#dependents[modname] = [list of modules which depend on modname]
db50981b 18
19def isloaded(modname): return modname in modules
d431e543 20def modhas(modname, attname): return getattr(modules[modname], attname, None) is not None
db50981b 21
b9c6eb1d 22def load(parent, modname, dependent=False):
9221e6f6 23 """Wrapper to call _load and print the return value."""
b9c6eb1d 24 if dependent:
a28e2ae9 25 print("(Loading dependency %s..." % (modname), end=' ')
b9c6eb1d 26 else:
a28e2ae9 27 print("%09.3f [MOD] [?] Loading %s..." % (time.time() % 100000, modname), end=' ')
b9c6eb1d 28 modstatus = _load(parent, modname, dependent)
29 if not modstatus:
a62d0d18 30 if dependent:
a28e2ae9 31 print("failed: %s)" % (modstatus), end=' ')
a62d0d18 32 else:
a28e2ae9 33 print("failed: %s." % (modstatus))
b9c6eb1d 34 elif modstatus == True:
35 if dependent:
a28e2ae9 36 print("OK)", end=' ')
b9c6eb1d 37 else:
a28e2ae9 38 print("OK.")
b9c6eb1d 39 else:
a62d0d18 40 if dependent:
a28e2ae9 41 print("OK: %s)" % (modstatus), end=' ')
a62d0d18 42 else:
a28e2ae9 43 print("OK: %s." % (modstatus))
b9c6eb1d 44 return modstatus
45
46def _load(parent, modname, dependent=False):
9221e6f6 47 """Load and return the new status of the module."""
a62d0d18 48 successstatus = []
db50981b 49 if not isloaded(modname):
9221e6f6 50 importlib.invalidate_caches()
96d0b31e 51 try:
9221e6f6 52 mod = importlib.import_module('modules.'+modname)
8ba56606 53 reload(mod) #in case it's been previously loaded.
83ed3882 54 except Exception as e:
96d0b31e 55 return modlib.error(e)
20df9fbb 56
e4255e70 57
6b2c681d 58 if not hasattr(mod, 'modinfo'):
59 return modlib.error('no modinfo')
60
a76c4bd8 61 if parent.APIVERSION not in mod.modinfo['compatible']:
e4255e70 62 return modlib.error('API-incompatible')
63
db50981b 64 modules[modname] = mod
e4255e70 65 dependents[modname] = []
66
67 for dep in mod.modinfo['depends']:
a62d0d18 68 if bool(int(parent.cfg.get('autoloads', dep, default=1))):
69 if dep not in modules:
70 depret = load(parent, dep, dependent=True)
71 if depret is not None and not depret:
72 return depret
73 else:
74 return modlib.error("dependent %s disabled" % (dep))
e4255e70 75 dependents[dep].append(modname)
76
a62d0d18 77 for dep in mod.modinfo['softdeps']:
78 if bool(int(parent.cfg.get('autoloads', dep, default=1))):
79 if dep not in modules:
80 depret = load(parent, dep, dependent=True)
9cf6ec9b
JR
81 if depret is not None and not depret:
82 successstatus.append("softdep %s failed" % (dep))
a62d0d18 83 else:
84 successstatus.append("softdep %s disabled" % (dep))
85 #swallow errors loading - softdeps are preferred, not required
86
e4255e70 87
db50981b 88 ret = mod.modstart(parent)
a62d0d18 89 if ret is None:
90 ret = True
91 if not ret:
db50981b 92 del modules[modname]
e4255e70 93 del dependents[modname]
94 for dep in mod.modinfo['depends']:
95 dependents[dep].remove(modname)
a62d0d18 96
97 successstatus = ';'.join(successstatus)
98 if len(successstatus) > 0 and ret:
99 if ret == True:
100 return successstatus
101 else:
102 return "%s (%s)" % (ret, successstatus)
103 else:
104 return ret
e4255e70 105 else: #if not isloaded...else:
106 return modlib.error('already loaded')
db50981b 107
108def unload(parent, modname):
109 if isloaded(modname):
e4255e70 110 for dependent in dependents[modname]:
111 unload(parent, dependent)
a8553c45 112 for dep in modules[modname].modinfo['depends']:
e4255e70 113 dependents[dep].remove(modname)
5a81c82b 114 ret = modules[modname].modstop(parent)
115 del modules[modname]
116 return ret
db50981b 117 else:
e4255e70 118 return modlib.error('already unloaded')
db50981b 119
120def reloadmod(parent, modname):
121 if isloaded(modname):
d431e543 122 if modhas(modname, 'modrestart'): modules[modname].modrestart(parent)
123 else: modules[modname].modstop(parent)
db50981b 124
e3878612 125 try:
65b86c90 126 reload(modules[modname])
96d0b31e 127 except BaseException as e:
e3878612 128 return modlib.error(e)
db50981b 129
8ba56606 130 if modhas(modname, 'modrestarted'): ret = modules[modname].modrestarted(parent)
131 else: ret = modules[modname].modstart(parent)
db50981b 132
8ba56606 133 return ret
db50981b 134 else:
e3878612 135 return load(parent, modname)
136
db50981b 137
138def loadall(parent, modlist):
139 for m in modlist: load(parent, m)
140def unloadall(parent, modlist):
141 for m in modlist: unload(parent, m)
142def reloadall(parent, modlist):
143 for m in modlist: reloadmod(parent, m)