PageRenderTime 32ms CodeModel.GetById 12ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/galaxy/web/controllers/async.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 138 lines | 118 code | 12 blank | 8 comment | 3 complexity | 8f31dd26033f301dc9a03d7aad7c601c MD5 | raw file
  1"""
  2Upload class
  3"""
  4
  5from galaxy.web.base.controller import *
  6
  7from galaxy import jobs, util, datatypes, web
  8
  9import logging, urllib, sys
 10from galaxy.util.hash_util import *
 11
 12log = logging.getLogger( __name__ )
 13
 14class ASync( BaseUIController ):
 15
 16    @web.expose
 17    def default(self, trans, tool_id=None, data_id=None, data_secret=None, **kwd):
 18        """Catches the tool id and redirects as needed"""
 19        return self.index( trans, tool_id=tool_id, data_id=data_id, data_secret=data_secret, **kwd)
 20
 21    @web.expose
 22    def index(self, trans, tool_id=None, data_secret=None, **kwd):
 23        """Manages ascynchronous connections"""
 24
 25        if tool_id is None:
 26            return "tool_id argument is required"
 27        tool_id=str(tool_id)
 28        #log.debug('async params -> %s' % kwd)
 29
 30        # redirect to main when getting no parameters
 31        if not kwd:
 32            return trans.response.send_redirect( "/index" )
 33
 34        history = trans.get_history( create=True )
 35        params  = util.Params(kwd, sanitize=False) 
 36        STATUS = params.STATUS
 37        URL = params.URL
 38        data_id = params.data_id
 39
 40        log.debug('async dataid -> %s' % data_id)
 41        trans.log_event( 'Async dataid -> %s' % str(data_id) )
 42
 43        # initialize the tool
 44        toolbox = self.get_toolbox()
 45        tool    = toolbox.tools_by_id.get(tool_id, '')
 46        if not tool:
 47            return "Tool with id %s not found" % tool_id
 48        
 49        #
 50        # we have an incoming data_id
 51        #
 52        if data_id:
 53            if not URL:
 54                return "No URL parameter was submitted for data %s" % data_id
 55            data = trans.sa_session.query( trans.model.HistoryDatasetAssociation ).get( data_id )
 56           
 57            if not data:
 58                return "Data %s does not exist or has already been deleted" % data_id
 59
 60            if STATUS == 'OK':
 61                key = hmac_new( trans.app.config.tool_secret, "%d:%d" % ( data.id, data.history_id ) )
 62                if key != data_secret:
 63                    return "You do not have permission to alter data %s." % data_id
 64                # push the job into the queue
 65                data.state = data.blurb = data.states.RUNNING
 66                log.debug('executing tool %s' % tool.id)
 67                trans.log_event( 'Async executing tool %s' % tool.id, tool_id=tool.id )
 68                galaxy_url  = trans.request.base + '/async/%s/%s/%s' % ( tool_id, data.id, key )
 69                galaxy_url = params.get("GALAXY_URL",galaxy_url)
 70                params = dict( url=URL, GALAXY_URL=galaxy_url )
 71                # Assume there is exactly one output file possible
 72                params[tool.outputs.keys()[0]] = data.id
 73                tool.execute( trans, incoming=params )
 74            else:
 75                log.debug('async error -> %s' % STATUS)
 76                trans.log_event( 'Async error -> %s' % STATUS )
 77                data.state = data.blurb = jobs.JOB_ERROR
 78                data.info  = "Error -> %s" % STATUS
 79            
 80            trans.sa_session.flush()
 81            
 82            return "Data %s with status %s received. OK" % (data_id, STATUS)
 83   
 84        #            
 85        # no data_id must be parameter submission
 86        #
 87        if not data_id and len(params)>3:
 88            
 89            if params.galaxyFileFormat == 'wig':
 90                GALAXY_TYPE = 'wig'
 91            else:
 92                GALAXY_TYPE = params.GALAXY_TYPE  or 'interval'
 93                
 94            GALAXY_NAME  = params.GALAXY_NAME  or '%s query' % tool.name
 95            GALAXY_INFO  = params.GALAXY_INFO  or params.galaxyDescription or ''
 96            GALAXY_BUILD = params.GALAXY_BUILD or params.galaxyFreeze or 'hg17'
 97            
 98            #data = datatypes.factory(ext=GALAXY_TYPE)()
 99            #data.ext   = GALAXY_TYPE
100            #data.name  = GALAXY_NAME
101            #data.info  = GALAXY_INFO
102            #data.dbkey = GALAXY_BUILD
103            #data.state = jobs.JOB_OK
104            #history.datasets.add_dataset( data )
105            
106            data = trans.app.model.HistoryDatasetAssociation( create_dataset=True, sa_session=trans.sa_session, extension=GALAXY_TYPE )
107            trans.app.security_agent.set_all_dataset_permissions( data.dataset, trans.app.security_agent.history_get_default_permissions( trans.history ) )
108            data.name = GALAXY_NAME
109            data.dbkey = GALAXY_BUILD
110            data.info = GALAXY_INFO
111            trans.sa_session.add( data ) #Need to add data to session before setting state (setting state requires that the data object is in the session, but this may change)
112            data.state = data.states.NEW
113            open( data.file_name, 'wb' ).close() #create the file
114            trans.history.add_dataset( data, genome_build=GALAXY_BUILD )
115            trans.sa_session.add( trans.history )
116            trans.sa_session.flush()
117            trans.log_event( "Added dataset %d to history %d" %(data.id, trans.history.id ), tool_id=tool_id )
118
119            try:
120                key = hmac_new( trans.app.config.tool_secret, "%d:%d" % ( data.id, data.history_id ) )
121                galaxy_url  = trans.request.base + '/async/%s/%s/%s' % ( tool_id, data.id, key )
122                params.update( { 'GALAXY_URL' :galaxy_url } )
123                params.update( { 'data_id' :data.id } )
124                url  = tool.action + '?' + urllib.urlencode( params.flatten() )
125                log.debug("connecting to -> %s" % url)
126                trans.log_event( "Async connecting to -> %s" % url )
127                text =  urllib.urlopen(url).read(-1)
128                text = text.strip()
129                if not text.endswith('OK'):
130                    raise Exception, text
131                data.state = data.blurb = data.states.RUNNING
132            except Exception, e:
133                data.info  = str(e)
134                data.state = data.blurb = data.states.ERROR
135            
136            trans.sa_session.flush()
137
138        return trans.fill_template( 'tool_executed.mako', history=history, toolbox=toolbox, tool=tool, util=util, out_data={} )