annotate tools/data_source/data_source.py @ 0:9071e359b9a3

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