annotate data_source.py @ 0:c3a93255587e draft

Uploaded
author crs4
date Thu, 06 Oct 2016 12:39:30 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c3a93255587e Uploaded
crs4
parents:
diff changeset
1 #!/usr/bin/env python
c3a93255587e Uploaded
crs4
parents:
diff changeset
2 # Retrieves data from external data source applications and stores in a dataset file.
c3a93255587e Uploaded
crs4
parents:
diff changeset
3 # Data source application parameters are temporarily stored in the dataset file.
c3a93255587e Uploaded
crs4
parents:
diff changeset
4 import socket, urllib, sys, os
c3a93255587e Uploaded
crs4
parents:
diff changeset
5 from galaxy import eggs #eggs needs to be imported so that galaxy.util can find docutils egg...
c3a93255587e Uploaded
crs4
parents:
diff changeset
6 from json import loads, dumps
c3a93255587e Uploaded
crs4
parents:
diff changeset
7 from galaxy.util import get_charset_from_http_headers
c3a93255587e Uploaded
crs4
parents:
diff changeset
8 import galaxy.model # need to import model before sniff to resolve a circular import dependency
c3a93255587e Uploaded
crs4
parents:
diff changeset
9 from galaxy.datatypes import sniff
c3a93255587e Uploaded
crs4
parents:
diff changeset
10 from galaxy.datatypes.registry import Registry
c3a93255587e Uploaded
crs4
parents:
diff changeset
11 from galaxy.jobs import TOOL_PROVIDED_JOB_METADATA_FILE
c3a93255587e Uploaded
crs4
parents:
diff changeset
12
c3a93255587e Uploaded
crs4
parents:
diff changeset
13 assert sys.version_info[:2] >= ( 2, 4 )
c3a93255587e Uploaded
crs4
parents:
diff changeset
14
c3a93255587e Uploaded
crs4
parents:
diff changeset
15 def stop_err( msg ):
c3a93255587e Uploaded
crs4
parents:
diff changeset
16 sys.stderr.write( msg )
c3a93255587e Uploaded
crs4
parents:
diff changeset
17 sys.exit()
c3a93255587e Uploaded
crs4
parents:
diff changeset
18
c3a93255587e Uploaded
crs4
parents:
diff changeset
19 GALAXY_PARAM_PREFIX = 'GALAXY'
c3a93255587e Uploaded
crs4
parents:
diff changeset
20 GALAXY_ROOT_DIR = os.path.realpath( os.path.join( os.path.split( os.path.realpath( __file__ ) )[0], '..', '..' ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
21 GALAXY_DATATYPES_CONF_FILE = os.path.join( GALAXY_ROOT_DIR, 'datatypes_conf.xml' )
c3a93255587e Uploaded
crs4
parents:
diff changeset
22
c3a93255587e Uploaded
crs4
parents:
diff changeset
23 def load_input_parameters( filename, erase_file = True ):
c3a93255587e Uploaded
crs4
parents:
diff changeset
24 datasource_params = {}
c3a93255587e Uploaded
crs4
parents:
diff changeset
25 try:
c3a93255587e Uploaded
crs4
parents:
diff changeset
26 json_params = loads( open( filename, 'r' ).read() )
c3a93255587e Uploaded
crs4
parents:
diff changeset
27 datasource_params = json_params.get( 'param_dict' )
c3a93255587e Uploaded
crs4
parents:
diff changeset
28 except:
c3a93255587e Uploaded
crs4
parents:
diff changeset
29 json_params = None
c3a93255587e Uploaded
crs4
parents:
diff changeset
30 for line in open( filename, 'r' ):
c3a93255587e Uploaded
crs4
parents:
diff changeset
31 try:
c3a93255587e Uploaded
crs4
parents:
diff changeset
32 line = line.strip()
c3a93255587e Uploaded
crs4
parents:
diff changeset
33 fields = line.split( '\t' )
c3a93255587e Uploaded
crs4
parents:
diff changeset
34 datasource_params[ fields[0] ] = fields[1]
c3a93255587e Uploaded
crs4
parents:
diff changeset
35 except:
c3a93255587e Uploaded
crs4
parents:
diff changeset
36 continue
c3a93255587e Uploaded
crs4
parents:
diff changeset
37 if erase_file:
c3a93255587e Uploaded
crs4
parents:
diff changeset
38 open( filename, 'w' ).close() #open file for writing, then close, removes params from file
c3a93255587e Uploaded
crs4
parents:
diff changeset
39 return json_params, datasource_params
c3a93255587e Uploaded
crs4
parents:
diff changeset
40
c3a93255587e Uploaded
crs4
parents:
diff changeset
41 def __main__():
c3a93255587e Uploaded
crs4
parents:
diff changeset
42 filename = sys.argv[1]
c3a93255587e Uploaded
crs4
parents:
diff changeset
43 try:
c3a93255587e Uploaded
crs4
parents:
diff changeset
44 max_file_size = int( sys.argv[2] )
c3a93255587e Uploaded
crs4
parents:
diff changeset
45 except:
c3a93255587e Uploaded
crs4
parents:
diff changeset
46 max_file_size = 0
c3a93255587e Uploaded
crs4
parents:
diff changeset
47
c3a93255587e Uploaded
crs4
parents:
diff changeset
48 job_params, params = load_input_parameters( filename )
c3a93255587e Uploaded
crs4
parents:
diff changeset
49
c3a93255587e Uploaded
crs4
parents:
diff changeset
50 if job_params is None: #using an older tabular file
c3a93255587e Uploaded
crs4
parents:
diff changeset
51 enhanced_handling = False
c3a93255587e Uploaded
crs4
parents:
diff changeset
52 job_params = dict( param_dict = params )
c3a93255587e Uploaded
crs4
parents:
diff changeset
53 job_params[ 'output_data' ] = [ dict( out_data_name = 'output',
c3a93255587e Uploaded
crs4
parents:
diff changeset
54 ext = 'data',
c3a93255587e Uploaded
crs4
parents:
diff changeset
55 file_name = filename,
c3a93255587e Uploaded
crs4
parents:
diff changeset
56 extra_files_path = None ) ]
c3a93255587e Uploaded
crs4
parents:
diff changeset
57 job_params[ 'job_config' ] = dict( GALAXY_ROOT_DIR=GALAXY_ROOT_DIR, GALAXY_DATATYPES_CONF_FILE=GALAXY_DATATYPES_CONF_FILE, TOOL_PROVIDED_JOB_METADATA_FILE = TOOL_PROVIDED_JOB_METADATA_FILE )
c3a93255587e Uploaded
crs4
parents:
diff changeset
58 else:
c3a93255587e Uploaded
crs4
parents:
diff changeset
59 enhanced_handling = True
c3a93255587e Uploaded
crs4
parents:
diff changeset
60 json_file = open( job_params[ 'job_config' ][ 'TOOL_PROVIDED_JOB_METADATA_FILE' ], 'w' ) #specially named file for output junk to pass onto set metadata
c3a93255587e Uploaded
crs4
parents:
diff changeset
61
c3a93255587e Uploaded
crs4
parents:
diff changeset
62 datatypes_registry = Registry()
c3a93255587e Uploaded
crs4
parents:
diff changeset
63 datatypes_registry.load_datatypes( root_dir = job_params[ 'job_config' ][ 'GALAXY_ROOT_DIR' ], config = job_params[ 'job_config' ][ 'GALAXY_DATATYPES_CONF_FILE' ] )
c3a93255587e Uploaded
crs4
parents:
diff changeset
64
c3a93255587e Uploaded
crs4
parents:
diff changeset
65 URL = params.get( 'URL', None ) #using exactly URL indicates that only one dataset is being downloaded
c3a93255587e Uploaded
crs4
parents:
diff changeset
66 URL_method = params.get( 'URL_method', None )
c3a93255587e Uploaded
crs4
parents:
diff changeset
67
c3a93255587e Uploaded
crs4
parents:
diff changeset
68 # The Python support for fetching resources from the web is layered. urllib uses the httplib
c3a93255587e Uploaded
crs4
parents:
diff changeset
69 # library, which in turn uses the socket library. As of Python 2.3 you can specify how long
c3a93255587e Uploaded
crs4
parents:
diff changeset
70 # a socket should wait for a response before timing out. By default the socket module has no
c3a93255587e Uploaded
crs4
parents:
diff changeset
71 # timeout and can hang. Currently, the socket timeout is not exposed at the httplib or urllib2
c3a93255587e Uploaded
crs4
parents:
diff changeset
72 # levels. However, you can set the default timeout ( in seconds ) globally for all sockets by
c3a93255587e Uploaded
crs4
parents:
diff changeset
73 # doing the following.
c3a93255587e Uploaded
crs4
parents:
diff changeset
74 socket.setdefaulttimeout( 600 )
c3a93255587e Uploaded
crs4
parents:
diff changeset
75
c3a93255587e Uploaded
crs4
parents:
diff changeset
76 for data_dict in job_params[ 'output_data' ]:
c3a93255587e Uploaded
crs4
parents:
diff changeset
77 cur_filename = data_dict.get( 'file_name', filename )
c3a93255587e Uploaded
crs4
parents:
diff changeset
78 cur_URL = params.get( '%s|%s|URL' % ( GALAXY_PARAM_PREFIX, data_dict[ 'out_data_name' ] ), URL )
c3a93255587e Uploaded
crs4
parents:
diff changeset
79 if not cur_URL:
c3a93255587e Uploaded
crs4
parents:
diff changeset
80 open( cur_filename, 'w' ).write( "" )
c3a93255587e Uploaded
crs4
parents:
diff changeset
81 stop_err( 'The remote data source application has not sent back a URL parameter in the request.' )
c3a93255587e Uploaded
crs4
parents:
diff changeset
82
c3a93255587e Uploaded
crs4
parents:
diff changeset
83 # The following calls to urllib.urlopen() will use the above default timeout
c3a93255587e Uploaded
crs4
parents:
diff changeset
84 try:
c3a93255587e Uploaded
crs4
parents:
diff changeset
85 if not URL_method or URL_method == 'get':
c3a93255587e Uploaded
crs4
parents:
diff changeset
86 page = urllib.urlopen( cur_URL )
c3a93255587e Uploaded
crs4
parents:
diff changeset
87 elif URL_method == 'post':
c3a93255587e Uploaded
crs4
parents:
diff changeset
88 page = urllib.urlopen( cur_URL, urllib.urlencode( params ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
89 except Exception, e:
c3a93255587e Uploaded
crs4
parents:
diff changeset
90 stop_err( 'The remote data source application may be off line, please try again later. Error: %s' % str( e ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
91 if max_file_size:
c3a93255587e Uploaded
crs4
parents:
diff changeset
92 file_size = int( page.info().get( 'Content-Length', 0 ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
93 if file_size > max_file_size:
c3a93255587e Uploaded
crs4
parents:
diff changeset
94 stop_err( 'The size of the data (%d bytes) you have requested exceeds the maximum allowed (%d bytes) on this server.' % ( file_size, max_file_size ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
95 #do sniff stream for multi_byte
c3a93255587e Uploaded
crs4
parents:
diff changeset
96 try:
c3a93255587e Uploaded
crs4
parents:
diff changeset
97 cur_filename, is_multi_byte = sniff.stream_to_open_named_file( page, os.open( cur_filename, os.O_WRONLY | os.O_CREAT ), cur_filename, source_encoding=get_charset_from_http_headers( page.headers ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
98 except Exception, e:
c3a93255587e Uploaded
crs4
parents:
diff changeset
99 stop_err( 'Unable to fetch %s:\n%s' % ( cur_URL, e ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
100
c3a93255587e Uploaded
crs4
parents:
diff changeset
101 #here import checks that upload tool performs
c3a93255587e Uploaded
crs4
parents:
diff changeset
102 if enhanced_handling:
c3a93255587e Uploaded
crs4
parents:
diff changeset
103 try:
c3a93255587e Uploaded
crs4
parents:
diff changeset
104 ext = sniff.handle_uploaded_dataset_file( filename, datatypes_registry, ext = data_dict[ 'ext' ], is_multi_byte = is_multi_byte )
c3a93255587e Uploaded
crs4
parents:
diff changeset
105 except Exception, e:
c3a93255587e Uploaded
crs4
parents:
diff changeset
106 stop_err( str( e ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
107 info = dict( type = 'dataset',
c3a93255587e Uploaded
crs4
parents:
diff changeset
108 dataset_id = data_dict[ 'dataset_id' ],
c3a93255587e Uploaded
crs4
parents:
diff changeset
109 ext = ext)
c3a93255587e Uploaded
crs4
parents:
diff changeset
110
c3a93255587e Uploaded
crs4
parents:
diff changeset
111 json_file.write( "%s\n" % dumps( info ) )
c3a93255587e Uploaded
crs4
parents:
diff changeset
112
c3a93255587e Uploaded
crs4
parents:
diff changeset
113 if __name__ == "__main__": __main__()