~ubuntu-branches/debian/jessie/armory/jessie

« back to all changes in this revision

Viewing changes to dynamicImport.py

  • Committer: Package Import Robot
  • Author(s): Joseph Bisch
  • Date: 2014-10-07 10:22:45 UTC
  • Revision ID: package-import@ubuntu.com-20141007102245-2s3x3rhjxg689hek
Tags: upstream-0.92.3
ImportĀ upstreamĀ versionĀ 0.92.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import sys
 
3
from armoryengine.ArmoryUtils import LOGEXCEPT
 
4
 
 
5
 
 
6
def getModuleList(inDir):
 
7
   moduleMap = {}
 
8
   if not os.path.exists(inDir):
 
9
      return moduleMap
 
10
 
 
11
   
 
12
   for fn in os.listdir(inDir):
 
13
      if not fn.endswith('.py') and not fn.endswith('.sig'):
 
14
         continue
 
15
 
 
16
      try:
 
17
         modName = fn.split('.')[0]
 
18
         fullfn = os.path.join(inDir, fn)
 
19
         with open(fullfn, 'r') as f:
 
20
            fileData = f.read()
 
21
 
 
22
         if not modName in moduleMap:
 
23
            moduleMap[modName] = {}
 
24
      
 
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
 
31
      except:
 
32
         LOGEXCEPT('Loading plugin %s failed.  Skipping' % fullfn)
 
33
         
 
34
      
 
35
   return moduleMap
 
36
      
 
37
 
 
38
def dynamicImport(inDir, moduleName, injectLocals=None):
 
39
   """
 
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). 
 
46
 
 
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
 
50
   """
 
51
   if injectLocals is None:
 
52
      injectLocals = {}
 
53
 
 
54
   pluginPath = os.path.join(inDir, moduleName+'.py')  
 
55
   if not os.path.exists(pluginPath):
 
56
      return None
 
57
 
 
58
   # Join using a character that would be invalid in a pathname
 
59
   prevSysPath = '\x00'.join(sys.path)
 
60
   sys.path.append(inDir)
 
61
 
 
62
 
 
63
   modTemp = __import__(moduleName)
 
64
   modTemp.__dict__.update(injectLocals)
 
65
 
 
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'
 
72
      exit(1)
 
73
   
 
74
   return modTemp
 
75