def to_xml_file(self, shed_tool_data_table_config, new_elems=None, remove_elems=None): """ Write the current in-memory version of the shed_tool_data_table_conf.xml file to disk. remove_elems are removed before new_elems are added. """ if not (new_elems or remove_elems): log.debug( 'ToolDataTableManager.to_xml_file called without any elements to add or remove.' ) return # no changes provided, no need to persist any changes if not new_elems: new_elems = [] if not remove_elems: remove_elems = [] full_path = os.path.abspath(shed_tool_data_table_config) # FIXME: we should lock changing this file by other threads / head nodes try: try: tree = util.parse_xml(full_path) except OSError as e: if e.errno == errno.ENOENT: with open(full_path, 'w') as fh: fh.write(TOOL_DATA_TABLE_CONF_XML) tree = util.parse_xml(full_path) else: raise root = tree.getroot() out_elems = [elem for elem in root] except Exception as e: out_elems = [] log.debug( 'Could not parse existing tool data table config, assume no existing elements: %s', e) for elem in remove_elems: # handle multiple occurrences of remove elem in existing elems while elem in out_elems: remove_elems.remove(elem) # add new elems out_elems.extend(new_elems) out_path_is_new = not os.path.exists(full_path) root = util.parse_xml_string( '<?xml version="1.0"?>\n<tables></tables>') for elem in out_elems: root.append(elem) with RenamedTemporaryFile(full_path, mode='w') as out: out.write(util.xml_to_string(root, pretty=True)) os.chmod(full_path, RW_R__R__) if out_path_is_new: self.tool_data_path_files.update_files()
def config_elems_to_xml_file(self, config_elems, config_filename, tool_path): """ Persist the current in-memory list of config_elems to a file named by the value of config_filename. """ try: root = parse_xml_string('<?xml version="1.0"?>\n<toolbox tool_path="%s"></toolbox>' % str(tool_path)) for elem in config_elems: root.append(elem) with RenamedTemporaryFile(config_filename, mode='w') as fh: fh.write(xml_to_string(root, pretty=True)) except Exception: log.exception("Exception in ToolPanelManager.config_elems_to_xml_file")
def config_elems_to_xml_file(self, config_elems, config_filename, tool_path, tool_cache_data_dir=None): """ Persist the current in-memory list of config_elems to a file named by the value of config_filename. """ try: tool_cache_data_dir = f' tool_cache_data_dir="{tool_cache_data_dir}"' if tool_cache_data_dir else '' root = parse_xml_string(f'<?xml version="1.0"?>\n<toolbox tool_path="{tool_path}"{tool_cache_data_dir}></toolbox>') for elem in config_elems: root.append(elem) with RenamedTemporaryFile(config_filename, mode='w') as fh: fh.write(xml_to_string(root, pretty=True)) except Exception: log.exception("Exception in ToolPanelManager.config_elems_to_xml_file")
def data_manager_config_elems_to_xml_file(self, config_elems, config_filename): """ Persist the current in-memory list of config_elems to a file named by the value of config_filename. """ data_managers_path = self.data_managers_path if data_managers_path: root_str = '<?xml version="1.0"?><data_managers tool_path="%s"></data_managers>' % data_managers_path else: root_str = '<?xml version="1.0"?><data_managers></data_managers>' root = ElementTree.fromstring(root_str) for elem in config_elems: root.append(elem) try: with RenamedTemporaryFile(config_filename, mode='w') as fh: fh.write(xml_to_string(root)) except Exception: log.exception("Exception in DataManagerHandler.data_manager_config_elems_to_xml_file")
def _write_integrated_tool_panel_config_file(self): """ Write the current in-memory version of the integrated_tool_panel.xml file to disk. Since Galaxy administrators use this file to manage the tool panel, we'll not use xml_to_string() since it doesn't write XML quite right. """ destination = os.path.abspath(self._integrated_tool_panel_config) log.debug("Writing integrated tool panel config file to '%s'", destination) tracking_directory = self._integrated_tool_panel_tracking_directory if tracking_directory: if not os.path.exists(tracking_directory): os.makedirs(tracking_directory) name = "integrated_tool_panel_%.10f.xml" % time.time() filename = os.path.join(tracking_directory, name) else: filename = destination template = string.Template("""<?xml version="1.0"?> <toolbox> <!-- $INTEGRATED_TOOL_PANEL_DESCRIPTION --> $INTEGRATED_TOOL_PANEL </toolbox> """) integrated_tool_panel = [] for key, item_type, item in self._integrated_tool_panel.panel_items_iter( ): if item: if item_type == panel_item_types.TOOL: integrated_tool_panel.append(' <tool id="%s" />\n' % item.id) elif item_type == panel_item_types.WORKFLOW: integrated_tool_panel.append(' <workflow id="%s" />\n' % item.id) elif item_type == panel_item_types.LABEL: label_id = item.id or '' label_text = item.text or '' label_version = item.version or '' integrated_tool_panel.append( ' <label id="%s" text="%s" version="%s" />\n' % (label_id, label_text, label_version)) elif item_type == panel_item_types.SECTION: section_id = item.id or '' section_name = item.name or '' section_version = item.version or '' integrated_tool_panel.append( ' <section id="%s" name="%s" version="%s">\n' % (escape(section_id), escape(section_name), section_version)) for section_key, section_item_type, section_item in item.panel_items_iter( ): if section_item_type == panel_item_types.TOOL: if section_item: integrated_tool_panel.append( ' <tool id="%s" />\n' % section_item.id) elif section_item_type == panel_item_types.WORKFLOW: if section_item: integrated_tool_panel.append( ' <workflow id="%s" />\n' % section_item.id) elif section_item_type == panel_item_types.LABEL: if section_item: label_id = section_item.id or '' label_text = section_item.text or '' label_version = section_item.version or '' integrated_tool_panel.append( ' <label id="%s" text="%s" version="%s" />\n' % (label_id, label_text, label_version)) integrated_tool_panel.append(' </section>\n') tool_panel_description = '\n '.join( [l for l in INTEGRATED_TOOL_PANEL_DESCRIPTION.split("\n") if l]) tp_string = template.substitute( INTEGRATED_TOOL_PANEL_DESCRIPTION=tool_panel_description, INTEGRATED_TOOL_PANEL='\n'.join(integrated_tool_panel)) with RenamedTemporaryFile(filename, mode='w') as f: f.write(tp_string) if tracking_directory: with open(filename + ".stack", "w") as f: f.write(''.join(traceback.format_stack())) shutil.copy(filename, filename + ".copy") shutil.move(filename + ".copy", destination) try: os.chmod(destination, 0o644) except OSError: # That can happen if multiple threads are simultaneously moving/chmod'ing this file # Should be harmless, though this race condition should be avoided. pass