3
from armoryengine.ArmoryUtils import LOGEXCEPT
6
def getModuleList(inDir):
8
if not os.path.exists(inDir):
12
for fn in os.listdir(inDir):
13
if not fn.endswith('.py') and not fn.endswith('.sig'):
17
modName = fn.split('.')[0]
18
fullfn = os.path.join(inDir, fn)
19
with open(fullfn, 'r') as f:
22
if not modName in moduleMap:
23
moduleMap[modName] = {}
25
if fn.endswith('.py'):
26
moduleMap[modName]['SourceCode'] = fileData
27
moduleMap[modName]['SourceDir'] = inDir
28
moduleMap[modName]['Filename'] = fn
29
elif fn.endswith('.sig'):
30
moduleMap[modName]['SigData'] = fileData
32
LOGEXCEPT('Loading plugin %s failed. Skipping' % fullfn)
38
def dynamicImport(inDir, moduleName, injectLocals=None):
40
We import from an arbitrary directory, we need to add the dir
41
to sys.path, but we want to prevent any shenanigans by an imported
42
module that messes with sys.path (perhaps maliciously, but trying
43
to import a malicious module without allowing malicious behavior
44
is probably impossible--make sure you're using safe code and then
45
assume the module is safe).
47
Either way, we're going to assume that the dynamically-imported
48
modules are really simple and have no reason to mess with sys.path.
49
We will revisit this later if it becomes a barrier to being useful
51
if injectLocals is None:
54
pluginPath = os.path.join(inDir, moduleName+'.py')
55
if not os.path.exists(pluginPath):
58
# Join using a character that would be invalid in a pathname
59
prevSysPath = '\x00'.join(sys.path)
60
sys.path.append(inDir)
63
modTemp = __import__(moduleName)
64
modTemp.__dict__.update(injectLocals)
66
# Assume that sys.path was unmodified by the module
67
sys.path = sys.path[:-1]
68
currSysPath = '\x00'.join(sys.path)
69
if not currSysPath==prevSysPath:
70
print '***ERROR: Dynamically imported module messed with sys.path!'
71
print ' : Make sure your module does not modify sys.path'