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