Mercurial > repos > iuc > data_manager_manual
comparison data_manager/data_manager_manual.py @ 2:1c1e228884d3 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_manual commit f2ab0315fa6df9e3e917bbc112e29f384c3affba"
author | iuc |
---|---|
date | Mon, 23 Sep 2019 10:53:17 -0400 |
parents | 6524e573d9c2 |
children | 31f44a9f507e |
comparison
equal
deleted
inserted
replaced
1:6524e573d9c2 | 2:1c1e228884d3 |
---|---|
5 import logging | 5 import logging |
6 import optparse | 6 import optparse |
7 import os | 7 import os |
8 import shutil | 8 import shutil |
9 import tempfile | 9 import tempfile |
10 import urllib2 | |
11 from xml.etree.ElementTree import tostring | 10 from xml.etree.ElementTree import tostring |
12 | 11 |
13 try: | 12 try: |
14 # For Python 3.0 and later | 13 # For Python 3.0 and later |
15 from shutil import unpack_archive | 14 from shutil import unpack_archive |
18 from setuptools.archive_util import unpack_archive | 17 from setuptools.archive_util import unpack_archive |
19 | 18 |
20 try: | 19 try: |
21 # For Python 3.0 and later | 20 # For Python 3.0 and later |
22 from urllib.request import urlretrieve | 21 from urllib.request import urlretrieve |
22 from urllib.parse import urlsplit | |
23 except ImportError: | 23 except ImportError: |
24 # Fall back to Python 2 imports | 24 # Fall back to Python 2 imports |
25 from urllib import urlretrieve | 25 from urllib import urlretrieve |
26 from urlparse import urlsplit | |
26 | 27 |
27 _log_name = __name__ | 28 _log_name = __name__ |
28 if _log_name == '__builtin__': | 29 if _log_name == '__builtin__': |
29 _log_name = 'toolshed.installed.manual.data.manager' | 30 _log_name = 'toolshed.installed.manual.data.manager' |
30 log = logging.getLogger( _log_name ) | 31 log = logging.getLogger(_log_name) |
31 | 32 |
32 | 33 |
33 # --- These methods are called by/within the Galaxy Application | 34 # --- These methods are called by/within the Galaxy Application |
34 def exec_before_job( app, inp_data, out_data, param_dict, tool=None, **kwd ): | 35 def exec_before_job(app, inp_data, out_data, param_dict, tool=None, **kwd): |
35 # Look for any data tables that haven't been defined for this data manager before and dynamically add them to Galaxy | 36 # Look for any data tables that haven't been defined for this data manager before and dynamically add them to Galaxy |
36 param_dict = dict( **param_dict ) | 37 param_dict = dict(**param_dict) |
37 data_tables_param = param_dict.get( 'data_tables', [] ) | 38 data_tables_param = param_dict.get('data_tables', []) |
38 if not isinstance( data_tables_param, list ): | 39 if not isinstance(data_tables_param, list): |
39 data_tables_param = [data_tables_param] | 40 data_tables_param = [data_tables_param] |
40 if tool: | 41 if tool: |
41 tool_shed_repository = tool.tool_shed_repository | 42 tool_shed_repository = tool.tool_shed_repository |
42 else: | 43 else: |
43 tool_shed_repository = None | 44 tool_shed_repository = None |
44 tdtm = None | 45 tdtm = None |
45 data_manager = app.data_managers.get_manager( tool.data_manager_id, None ) | 46 data_manager = app.data_managers.get_manager(tool.data_manager_id, None) |
46 for data_table_param in data_tables_param: | 47 for data_table_param in data_tables_param: |
47 data_table_name = data_table_param.get( 'data_table_name', None ) | 48 data_table_name = data_table_param.get('data_table_name') |
48 if data_table_name: | 49 if data_table_name: |
49 # get data table managed by this data Manager | 50 # get data table managed by this data Manager |
50 data_table = app.tool_data_tables.get_tables().get( str( data_table_name ), None ) | 51 data_table = app.tool_data_tables.get_tables().get(data_table_name) |
51 if data_table: | 52 if data_table: |
52 data_table_filename = data_table.get_filename_for_source( data_manager, None ) | 53 data_table_filename = data_table.get_filename_for_source(data_manager, None) |
53 if not data_table_filename: | 54 if not data_table_filename: |
54 if tdtm is None: | 55 if tdtm is None: |
55 from tool_shed.tools import data_table_manager | 56 from tool_shed.tools import data_table_manager |
56 tdtm = data_table_manager.ToolDataTableManager( app ) | 57 tdtm = data_table_manager.ToolDataTableManager(app) |
57 target_dir, tool_path, relative_target_dir = tdtm.get_target_install_dir( tool_shed_repository ) | 58 target_dir, tool_path, relative_target_dir = tdtm.get_target_install_dir(tool_shed_repository) |
58 # Dynamically add this data table | 59 # Dynamically add this data table |
59 log.debug( "Attempting to dynamically create a missing Tool Data Table named %s." % data_table_name ) | 60 log.debug("Attempting to dynamically create a missing Tool Data Table named %s." % data_table_name) |
60 repo_info = tdtm.generate_repository_info_elem_from_repository( tool_shed_repository, parent_elem=None ) | 61 repo_info = tdtm.generate_repository_info_elem_from_repository(tool_shed_repository, parent_elem=None) |
61 if repo_info is not None: | 62 if repo_info is not None: |
62 repo_info = tostring( repo_info ) | 63 repo_info = tostring(repo_info) |
63 tmp_file = tempfile.NamedTemporaryFile() | 64 tmp_file = tempfile.NamedTemporaryFile() |
64 tmp_file.write( __get_new_xml_definition( app, data_table, data_manager, repo_info, target_dir ) ) | 65 tmp_file.write(__get_new_xml_definition(app, data_table, data_manager, repo_info, target_dir)) |
65 tmp_file.flush() | 66 tmp_file.flush() |
66 app.tool_data_tables.add_new_entries_from_config_file( tmp_file.name, None, app.config.shed_tool_data_table_config, persist=True ) | 67 app.tool_data_tables.add_new_entries_from_config_file(tmp_file.name, None, app.config.shed_tool_data_table_config, persist=True) |
67 tmp_file.close() | 68 tmp_file.close() |
68 | 69 |
69 | 70 |
70 def __get_new_xml_definition( app, data_table, data_manager, repo_info=None, location_file_dir=None ): | 71 def __get_new_xml_definition(app, data_table, data_manager, repo_info=None, location_file_dir=None): |
71 sub_dict = { 'table_name': data_table.name, 'comment_char': '', 'columns': '', 'file_path': '' } | 72 sub_dict = {'table_name': data_table.name, 'comment_char': '', 'columns': '', 'file_path': ''} |
72 sub_dict.update( data_manager.get_tool_shed_repository_info_dict() ) | 73 sub_dict.update(data_manager.get_tool_shed_repository_info_dict()) |
73 if data_table.comment_char: | 74 if data_table.comment_char: |
74 sub_dict['comment_char'] = 'comment_char="%s"' % ( data_table.comment_char ) | 75 sub_dict['comment_char'] = 'comment_char="%s"' % (data_table.comment_char) |
75 for i, name in enumerate( data_table.get_column_name_list() ): | 76 for i, name in enumerate(data_table.get_column_name_list()): |
76 if name is not None: | 77 if name is not None: |
77 sub_dict['columns'] = "%s\n%s" % ( sub_dict['columns'], '<column name="%s" index="%s" />' % ( name, i ) ) | 78 sub_dict['columns'] = "%s\n%s" % (sub_dict['columns'], '<column name="%s" index="%s" />' % (name, i)) |
78 location_file_dir = location_file_dir or app.config.galaxy_data_manager_data_path | 79 location_file_dir = location_file_dir or app.config.galaxy_data_manager_data_path |
79 for filename in data_table.filenames.keys(): | 80 for filename in data_table.filenames.keys(): |
80 sub_dict['file_path'] = os.path.basename( filename ) | 81 sub_dict['file_path'] = os.path.basename(filename) |
81 sub_dict['file_path'] = os.path.join( location_file_dir, sub_dict['file_path'] ) # os.path.abspath? | 82 sub_dict['file_path'] = os.path.join(location_file_dir, sub_dict['file_path']) # os.path.abspath? |
82 if not os.path.exists( sub_dict['file_path'] ): | 83 if not os.path.exists(sub_dict['file_path']): |
83 # Create empty file | 84 # Create empty file |
84 log.debug( "Attempting to create a missing location file %s." % sub_dict['file_path'] ) | 85 log.debug("Attempting to create a missing location file %s." % sub_dict['file_path']) |
85 open( sub_dict['file_path'], 'wb+' ).close() | 86 open(sub_dict['file_path'], 'wb+').close() |
86 break | 87 break |
87 sub_dict[ 'repo_info' ] = repo_info or '' | 88 sub_dict['repo_info'] = repo_info or '' |
88 return """ | 89 return """ |
89 <tables><table name="%(table_name)s" %(comment_char)s> | 90 <tables><table name="%(table_name)s" %(comment_char)s> |
90 %(columns)s | 91 %(columns)s |
91 <file path="%(file_path)s" /> | 92 <file path="%(file_path)s" /> |
92 %(repo_info)s | 93 %(repo_info)s |
93 </table></tables> | 94 </table></tables> |
94 """ % sub_dict | 95 """ % sub_dict |
95 | 96 |
96 | 97 |
97 def galaxy_code_get_available_data_tables( trans ): | 98 def galaxy_code_get_available_data_tables(trans): |
98 # list of data tables | 99 # list of data tables |
99 return [ ( x, x, False ) for x in trans.app.tool_data_tables.get_tables().keys() ] | 100 return [(x, x, False) for x in trans.app.tool_data_tables.get_tables().keys()] |
100 | 101 |
101 | 102 |
102 def galaxy_code_get_available_data_table_columns( trans, data_table_name ): | 103 def galaxy_code_get_available_data_table_columns(trans, data_table_name): |
103 return [ ( x, x, True ) for x in trans.app.tool_data_tables.get( data_table_name ).get_column_name_list() ] | 104 return [(x, x, True) for x in trans.app.tool_data_tables.get(data_table_name).get_column_name_list()] |
104 # --- End Galaxy called Methods --- | 105 # --- End Galaxy called Methods --- |
105 | 106 |
106 | 107 |
107 def get_data_table_entries( params, galaxy_data_manager_data_path ): | 108 def get_data_table_entries(params, galaxy_data_manager_data_path): |
108 rval = {} | 109 rval = {} |
109 data_tables = params.get( 'data_tables', [] ) | 110 data_tables = params.get('data_tables', []) |
110 for data_table in data_tables: | 111 for data_table in data_tables: |
111 entry_dict = {} | 112 entry_dict = {} |
112 for column in data_table.get( 'columns', [] ): | 113 for column in data_table.get('columns', []): |
113 value = column.get( 'data_table_column_value', '' ) | 114 value = column.get('data_table_column_value', '') |
114 if column.get( 'is_path', {} ).get( 'is_path_selector', None ) == 'yes' and column.get( 'is_path', {} ).get( 'abspath', None ) == 'abspath': | 115 if column.get('is_path', {}).get('is_path_selector') == 'yes' and column.get('is_path', {}).get('abspath') == 'abspath': |
115 value = os.path.abspath( os.path.join( galaxy_data_manager_data_path, value ) ) | 116 value = os.path.abspath(os.path.join(galaxy_data_manager_data_path, value)) |
116 entry_dict[ column.get( 'data_table_column_name', '' ) ] = value | 117 entry_dict[column.get('data_table_column_name', '')] = value |
117 data_table_name = data_table['data_table_name'] | 118 data_table_name = data_table['data_table_name'] |
118 rval[ data_table_name ] = rval.get( data_table_name, [] ) | 119 rval[data_table_name] = rval.get(data_table_name, []) |
119 rval[ data_table_name ].append( entry_dict ) | 120 rval[data_table_name].append(entry_dict) |
120 return rval | 121 return rval |
121 | 122 |
122 | 123 |
123 def get_file_content( params, target_directory ): | 124 def get_file_content(params, target_directory): |
124 directory_content = params.get( 'directory_content', [] ) | 125 directory_content = params.get('directory_content', []) |
125 for content in directory_content: | 126 for content in directory_content: |
126 target_path = os.path.join( target_directory, content.get( 'subdir', '' ) ) | 127 target_path = os.path.join(target_directory, content.get('subdir', '')) |
127 try: | 128 try: |
128 os.makedirs( target_path ) | 129 os.makedirs(target_path) |
129 except OSError: | 130 except OSError: |
130 pass | 131 pass |
131 if content.get( 'file_source', {}).get( 'file_source_selector', None ) == 'URL': | 132 if content.get('file_source', {}).get('file_source_selector') == 'URL': |
132 ( filename, headers ) = urlretrieve( content.get( 'file_source', {}).get( 'file_URL', None ) ) | 133 (filename, headers) = urlretrieve(content.get('file_source', {}).get('file_URL')) |
133 try: | 134 try: |
134 bname = headers['Content-Disposition'] | 135 bname = headers['Content-Disposition'] |
135 except KeyError: | 136 except KeyError: |
136 bname = os.path.basename( urllib2.urlparse.urlsplit( content.get( 'file_source', {}).get( 'file_URL', None ) ).path ) | 137 bname = os.path.basename(urlsplit(content.get('file_source', {}).get('file_URL')).path) |
137 else: | 138 else: |
138 filename = content.get( 'file_source', {}).get( 'file_history', None ) | 139 filename = content.get('file_source', {}).get('file_history') |
139 bname = os.path.basename( filename ) | 140 bname = os.path.basename(filename) |
140 file_action = content.get( 'file_action', {}).get( 'file_action_selector', None ) | 141 file_action = content.get('file_action', {}).get('file_action_selector') |
141 if file_action == 'unpack': | 142 if file_action == 'unpack': |
142 unpack_archive( filename, target_path ) | 143 unpack_archive(filename, target_path) |
143 else: | 144 else: |
144 filename_override = content.get( 'file_action', {}).get( 'filename_override', None ) | 145 filename_override = content.get('file_action', {}).get('filename_override') |
145 if filename_override: | 146 if filename_override: |
146 target_path = os.path.join( target_path, filename_override ) | 147 target_path = os.path.join(target_path, filename_override) |
147 else: | 148 else: |
148 target_path = os.path.join( target_path, bname ) | 149 target_path = os.path.join(target_path, bname) |
149 shutil.copyfile( filename, target_path ) | 150 shutil.copyfile(filename, target_path) |
150 return len( directory_content ) | 151 return len(directory_content) |
151 | 152 |
152 | 153 |
153 def main(): | 154 def main(): |
154 parser = optparse.OptionParser() | 155 parser = optparse.OptionParser() |
155 parser.add_option( '', '--galaxy_data_manager_data_path', dest='galaxy_data_manager_data_path', default='', help='Root path for galaxy_data_manager_data_path' ) | 156 parser.add_option('', '--galaxy_data_manager_data_path', dest='galaxy_data_manager_data_path', default='', help='Root path for galaxy_data_manager_data_path') |
156 (options, args) = parser.parse_args() | 157 (options, args) = parser.parse_args() |
157 | 158 |
158 filename = args[0] | 159 filename = args[0] |
159 | 160 |
160 params = json.loads( open( filename ).read() ) | 161 with open(filename) as fh: |
161 target_directory = params[ 'output_data' ][0]['extra_files_path'] | 162 params = json.loads(fh.read()) |
163 target_directory = params['output_data'][0]['extra_files_path'] | |
162 | 164 |
163 data_table_entries = get_data_table_entries( params['param_dict'], options.galaxy_data_manager_data_path ) | 165 data_table_entries = get_data_table_entries(params['param_dict'], options.galaxy_data_manager_data_path) |
164 | 166 |
165 # save info to json file | 167 # save info to json file |
166 open( filename, 'wb' ).write( json.dumps( { "data_tables": data_table_entries} ) ) | 168 with open(filename, 'wb') as fh: |
169 fh.write(json.dumps({"data_tables": data_table_entries})) | |
167 | 170 |
168 get_file_content( params['param_dict'], target_directory ) | 171 get_file_content(params['param_dict'], target_directory) |
169 | 172 |
170 | 173 |
171 if __name__ == "__main__": | 174 if __name__ == "__main__": |
172 main() | 175 main() |