Issue #1291 Implement RollBackImporter in Init.py to ensure the correct site modules are reloaded each time a smartInit is run

Change-Id: Ie9bfc66b5cba7639155944765701833ffa788660

Former-commit-id: bd5537e63d [formerly e226e41852] [formerly bd5537e63d [formerly e226e41852] [formerly e0b809db9f [formerly f00655f5c5fb53af0682313e5054124a459b9649]]]
Former-commit-id: e0b809db9f
Former-commit-id: a0402bb612 [formerly 56a04517a8]
Former-commit-id: d7de405506
This commit is contained in:
Ron Anderson 2012-10-24 13:34:10 -05:00
parent 60dfce2758
commit 119dd2f93e
3 changed files with 53 additions and 8 deletions

View file

@ -32,6 +32,10 @@ import SmartInitParams
from numpy import *
pytime = time
import RollBackImporter
rollbackImporter = RollBackImporter.RollBackImporter()
#--------------------------------------------------------------------------
# Main program that calls model-specific algorithms to generate ifp grids.
#--------------------------------------------------------------------------
@ -1289,9 +1293,6 @@ def runFromJava(dbName, model, validTime):
SmartInitParams.params['dbName'] = dbName
SmartInitParams.params['validTime'] = validTime
if sys.modules.has_key(model):
sys.modules.__delitem__(model)
mod = __import__(model)
mod.main()
rollbackImporter.rollback()

View file

@ -0,0 +1,40 @@
import sys, __builtin__, LogStream, re
class RollBackImporter:
def __init__(self):
"Creates an instance and installs as the global importer"
self.previousModules = sys.modules.copy()
self.realImport = __builtin__.__import__
__builtin__.__import__ = self._import
self.newModules = {}
self.pattern = re.compile("/.*?/edex/data/utility/.*?/(.*?)/")
def _import(self, name, globals=None, locals=None, fromlist=[], level=-1):
result = self.realImport(name, globals, locals, fromlist, level)
if hasattr(result, '__file__'):
match = re.match(self.pattern, result.__file__)
if match is not None:
level = match.group(1)
if level != 'base':
LogStream.logEvent("IMPORTING:", name, result)
self.newModules[result.__name__] = 1
else:
LogStream.logDebug("IGNORING BASE:", name, result)
else:
LogStream.logDebug("IGNORING NON-LOCALIZED:", name, result)
else:
LogStream.logDebug("IGNORING BUILTIN:", name, result)
return result
def rollback(self):
for modname in self.newModules.keys():
if not self.previousModules.has_key(modname):
# Force reload when modname next imported
LogStream.logEvent("UNLOADING:", modname, sys.modules[modname])
del(sys.modules[modname])
else:
LogStream.logDebug("SKIPPING PRELOADED:", modname)
self.newModules = {}

View file

@ -23,7 +23,7 @@ import java.util.Map;
import jep.JepException;
import com.raytheon.uf.common.python.PythonScript;
import com.raytheon.uf.common.python.PythonEval;
/**
* A wrapper for running smart init python
@ -33,6 +33,10 @@ import com.raytheon.uf.common.python.PythonScript;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Apr 28, 2008 njensen Initial creation
* Oct 23, 2012 #1291 randerso Changed to extend PythonEval instead
* of PythonScript so it doesn't get run
* causing it to load as module __main__
* instead of Init.
*
* </pre>
*
@ -40,7 +44,7 @@ import com.raytheon.uf.common.python.PythonScript;
* @version 1.0
*/
public class SmartInitScript extends PythonScript {
public class SmartInitScript extends PythonEval {
private static final String METHOD_NAME = "runFromJava";
@ -57,8 +61,8 @@ public class SmartInitScript extends PythonScript {
*/
public SmartInitScript(String aFilePath, String anIncludePath,
ClassLoader aClassLoader) throws JepException {
super(aFilePath, anIncludePath, aClassLoader);
jep.eval("import sys");
super(anIncludePath, aClassLoader);
jep.eval("from Init import *");
}
/**