1
|
1 #!/usr/bin/env python
|
|
2 # Retrieves data from external data source applications and stores in a dataset file.
|
|
3 # Data source application parameters are temporarily stored in the dataset file.
|
|
4 import socket, urllib, sys, os
|
|
5 from galaxy import eggs #eggs needs to be imported so that galaxy.util can find docutils egg...
|
|
6 from galaxy.util.json import from_json_string, to_json_string
|
|
7 import galaxy.model # need to import model before sniff to resolve a circular import dependency
|
|
8 from galaxy.datatypes import sniff
|
|
9 from galaxy.datatypes.registry import Registry
|
|
10 from galaxy.jobs import TOOL_PROVIDED_JOB_METADATA_FILE
|
|
11
|
|
12 assert sys.version_info[:2] >= ( 2, 4 )
|
|
13
|
|
14 def stop_err( msg ):
|
|
15 sys.stderr.write( msg )
|
|
16 sys.exit()
|
|
17
|
|
18 GALAXY_PARAM_PREFIX = 'GALAXY'
|
|
19 GALAXY_ROOT_DIR = os.path.realpath( os.path.join( os.path.split( os.path.realpath( __file__ ) )[0], '..', '..' ) )
|
|
20 GALAXY_DATATYPES_CONF_FILE = os.path.join( GALAXY_ROOT_DIR, 'datatypes_conf.xml' )
|
|
21
|
|
22 def load_input_parameters( filename, erase_file = True ):
|
|
23 datasource_params = {}
|
|
24 try:
|
|
25 json_params = from_json_string( open( filename, 'r' ).read() )
|
|
26 datasource_params = json_params.get( 'param_dict' )
|
|
27 except:
|
|
28 json_params = None
|
|
29 for line in open( filename, 'r' ):
|
|
30 try:
|
|
31 line = line.strip()
|
|
32 fields = line.split( '\t' )
|
|
33 datasource_params[ fields[0] ] = fields[1]
|
|
34 except:
|
|
35 continue
|
|
36 if erase_file:
|
|
37 open( filename, 'w' ).close() #open file for writing, then close, removes params from file
|
|
38 return json_params, datasource_params
|
|
39
|
|
40 def __main__():
|
|
41 filename = sys.argv[1]
|
|
42 try:
|
|
43 max_file_size = int( sys.argv[2] )
|
|
44 except:
|
|
45 max_file_size = 0
|
|
46
|
|
47 job_params, params = load_input_parameters( filename )
|
|
48 if job_params is None: #using an older tabular file
|
|
49 enhanced_handling = False
|
|
50 job_params = dict( param_dict = params )
|
|
51 job_params[ 'output_data' ] = [ dict( out_data_name = 'output',
|
|
52 ext = 'data',
|
|
53 file_name = filename,
|
|
54 extra_files_path = None ) ]
|
|
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 )
|
|
56 else:
|
|
57 enhanced_handling = True
|
|
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
|
|
59
|
|
60 datatypes_registry = Registry()
|
|
61 datatypes_registry.load_datatypes( root_dir = job_params[ 'job_config' ][ 'GALAXY_ROOT_DIR' ], config = job_params[ 'job_config' ][ 'GALAXY_DATATYPES_CONF_FILE' ] )
|
|
62
|
|
63 URL = params.get( 'URL', None ) #using exactly URL indicates that only one dataset is being downloaded
|
|
64 URL_method = params.get( 'URL_method', None )
|
|
65 simpleD = params.get('galaxyData')
|
|
66 # The Python support for fetching resources from the web is layered. urllib uses the httplib
|
|
67 # library, which in turn uses the socket library. As of Python 2.3 you can specify how long
|
|
68 # a socket should wait for a response before timing out. By default the socket module has no
|
|
69 # timeout and can hang. Currently, the socket timeout is not exposed at the httplib or urllib2
|
|
70 # levels. However, you can set the default timeout ( in seconds ) globally for all sockets by
|
|
71 # doing the following.
|
|
72 socket.setdefaulttimeout( 600 )
|
|
73 cur_filename = params.get('output')
|
|
74 outputfile = open( cur_filename, 'w' ).write( simpleD )
|
|
75
|
|
76 if __name__ == "__main__": __main__()
|
|
77
|
|
78
|
|
79
|