def __init__( self , execution_config  ):
     """
     @param execution_config: the configuration of the Execution 
     @type execution_config: ExecutionConfig instance
     """
     self._cfg = Config()
     self.execution_config = execution_config
     self.execution_config_alias = self._cfg.getAliasFromConfig( self.execution_config )
     self.status_manager = StatusManager()
Exemple #2
0
def executionLoader(jobID=None, alias=None, execution_config=None):
    assert (
        bool(jobID) + bool(alias) + bool(execution_config) == 1
    ), "please provide either a jobID, an alias or an execution_config"
    from Mobyle.ConfigManager import Config

    cfg = Config()
    if not execution_config:
        if jobID:
            from Mobyle.JobState import normUri
            from urlparse import urlparse

            path = normUri(jobID)
            protocol, host, path, a, b, c = urlparse(path)
            if protocol == "http":
                raise NotImplementedError, "trying to instanciate an Execution system from a remote Job"
            if path[-9:] == "index.xml":
                path = path[:-10]
            adm = Admin(path)
            alias = adm.getExecutionAlias()
        if not alias:
            msg = "cant determine the Execution system for %s " % (jobID)
            u_log.error(msg)
            raise MobyleError(msg)
        try:
            execution_config = cfg.getExecutionConfigFromAlias(alias)
        except KeyError:
            msg = "the ExecutionConfig alias %s doesn't match with any alias in Config" % alias
            u_log.critical(msg)
            raise MobyleError(msg)
    klass_name = execution_config.execution_class_name
    try:
        module = __import__("Mobyle.Execution.%s" % klass_name)
    except ImportError, err:
        msg = "The Execution.%s module is missing" % klass_name
        u_log.critical(msg)
        raise MobyleError, msg
#   COPYING.LIB document.                                   #
#                                                           #
#############################################################
"""
Validator.py

This module is used to validate a Mobyle XML file (program or else?)
against the Relax NG schema and the schematron rules
- The Validator class
"""
from lxml import etree
import os
from subprocess import Popen, PIPE
from Mobyle.ConfigManager import Config

_cfg = Config()

from logging import getLogger

v_log = getLogger(__name__)

# defining paths for validation external stuff
mobschPath = os.path.join(_cfg.mobylehome(), "Schema", "mobyle.sch")
schprocPath = os.path.join(_cfg.mobylehome(), "Tools", "validation", "iso_svrl.xsl")
rbPath = os.path.join(_cfg.mobylehome(), "Tools", "validation", "remove_base.xsl")
jingPath = os.path.join(_cfg.mobylehome(), "Tools", "validation", "jing", "jing.jar")

SVRL_NS = "http://purl.oclc.org/dsdl/svrl"
svrl_validation_errors = etree.XPath("//svrl:failed-assert/svrl:text/text()", namespaces={"svrl": SVRL_NS})

