6
|
1 #!/usr/bin/env python
|
|
2 import json
|
|
3 import optparse
|
|
4 import urllib
|
|
5 import os.path
|
|
6 import os
|
|
7 from operator import itemgetter
|
|
8 import tarfile
|
|
9
|
|
10 __version__ = "1.0.0"
|
|
11 CHUNK_SIZE = 2**20 #1mb
|
|
12 VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
|
|
13
|
|
14
|
|
15 def splitext(path):
|
|
16 for ext in ['.tar.gz', '.tar.bz2']:
|
|
17 if path.endswith(ext):
|
|
18 path, ext = path[:-len(ext)], path[-len(ext):]
|
|
19 break
|
|
20 else:
|
|
21 path, ext = os.path.splitext(path)
|
|
22 return path, ext[1:]
|
|
23
|
|
24
|
|
25 def chunk_write( source_stream, target_stream, source_method = "read", target_method="write" ):
|
|
26 source_method = getattr( source_stream, source_method )
|
|
27 target_method = getattr( target_stream, target_method )
|
|
28 while True:
|
|
29 chunk = source_method( CHUNK_SIZE )
|
|
30 if chunk:
|
|
31 target_method( chunk )
|
|
32 else:
|
|
33 break
|
|
34
|
|
35
|
|
36 def deconstruct_multi_filename( multi_filename ):
|
|
37 keys = [ 'primary', 'id', 'name', 'visible', 'file_type' ]
|
|
38 return ( dict( zip( keys, multi_filename.split('_') ) ) )
|
|
39
|
|
40
|
|
41 def construct_multi_filename( id, name, file_type ):
|
|
42 """ Implementation of *Number of Output datasets cannot be determined until tool run* from documentation_.
|
|
43 .. _documentation: http://wiki.galaxyproject.org/Admin/Tools/Multiple%20Output%20Files
|
|
44 """
|
|
45 filename = "%s_%s_%s_%s_%s" % ( 'primary', id, name, 'visible', file_type )
|
|
46 return filename
|
|
47
|
|
48
|
|
49 def download_from_query( query_data, target_output_filename ):
|
|
50 """ Download file from the json data and write it to target_output_filename.
|
|
51 """
|
|
52 query_url = query_data.get( 'url' )
|
|
53 query_file_type = query_data.get( 'extension' )
|
|
54 query_stream = urllib.urlopen( query_url )
|
|
55 output_stream = open( target_output_filename, 'wb' )
|
|
56 chunk_write( query_stream, output_stream )
|
|
57 query_stream.close()
|
|
58 output_stream.close()
|
|
59
|
|
60 def store_file_from_archive( file_object, target_output_filename ):
|
|
61 """ Store file after extracting from archive and organize them as a collection using the structure
|
|
62 (collection-name)_(file-name).ext as file name
|
|
63 """
|
|
64 output_stream = open( target_output_filename, 'wb' )
|
|
65 chunk_write( file_object.read(), output_stream )
|
|
66 output_stream.close()
|
|
67
|
|
68
|
|
69 def download_extra_data( query_ext_data, base_path ):
|
|
70 """ Download any extra data defined in the JSON.
|
|
71 NOTE: the "path" value is a relative path to the file on our
|
|
72 file system. This is slightly dangerous and we should make every effort
|
|
73 to avoid a malicious absolute path to write the file elsewhere on the
|
|
74 filesystem.
|
|
75 """
|
|
76 for ext_data in query_ext_data:
|
|
77 if not os.path.exists( base_path ):
|
|
78 os.mkdir( base_path )
|
|
79 query_stream = urllib.urlopen( ext_data.get( 'url' ) )
|
|
80 ext_path = ext_data.get( 'path' )
|
|
81 os.makedirs( os.path.normpath( '/'.join( [ base_path, os.path.dirname( ext_path ) ] ) ) )
|
|
82 output_stream = open( os.path.normpath( '/'.join( [ base_path, ext_path ] ) ), 'wb' )
|
|
83 chunk_write( query_stream, output_stream )
|
|
84 query_stream.close()
|
|
85 output_stream.close()
|
|
86
|
|
87
|
|
88 def metadata_to_json_for_archive_entry( dataset_id, extension, metaname, filename, ds_type='dataset', primary=False ):
|
|
89 """ Return line separated JSON """
|
|
90 meta_dict = dict( type = ds_type,
|
|
91 ext = extension,
|
|
92 filename = filename,
|
|
93 name = metaname,
|
|
94 metadata = {} )
|
|
95 if primary:
|
|
96 meta_dict[ 'base_dataset_id' ] = dataset_id
|
|
97 else:
|
|
98 meta_dict[ 'dataset_id' ] = dataset_id
|
|
99 return "%s\n" % json.dumps( meta_dict )
|
|
100
|
|
101
|
|
102 def metadata_to_json( dataset_id, metadata, filename, ds_type='dataset', primary=False):
|
|
103 """ Return line separated JSON """
|
|
104 meta_dict = dict( type = ds_type,
|
|
105 ext = metadata.get( 'extension' ),
|
|
106 filename = filename,
|
|
107 name = metadata.get( 'name' ),
|
|
108 metadata = metadata.get( 'metadata', {} ) )
|
|
109 if metadata.get( 'extra_data', None ):
|
|
110 meta_dict[ 'extra_files' ] = '_'.join( [ filename, 'files' ] )
|
|
111 if primary:
|
|
112 meta_dict[ 'base_dataset_id' ] = dataset_id
|
|
113 else:
|
|
114 meta_dict[ 'dataset_id' ] = dataset_id
|
|
115 return "%s\n" % json.dumps( meta_dict )
|
|
116
|
|
117
|
|
118 def download_files_and_write_metadata(query_item, json_params, output_base_path, metadata_parameter_file, primary, appdata_path):
|
|
119 """ Main work function that operates on the JSON representation of
|
|
120 one dataset and its metadata. Returns True.
|
|
121 """
|
|
122 dataset_url, output_filename, \
|
|
123 extra_files_path, file_name, \
|
|
124 ext, out_data_name, \
|
|
125 hda_id, dataset_id = set_up_config_values(json_params)
|
|
126 extension = query_item.get( 'extension' )
|
|
127 filename = query_item.get( 'url' )
|
|
128 extra_data = query_item.get( 'extra_data', None )
|
|
129 if primary:
|
|
130 filename = ''.join( c in VALID_CHARS and c or '-' for c in filename )
|
|
131 name = construct_multi_filename( hda_id, filename, extension )
|
|
132 target_output_filename = os.path.normpath( '/'.join( [ output_base_path, name ] ) )
|
|
133 metadata_parameter_file.write( metadata_to_json( dataset_id, query_item,
|
|
134 target_output_filename,
|
|
135 ds_type='new_primary_dataset',
|
|
136 primary=primary) )
|
|
137 else:
|
|
138 target_output_filename = output_filename
|
|
139 metadata_parameter_file.write( metadata_to_json( dataset_id, query_item,
|
|
140 target_output_filename,
|
|
141 ds_type='dataset',
|
|
142 primary=primary) )
|
|
143 download_from_query( query_item, target_output_filename )
|
|
144 if extra_data:
|
|
145 extra_files_path = ''.join( [ target_output_filename, 'files' ] )
|
|
146 download_extra_data( extra_data, extra_files_path )
|
|
147
|
10
|
148 """ the following code handles archives and decompress them in a collection """
|
6
|
149 check_ext = ""
|
|
150 if ( fname.endswith( "gz" ) ):
|
|
151 check_ext = "r:gz"
|
|
152 elif ( fname.endswith( "bz2" ) ):
|
|
153 check_ext = "r:bz2"
|
|
154 elif ( fname.endswith( "tar" ) ):
|
|
155 check_ext = "r:"
|
|
156 if ( bool( check_ext and check_ext.strip() ) ):
|
|
157 with tarfile.open( target_output_filename, check_ext ) as tf:
|
|
158 for entry in tf:
|
|
159 fileobj = tf.extractfile( entry )
|
|
160 if entry.isfile():
|
10
|
161
|
|
162 #dataset_url, output_filename, \
|
|
163 # extra_files_path, file_name, \
|
|
164 # ext, out_data_name, \
|
|
165 # hda_id, dataset_id = set_up_config_values(json_params)
|
|
166
|
6
|
167 filename = os.path.basename( entry.name )
|
|
168 extension = splitext( filename )
|
|
169 extra_data = None
|
|
170 #target_output_filename = output_filename
|
10
|
171 # (?P<archive_name>.*)_(?P<file_name>.*)\..*
|
6
|
172 filename_with_collection_prefix = query_item.get( 'name' ) + "_" + filename
|
|
173 target_output_filename = os.path.join(appdata_path, filename_with_collection_prefix)
|
10
|
174
|
|
175 #metadata_parameter_file.write( metadata_to_json_for_archive_entry( dataset_id, extension,
|
|
176 # filename, target_output_filename,
|
|
177 # ds_type='dataset',
|
|
178 # primary=primary) )
|
|
179
|
6
|
180 store_file_from_archive( fileobj, target_output_filename )
|
|
181
|
|
182 return True
|
|
183
|
|
184
|
|
185 def set_up_config_values():
|
|
186 extra_files_path, file_name, ext, out_data_name, hda_id, dataset_id = \
|
|
187 itemgetter('extra_files_path', 'file_name', 'ext', 'out_data_name', 'hda_id', 'dataset_id')(output_data[0])
|
|
188
|
|
189 def set_up_config_values(json_params):
|
|
190 """ Parse json_params file and return a tuple of necessary configuration
|
|
191 values.
|
|
192 """
|
|
193 datasource_params = json_params.get( 'param_dict' )
|
|
194 dataset_url = datasource_params.get( 'URL' )
|
|
195 output_filename = datasource_params.get( 'output1', None )
|
|
196 output_data = json_params.get( 'output_data' )
|
|
197 extra_files_path, file_name, ext, out_data_name, hda_id, dataset_id = \
|
|
198 itemgetter('extra_files_path', 'file_name', 'ext', 'out_data_name', 'hda_id', 'dataset_id')(output_data[0])
|
|
199 return (dataset_url, output_filename,
|
|
200 extra_files_path, file_name,
|
|
201 ext, out_data_name,
|
|
202 hda_id, dataset_id)
|
|
203
|
|
204
|
|
205 def download_from_json_data( options, args ):
|
|
206 """ Parse the returned JSON data and download files. Write metadata
|
|
207 to flat JSON file.
|
|
208 """
|
|
209 output_base_path = options.path
|
|
210 appdata_path = options.appdata
|
|
211 if not os.path.exists(appdata_path):
|
|
212 os.makedirs(appdata_path)
|
|
213
|
|
214 # read tool job configuration file and parse parameters we need
|
|
215 json_params = json.loads( open( options.json_param_file, 'r' ).read() )
|
|
216 dataset_url, output_filename, \
|
|
217 extra_files_path, file_name, \
|
|
218 ext, out_data_name, \
|
|
219 hda_id, dataset_id = set_up_config_values(json_params)
|
|
220 # line separated JSON file to contain all dataset metadata
|
|
221 metadata_parameter_file = open( json_params['job_config']['TOOL_PROVIDED_JOB_METADATA_FILE'], 'wb' )
|
|
222
|
|
223 # get JSON response from data source
|
|
224 # TODO: make sure response is not enormous
|
|
225 query_params = json.loads(urllib.urlopen( dataset_url ).read())
|
|
226 # download and write files
|
|
227 primary = False
|
|
228 # query_item, hda_id, output_base_path, dataset_id
|
|
229 for query_item in query_params:
|
|
230 if isinstance( query_item, list ):
|
|
231 # TODO: do something with the nested list as a collection
|
|
232 for query_subitem in query_item:
|
|
233 primary = download_files_and_write_metadata(query_subitem, json_params, output_base_path,
|
|
234 metadata_parameter_file, primary, appdata_path)
|
|
235
|
|
236 elif isinstance( query_item, dict ):
|
|
237 primary = download_files_and_write_metadata(query_item, json_params, output_base_path,
|
|
238 metadata_parameter_file, primary, appdata_path)
|
|
239 metadata_parameter_file.close()
|
|
240
|
|
241 def __main__():
|
|
242 """ Read the JSON return from a data source. Parse each line and request
|
|
243 the data, download to "newfilepath", and write metadata.
|
|
244
|
|
245 Schema
|
|
246 ------
|
|
247
|
|
248 [ {"url":"http://url_of_file",
|
|
249 "name":"encode WigData",
|
|
250 "extension":"wig",
|
|
251 "metadata":{"db_key":"hg19"},
|
|
252 "extra_data":[ {"url":"http://url_of_ext_file",
|
|
253 "path":"rel/path/to/ext_file"}
|
|
254 ]
|
|
255 }
|
|
256 ]
|
|
257
|
|
258 """
|
|
259 # Parse the command line options
|
|
260 usage = "Usage: json_data_source_mod.py max_size --json_param_file filename [options]"
|
|
261 parser = optparse.OptionParser(usage = usage)
|
|
262 parser.add_option("-j", "--json_param_file", type="string",
|
|
263 action="store", dest="json_param_file", help="json schema return data")
|
|
264 parser.add_option("-p", "--path", type="string",
|
|
265 action="store", dest="path", help="new file path")
|
|
266 parser.add_option("-a", "--appdata", type="string",
|
|
267 action="store", dest="appdata", help="appdata folder name")
|
|
268 parser.add_option("-v", "--version", action="store_true", dest="version",
|
|
269 default=False, help="display version and exit")
|
|
270
|
|
271 (options, args) = parser.parse_args()
|
|
272 if options.version:
|
|
273 print __version__
|
|
274 else:
|
|
275 download_from_json_data( options, args )
|
|
276
|
|
277
|
|
278 if __name__ == "__main__": __main__()
|