-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace.py
456 lines (344 loc) · 16.8 KB
/
namespace.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# TODO:
# - DestroyNamespace may be a little enthusiastic. Should check the contents
# of a namespace which is presumed to be clean, before deleting it. If there
# is something left, then do the appropriate thing.
# - The namespace contributions can only come from one ScriptDirectory.
# - __file__ might be made shorter and clearer by using paths relative to
# the base directory.
import os
import sys
import imp
import traceback
import types
import logging
import unittest
logger = logging.getLogger("namespace")
#logger.setLevel(logging.DEBUG)
class ScriptFile(object):
lastError = None
namespaceContributions = None
def __init__(self, filePath, namespacePath, implicitLoad=True, delGlobals=False):
self.filePath = filePath
self.namespacePath = namespacePath
self.scriptGlobals = {}
self.delGlobals = delGlobals
if implicitLoad:
self.Load(filePath)
def __del__(self):
if self.delGlobals:
self.scriptGlobals.clear()
# print "GC", self.namespacePath, self.filePath, hex(id(self.scriptGlobals))
def __repr__(self):
return "<ScriptFile filePath='%s' namespacePath='%s'>" % (self.filePath, self.namespacePath)
def Load(self, filePath):
self.filePath = filePath
script = open(self.filePath, 'rU').read() +"\n"
self.codeObject = compile(script, self.filePath, "exec")
def GetAttributeValue(self, attributeName):
return self.scriptGlobals[attributeName]
def SetNamespaceContributions(self, namespaceContributions):
self.namespaceContributions = namespaceContributions
def AddNamespaceContributions(self, namespaceContributions):
self.namespaceContributions |= namespaceContributions
def Run(self):
self.scriptGlobals = {}
try:
eval(self.codeObject, self.scriptGlobals, self.scriptGlobals)
except (ImportError, AttributeError):
# Likely reasons for encountered errors:
# ImportError: A namespace has not been exported yet.
# AttributeError: A namespace attribute is not exported yet.
self.lastError = traceback.format_exception(*sys.exc_info())
return False
return True
def UnitTest(self):
dirPath, scriptFileName = os.path.split(self.filePath)
testScriptPath = os.path.join(dirPath, scriptFileName[:-3] + "_unittest.py")
testFileExists = os.path.exists(testScriptPath)
# Create a throwaway script file object for the unit test script.
scriptFile = self.__class__(testScriptPath, None, implicitLoad=testFileExists)
if testFileExists:
scriptFile.Run()
# Inject the main script file contents underneath the test script contents.
scriptFile.scriptGlobals.update(self.scriptGlobals)
# Gather all the unit tests from any test suites.
testSuite = unittest.TestSuite()
for testCase in scriptFile.scriptGlobals.values():
if type(testCase) is types.TypeType or type(testCase) is types.ClassType:
if issubclass(testCase, unittest.TestCase):
subSuite = unittest.defaultTestLoader.loadTestsFromTestCase(testCase)
testSuite.addTests(subSuite)
logger.debug("UnitTest extracted %d tests", testSuite.countTestCases())
testResult = unittest.TestResult()
testSuite.run(testResult)
if testResult.errors or testResult.failures:
self.lastError = []
lastTestCase = None
for errorTestCase, tracebackText in testResult.errors:
if lastTestCase is not errorTestCase:
self.lastError.append("Error in test case '%s'" % errorTestCase.__class__.__name__)
lastTestCase = errorTestCase
self.lastError.append(tracebackText)
lastTestCase = None
for failureTestCase, tracebackText in testResult.failures:
if lastTestCase is not failureTestCase:
self.lastError.append("Failure in test case '%s'" % failureTestCase.__class__.__name__)
lastTestCase = failureTestCase
self.lastError.append(tracebackText)
return False
# No unit tests, or the unit tests did not error or fail.
return True
def LogLastError(self, flush=True, context="Unknown logic"):
if self.lastError is None:
logger.error("Script file '%s' unexpectedly missing a last error", self.filePath)
return
logger.error("Error executing script file '%s'\n%s", self.filePath, "".join(self.lastError).rstrip("\r\n"))
if flush:
self.lastError = None
def GetExportableAttributes(self):
import __builtin__, types
# No point handing around standard global objects.
# Is this line in particular needed?
builtinValues = set(__builtin__.__dict__.itervalues())
# Some objects in types are special __builtin__ sourced ones.
builtinValues.update(v for k, v in types.__dict__.iteritems() if k[0] != '_')
for k, v in self.scriptGlobals.iteritems():
if k == "__builtins__":
continue
valueType = type(v)
exportable = True
# Modules will have been imported from elsewhere.
if isinstance(v, types.ModuleType):
exportable = False
elif valueType in (types.ClassType, types.TypeType):
# Classes with valid modules will have been imported from elsewhere.
if v.__module__ != "__builtin__":
exportable = False
# Skip actual builtin objects.
elif v in builtinValues or issubclass(v, unittest.TestCase):
exportable = False
yield k, v, valueType, exportable
class ScriptDirectory(object):
scriptFileClass = ScriptFile
unitTest = True
dependencyResolutionPasses = 10
def __init__(self, baseDirPath=None, baseNamespace=None, delScriptGlobals=False):
# Script file objects indexed in different ways.
self.filesByPath = {}
self.filesByDirectory = {}
# Personal references to created namespaces.
self.namespaces = {}
self.classCreationCallback = None
self.validateScriptCallback = None
self.delScriptGlobals = delScriptGlobals
self.SetBaseDirectory(baseDirPath)
self.SetBaseNamespaceName(baseNamespace)
def __del__(self):
self.Unload()
def SetClassCreationCallback(self, ob):
self.classCreationCallback = ob
def SetValidateScriptCallback(self, ob):
self.validateScriptCallback = ob
def SetBaseDirectory(self, baseDirPath):
self.baseDirPath = baseDirPath
def SetBaseNamespaceName(self, baseNamespaceName):
self.baseNamespaceName = baseNamespaceName
def GetNamespacePath(self, dirPath):
namespace = self.baseNamespaceName
relativeDirPath = os.path.relpath(dirPath, self.baseDirPath)
if relativeDirPath != ".":
namespace += "."+ relativeDirPath.replace(os.path.sep, ".")
return namespace
def Load(self):
## Pass 1: Load all the valid scripts under the given directory.
self.LoadDirectory(self.baseDirPath)
## Pass 2: Execute the scripts, ordering for dependencies and then add the namespace entries.
scriptFilesToLoad = set(self.filesByPath.itervalues())
attemptsLeft = self.dependencyResolutionPasses
while len(scriptFilesToLoad) and attemptsLeft > 0:
logger.debug("ScriptDirectory.Load dependency resolution attempts left %d", attemptsLeft)
if len(scriptFilesToLoad) == 1:
attemptsLeft = 1
scriptFilesLoaded = set()
for scriptFile in scriptFilesToLoad:
if self.RunScript(scriptFile):
scriptFilesLoaded.add(scriptFile)
# Update the set of scripts which have yet to be loaded.
scriptFilesToLoad -= scriptFilesLoaded
attemptsLeft -= 1
if len(scriptFilesToLoad):
logger.error("ScriptDirectory.Load failed to resolve dependencies")
# Log information about the problematic script files.
for scriptFile in scriptFilesToLoad:
scriptFile.LogLastError()
return False
return True
def LoadDirectory(self, dirPath):
logger.debug("LoadDirectory %s", dirPath)
namespace = self.GetNamespacePath(dirPath)
for entryName in os.listdir(dirPath):
if entryName == ".svn":
continue
entryPath = os.path.join(dirPath, entryName)
if os.path.isdir(entryPath):
self.LoadDirectory(entryPath)
elif os.path.isfile(entryPath):
if not entryName.endswith(".py") or entryName.endswith("_unittest.py"):
continue
scriptFile = self.LoadScript(entryPath, namespace)
self.RegisterScript(scriptFile)
else:
logger.error("Unrecognised type of directory entry %s", entryPath)
def Unload(self):
if not len(self.filesByPath) and not len(self.namespaces):
return
logger.debug("Cleaning up after removed directory '%s'", self.baseDirPath)
for k, scriptFile in self.filesByPath.items():
self.UnloadScript(scriptFile)
del self.filesByPath[k]
namespacePaths = self.namespaces.keys()
namespacePaths.sort()
namespacePaths.reverse()
for namespacePath in namespacePaths:
self.DestroyNamespace(namespacePath)
def GetNamespace(self, namespaceName):
return self.namespaces[namespaceName]
def CreateNamespace(self, namespaceName, filePath):
module = self.namespaces.get(namespaceName, None)
if module is not None:
if filePath in module.__file__:
raise RuntimeError("Namespace already exists", namespaceName)
return module
if namespaceName in sys.modules:
raise RuntimeError("Namespace already occupied", namespaceName)
parts = namespaceName.rsplit(".", 1)
if len(parts) == 2:
baseNamespaceName, moduleName = parts
baseNamespace = self.CreateNamespace(baseNamespaceName, filePath)
else:
baseNamespaceName, moduleName = None, parts[0]
baseNamespace = None
logger.info("Creating namespace '%s'", namespaceName)
module = imp.new_module(namespaceName)
# module.__name__ = moduleName
# Our modules don't map to files. Have a placeholder.
module.__file__ = ""
module.__package__ = baseNamespaceName
self.namespaces[namespaceName] = module
sys.modules[namespaceName] = module
if baseNamespace is not None:
setattr(baseNamespace, moduleName, module)
return module
def DestroyNamespace(self, namespaceName):
module = self.namespaces.get(namespaceName, None)
if module.__file__:
logger.debug("DestroyNamespace '%s' skipping, still used %s", namespaceName, module.__file__)
return
logger.debug("DestroyNamespace '%s'", namespaceName)
del sys.modules[namespaceName]
del self.namespaces[namespaceName]
def RegisterScript(self, scriptFile):
# Index the file by its full path.
self.filesByPath[scriptFile.filePath] = scriptFile
dirPath = os.path.dirname(scriptFile.filePath)
relativeDirPath = os.path.relpath(dirPath, self.baseDirPath)
# Index the file with other files in the same directory.
if relativeDirPath not in self.filesByDirectory:
self.filesByDirectory[relativeDirPath] = []
self.filesByDirectory[relativeDirPath].append(scriptFile)
def UnregisterScript(self, scriptFile):
dirPath = os.path.dirname(scriptFile.filePath)
relativeDirPath = os.path.relpath(dirPath, self.baseDirPath)
self.filesByDirectory[relativeDirPath].remove(scriptFile)
if not len(self.filesByDirectory[relativeDirPath]):
del self.filesByDirectory[relativeDirPath]
del self.filesByPath[scriptFile.filePath]
def FindScript(self, filePath):
if filePath in self.filesByPath:
return self.filesByPath[filePath]
def LoadScript(self, filePath, namespacePath):
logger.debug("LoadScript %s", filePath)
return self.scriptFileClass(filePath, namespacePath, delGlobals=self.delScriptGlobals)
def RunScript(self, scriptFile, tentative=False):
logger.debug("RunScript %s", scriptFile.filePath)
if not scriptFile.Run():
logger.debug("RunScript failed")
return False
# Give whatever is using the framework to analyse and reject script changes.
if not self.BroadcastValidateScriptEvent(scriptFile):
return False
if self.unitTest and not scriptFile.UnitTest():
logger.debug("RunScript tests failed or errored")
return False
if not tentative:
logger.debug("RunScript exporting to namespace %s", scriptFile.namespacePath)
namespace = self.CreateNamespace(scriptFile.namespacePath, scriptFile.filePath)
self.SetModuleAttributes(scriptFile, namespace)
return True
def BroadcastValidateScriptEvent(self, scriptFile):
if self.validateScriptCallback:
try:
if type(self.validateScriptCallback) is tuple:
getattr(self.validateScriptCallback[0], self.validateScriptCallback[1])(scriptFile)
else:
self.validateScriptCallback(scriptFile)
except ReferenceError:
self.validateScriptCallback = None
except Exception:
scriptFile.lastError = traceback.format_exception(*sys.exc_info())
return False
return True
def UnloadScript(self, scriptFile, force=False):
namespace = self.GetNamespace(scriptFile.namespacePath)
if self.RemoveModuleAttributes(scriptFile, namespace):
return True
return False
def SetModuleAttributes(self, scriptFile, namespace, overwritableAttributes=set()):
moduleName = namespace.__name__
# Track what files have contributed to the namespace.
if scriptFile.filePath not in namespace.__file__:
if len(namespace.__file__):
namespace.__file__ += ";"
namespace.__file__ += scriptFile.filePath
namespaceContributions = set()
for k, v, valueType, exportable in scriptFile.GetExportableAttributes():
logger.debug("InsertModuleAttribute %s.%s exported=%s", moduleName, k, exportable)
if not exportable:
logger.debug("Added a non-exported global: %s %s", k, valueType)
continue
# By default we never overwrite. This way we can identify duplicate contributions.
if hasattr(namespace, k) and k not in overwritableAttributes and k != "__doc__":
logger.error("Duplicate namespace contribution for '%s.%s' from '%s', our class = %s", moduleName, k, scriptFile.filePath, v.__file__ == scriptFile.filePath)
continue
if valueType in (types.ClassType, types.TypeType):
v.__module__ = moduleName
v.__file__ = scriptFile.filePath
setattr(namespace, k, v)
namespaceContributions.add(k)
if type(v) in (types.TypeType, types.ClassType):
self.BroadcastClassCreationEvent(v)
# print namespace, k, type(v)
scriptFile.SetNamespaceContributions(namespaceContributions)
def BroadcastClassCreationEvent(self, *args):
if self.classCreationCallback:
try:
if type(self.classCreationCallback) is tuple:
getattr(self.classCreationCallback[0], self.classCreationCallback[1])(*args)
else:
self.classCreationCallback(*args)
except ReferenceError:
self.classCreationCallback = None
except Exception:
logger.exception("Error broadcasting class creation")
def RemoveModuleAttributes(self, scriptFile, namespace):
logger.debug("RemoveModuleAttributes %s", scriptFile.filePath)
if scriptFile.namespaceContributions is None:
return True
paths = namespace.__file__.split(";")
if scriptFile.filePath not in paths:
raise RuntimeError("Namespace mismatch")
paths.remove(scriptFile.filePath)
namespace.__file__ = ";".join(paths)
for k in scriptFile.namespaceContributions:
delattr(namespace, k)
return True