Friday, August 9, 2013

wlst NameError: cd (using WLST as a Jython module)

I am defining a function in a python module acmelibrary.py:
def createMailSession(theMailSessionName, adminName, clusterName):
    cd('/')

and I invoke it like this:
from acmelibrary import createMailSession

createMailSession(theMailSessionName, adminName, clusterName)


Much to my dismay, the cd('/') statement works perfectly when invoked in the main module, but not in the library:
NameError: cd

Workaround:

in all WLST scripts there is a variable WLS of type com.oracle.cie.domain.script.jython.WLScriptContext, and you have to pass it as a parameter to the createMailSession:

def createMailSession(wls, theMailSessionName, adminName, clusterName):
    wls.cd('/')
A more radical approach is to create a wl.py module:
from weblogic.management.scripting.utils import WLSTUtil
import sys
origPrompt = sys.ps1
theInterpreter = WLSTUtil.ensureInterpreter();
WLSTUtil.ensureWLCtx(theInterpreter)
execfile(WLSTUtil.getWLSTScriptPath())
execfile(WLSTUtil.getOfflineWLSTScriptPath())
exec(WLSTUtil.getOfflineWLSTScriptForModule())
execfile(WLSTUtil.getWLSTCommonModulePath())
theInterpreter = None
sys.ps1 = origPrompt
modules = WLSTUtil.getWLSTModules()
for mods in modules:
  execfile(mods.getAbsolutePath())
wlstPrompt = "false"  

and in your acmelibrary.py you start with a
import wl


at this point your code can do
def createMailSession(theMailSessionName, adminName, clusterName):
    wl.cd('/')


See also http://docs.oracle.com/cd/E15051_01/wls/docs103/config_scripting/using_WLST.html#wp1094333

No comments: