'A tool shed repository is being uninstalled so change the
shed_tool_conf file. Parse the config file to generate the entire list
of config_elems instead of using the in-memory list since it will be a
subset of the entire list if one or more repositories have been
deactivated.'

def remove_from_shed_tool_config(self, shed_tool_conf_dict, guids_to_remove):

    shed_tool_conf = shed_tool_conf_dict['config_filename']
    tool_path = shed_tool_conf_dict['tool_path']
    config_elems = []
    (tree, error_message) = xml_util.parse_xml(shed_tool_conf)
    if tree:
        root = tree.getroot()
        for elem in root:
            config_elems.append(elem)
        config_elems_to_remove = []
        for config_elem in config_elems:
            if (config_elem.tag == 'section'):
                tool_elems_to_remove = []
                for tool_elem in config_elem:
                    if (tool_elem.get('guid') in guids_to_remove):
                        tool_elems_to_remove.append(tool_elem)
                for tool_elem in tool_elems_to_remove:
                    config_elem.remove(tool_elem)
                if (len(config_elem) < 1):
                    config_elems_to_remove.append(config_elem)
            elif (config_elem.tag == 'tool'):
                if (config_elem.get('guid') in guids_to_remove):
                    config_elems_to_remove.append(config_elem)
        for config_elem in config_elems_to_remove:
            config_elems.remove(config_elem)
        self.config_elems_to_xml_file(config_elems, shed_tool_conf, tool_path)