# generating schematron validation xsl from its xsl generator
class ExecutionSystem(object):
    """
    abstract class
    manage the status by updating the file index.xml
    """

    def __init__( self , execution_config  ):
        """
        @param execution_config: the configuration of the Execution 
        @type execution_config: ExecutionConfig instance
        """
        self._cfg = Config()
        self.execution_config = execution_config
        self.execution_config_alias = self._cfg.getAliasFromConfig( self.execution_config )
        self.status_manager = StatusManager()
    
    def run( self , commandLine , dirPath , serviceName , jobState , xmlEnv = None):
        """
        @param execution_config: the configuration of the Execution 
        @type execution_config: ExecutionConfig instance
        @param commandLine: the command to be executed
        @type commandLine: String
        @param dirPath: the absolute path to directory where the job will be executed (normaly we are already in)
        @type dirPath: String
        @param serviceName: the name of the service
        @type serviceName: string
        @param jobState:
        @type jobState: a L{JobState} instance
        """
        self.jobState = jobState
        if dirPath[-1] == '/':
            dirPath = dirPath[:-1]
        jobKey = os.path.split( dirPath )[1]
        if os.getcwd() != os.path.abspath( dirPath ):
            msg = "the child process execute itself in a wrong directory"
            self._logError( dirPath , serviceName ,jobKey,
                            userMsg = "Mobyle internal server error" ,
                            logMsg = msg  )
            raise MobyleError , msg 
        
        protectedCommandLine = ''
        for c in commandLine:
            protectedCommandLine += '\\'+ c
            
        if xmlEnv is None:
            xmlEnv = {}
            
        dispatcher = self._cfg.getDispatcher()
        queue = dispatcher.getQueue( jobState )
        adm = Admin( dirPath )
        adm.setQueue( queue )
        adm.commit()
        
        new_path = ''
        binary_path = self._cfg.binary_path()
        if binary_path :
                new_path = ":".join( binary_path )        

        if xmlEnv.has_key( 'PATH' ) :      
                new_path = "%s:%s" %( xmlEnv[ 'PATH' ] , new_path )
        if new_path :
            xmlEnv[ 'PATH' ] = "%s:%s" %( new_path , os.environ[ 'PATH' ] ) 
        else:
            xmlEnv[ 'PATH' ] = os.environ[ 'PATH' ] 
        for var in os.environ.keys():
            if var != 'PATH':
                xmlEnv[ var ] = os.environ[ var ]
        self._returncode = None
        accounting = self._cfg.accounting()
        if accounting:
            beg_time = time.time()

        ###################################
        mobyleStatus = self._run( commandLine , dirPath , serviceName , jobKey , jobState , queue , xmlEnv )
        ###################################
        
        if accounting:
            end_time = time.time()
            elapsed_time = end_time - beg_time
            a_log = getLogger( 'Mobyle.account' )
            #%d trunc time to second
            #%f for millisecond
            a_log.info("%(serviceName)s/%(jobkey)s : %(exec_class)s/%(queue)s : %(beg_time)d-%(end_time)d %(ela_time)d : %(status)s" %{ 'serviceName':serviceName ,
                                                                                                                          'jobkey':jobKey,
                                                                                                                          'exec_class':self.execution_config.execution_class_name ,
                                                                                                                          'queue': queue,
                                                                                                                          'beg_time':beg_time ,
                                                                                                                          'end_time':end_time ,
                                                                                                                          'ela_time':elapsed_time ,
                                                                                                                          'status': mobyleStatus ,
                                                                                                                          }
                                                                                                              )
        self.status_manager.setStatus( dirPath , mobyleStatus )


    def getStatus( self ,  number ):
        """
        @param execution_config: a configuration object for this execution system
        @type execution_config: an ExecutionConfig subclass instance
        @param number:
        @type number:
        @return the status of the job
        @rtype:
        abstract method. this method must be implemented in child classes
        """
        raise NotImplementedError, "Must be Implemented in child classes"

    def kill(  self , number ):
        """
        kill the Job
        @param execution_config: a configuration object for this execution system
        @type execution_config: an ExecutionConfig subclass instance
        @param number:
        @type number:
        abstract method. this method must be implemented in child classes
        """
        raise NotImplementedError, "Must be Implemented in child classes"
            



    def _logError( self , dirPath , serviceName , jobKey , userMsg = None , logMsg = None ):


        if userMsg :
            self.status_manager.setStatus( dirPath, Status( code = 5 , message = userMsg ) )

        if logMsg :
            _log.error( "%s/%s : %s" %( serviceName ,
                                        jobKey ,
                                        logMsg
                                      )
                        )
    MOBYLEHOME = os.environ['MOBYLEHOME']
if not MOBYLEHOME:
    sys.exit('MOBYLEHOME must be defined in your environment')

if ( os.path.join( MOBYLEHOME , 'Src' ) ) not in sys.path:
    sys.path.append(  os.path.join( MOBYLEHOME , 'Src' )  )



import urllib, urllib2, cookielib #@UnresolvedImport

from Mobyle.ConfigManager  import Config
from Workflow import Parser

if __name__ == "__main__":
    cfg = Config()
    url = cfg.cgi_url()+'/workflow.py'

        
    # the cookie jar keeps the Mobyle session cookie in a warm place, so that 
    # all the HTTP requests connect to the same workspace 
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    mp = Parser()

    def get_str(params):
        data = urllib.urlencode(params)
        req = urllib2.Request(url, data)
        handle = opener.open(req)
        str = handle.read()
        return str.strip('\n')