我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用xml.etree.cElementTree.ParseError()。
def check_version(self): # ???? && ?? try: mobilesignin = self.netease.daily_signin(0) if mobilesignin != -1 and mobilesignin['code'] not in (-2, 301): notify('???????', 1) time.sleep(0.5) pcsignin = self.netease.daily_signin(1) if pcsignin != -1 and pcsignin['code'] not in (-2, 301): notify('PC?????', 1) tree = ET.ElementTree(ET.fromstring(self.netease.get_version())) root = tree.getroot() return root[0][4][0][0].text except (ET.ParseError, TypeError) as e: log.error(e) return 0
def parseWindowsAliases(self, aliases): try: with open(aliases) as xmlfile: xmlroot = XML.ElementTree(file=xmlfile).getroot() except (IOError, XMLParseError): raise ValueError("Unable to open or read windows alias file: {}".format(aliases)) # Extract the mappings try: for elem in xmlroot.findall("./windowsZones/mapTimezones/mapZone"): if elem.get("territory", "") == "001": if elem.get("other") not in self.links: self.links[elem.get("other")] = elem.get("type") else: print("Ignoring duplicate Windows alias: {}".format(elem.get("other"))) except (ValueError, KeyError): raise ValueError("Unable to parse windows alias file: {}".format(aliases))
def _poll(self, url): request = urllib2.Request(url) for key, value in self.http_headers: request.add_header(key, value) try: self.log.info('Downloading feed from: "%s"', url) _, fileobj = yield utils.fetch_url(request) except utils.FetchUrlFailed as e: self.log.error('Failed to download feed "%s": %r', url, e) idiokit.stop(False) self.log.info("Finished downloading the feed.") byte = fileobj.read(1) while byte and byte != "<": byte = fileobj.read(1) if byte == "<": fileobj.seek(-1, 1) try: for _, elem in etree.iterparse(fileobj): for event in self._parse(elem, url): if event: yield idiokit.send(event) except ParseError as e: self.log.error('Invalid format on feed: "%s", "%r"', url, e)
def parse_new_asx(data): # Copied from mopidy.audio.playlists try: for _, element in elementtree.iterparse(data): element.tag = element.tag.lower() # normalize for ref in element.findall('entry/ref[@href]'): yield fix_asf_uri(ref.get('href', '').strip()) for entry in element.findall('entry[@href]'): yield fix_asf_uri(entry.get('href', '').strip()) except elementtree.ParseError: return
def xml_parse_file(self, file_object): """ Return the root of the XML parsed tree from the file object. If there is a parsing error, print the surrounding environment and raise an exception.""" try: e = ET.parse(file_object).getroot() except ET.ParseError as e: # in case of a parsing error, print the environment that caused # the failure: m = re.search(r"line (\d*), column (\d*)", str(e)) if m: line = int(m.group(1)) column = int(m.group(2)) start_line = max(0, line - 5) end_line = line + 5 else: start_line = 0 end_line = 999999 #S = S.splitlines() S = [] self.logger.error(e) for i, x in enumerate(S): if i > start_line: warnings.warn("{:<3}: {}".format(i, x.decode("utf8"))) if i == line - 1: warnings.warn(" " + " " * (column - 1) + "^") if i > end_line: break raise e return e
def _get_doc_tree(self): if self.part_nr not in self._doc_trees: doc_name = 'part{:02}.xml'.format(self.part_nr) document_files = os.listdir(self.spec_dir) if doc_name not in document_files: raise SpecReaderFileError(u'Missing docbook file {} in {}'.format(doc_name, self.spec_dir)) try: self._doc_trees[self.part_nr] = ElementTree.parse(os.path.join(self.spec_dir, doc_name)) except ElementTree.ParseError: raise SpecReaderFileError(u'Parse error in docbook file {} in {}'.format(doc_name, self.spec_dir)) return self._doc_trees.get(self.part_nr)
def load_annotations(self, image_index): filename = self.image_names[image_index] + '.xml' try: tree = ET.parse(os.path.join(self.data_dir, 'Annotations', filename)) return self.__parse_annotations(tree.getroot()) except ET.ParseError as e: raise_from(ValueError('invalid annotations file: {}: {}'.format(filename, e)), None) except ValueError as e: raise_from(ValueError('invalid annotations file: {}: {}'.format(filename, e)), None)
def test_parsing_note_error(xmldata_note_error, parser): with pytest.raises((ParseError, cParseError, XMLSyntaxError), parsing_method=parser): for doc in xmliter(xmldata_note_error, 'note', parsing_method=parser): pass
def test_parsing_note_error(xmldata_note_error, parser): with pytest.raises((ParseError, cParseError, XMLSyntaxError)): xmlparse(xmldata_note_error, parsing_method=parser)
def detect_xspf_header(data): data = data[0:150] if b'xspf' not in data.lower(): return False try: data = io.BytesIO(data) for event, element in elementtree.iterparse(data, events=(b'start',)): return element.tag.lower() == '{http://xspf.org/ns/0/}playlist' except elementtree.ParseError: pass return False
def detect_asx_header(data: bytes): data = data[0:50] if b'asx' not in data.lower(): return False try: bytesIO = io.BytesIO(data) for event, element in elementtree.iterparse(bytesIO, events=(b'start',)): return element.tag.lower() == 'asx' except elementtree.ParseError: pass return False
def parse_xspf(data: bytes): try: # Last element will be root. element = None for event, element in elementtree.iterparse(io.BytesIO(data)): element.tag = element.tag.lower() # normalize if element is not None: ns = 'http://xspf.org/ns/0/' for track in element.iterfind('{%s}tracklist/{%s}track' % (ns, ns)): yield track.findtext('{%s}location' % ns) except elementtree.ParseError: return
def parse_asx(data): try: # Last element will be root. element = None for event, element in elementtree.iterparse(io.BytesIO(data)): element.tag = element.tag.lower() # normalize if element is not None: for ref in element.findall('entry/ref[@href]'): yield ref.get('href', '').strip() for entry in element.findall('entry[@href]'): yield entry.get('href', '').strip() except elementtree.ParseError: return
def parse_volume_info(info): tree = etree.fromstring(info) volumes = [] for el in tree.findall('volInfo/volumes/volume'): try: volumes.append(_parse_a_vol(el)) except (ParseError, AttributeError, ValueError) as e: raise GlusterCmdOutputParseError(e) return volumes
def _parse_volume_status(data): tree = etree.fromstring(data) nodes = [] for el in tree.findall('volStatus/volumes/volume/node'): try: nodes.append(_parse_a_node(el)) except (ParseError, AttributeError, ValueError) as e: raise GlusterCmdOutputParseError(e) return nodes
def parse_volume_profile_info(info, op): xml = etree.fromstring(info) profiles = [] for el in xml.findall('volProfile'): try: if op == "clear": profiles.append(_parse_profile_info_clear(el)) else: profiles.append(_parse_profile_info(el)) except (ParseError, AttributeError, ValueError) as e: raise GlusterCmdOutputParseError(e) return profiles
def parse_peer_status(data): tree = etree.fromstring(data) peers = [] for el in tree.findall('peerStatus/peer'): try: peers.append(_parse_a_peer(el)) except (ParseError, AttributeError, ValueError) as e: raise GlusterCmdOutputParseError(e) return peers
def parse_pool_list(data): tree = etree.fromstring(data) pools = [] for el in tree.findall('peerStatus/peer'): try: pools.append(_parse_a_peer(el)) except (ParseError, AttributeError, ValueError) as e: raise GlusterCmdOutputParseError(e) return pools
def is_svg(self, f): """ Check if provided file is svg """ f.seek(0) tag = None try: for event, el in et.iterparse(f, ('start',)): tag = el.tag break except et.ParseError: pass return tag == '{http://www.w3.org/2000/svg}svg'
def is_heal_disabled(mnode, volname): """Check if heal is disabled for a volume. Args: mnode : Node on which commands are executed volname : Name of the volume Returns: bool : True if heal is diabled on volume. False otherwise. NoneType: None if unable to get the volume status shd or parse error. """ cmd = "gluster volume status %s shd --xml" % volname ret, out, _ = g.run(mnode, cmd, log_level='DEBUG') if ret != 0: g.log.error("Failed to get the self-heal-daemon status for the " "volume" % volname) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the volume status shd xml output.") return None operr = root.find("opErrstr") if operr: if "Self-heal Daemon is disabled for volume" in operr.text: return True return False
def is_valid_xml_path(filepath): """ Attempts to parse XML file at filepath. ElementTree.parse() raises informative exceptions as ParseError if cannot parse as valid XML. This function catches them and returns as part of the function response to caller. Returns tuple of (boolean, message string) where True = valid XML, False = invalid XML. :param filepath: (string) filepath to attempt to parse as XML formatted text file :return: (boolean, message string) tuple """ try: tree = Etree.parse(filepath) return True, tree except Exception as e: return False, str(e)
def log_error(self, log, device, error): ''' Log an approppriate message for error occurring on device. ''' message, args = (None, [device.id]) if isinstance(error, txwinrm.collect.RequestError): message = "Query error on %s: %s" args.append(error[0]) if isinstance(error, UnauthorizedError): message += ' or check server WinRM settings \n Please refer to txwinrm documentation at '\ 'http://wiki.zenoss.org/ZenPack:Microsoft_Windows#winrm_setup' elif isinstance(error, ConnectionRefusedError): message = "Connection refused on %s: Verify WinRM setup" elif isinstance(error, TimeoutError): message = "Timeout on %s: Verify WinRM and firewall setup" elif isinstance(error, ConnectError): message = "Connection error on %s: %s" args.append(error.message) elif isinstance(error, cParseError) and 'line 1, column 0' in error.msg: message = "Error on %s: Check WinRM AllowUnencrypted is set to true" elif type(error) == Exception and "Credentials cache file" in error.message: message = "Credentials cache file not found. Please make sure that this file exist and server has access to it." elif type(error) == Exception and error.message.startswith('kerberos authGSSClientStep failed'): message = "Unable to connect to %s. Please make sure zWinKDC, zWinRMUser and zWinRMPassword property is configured correctly" elif isinstance(error, ResponseFailed): for reason in error.reasons: if isinstance(reason.value, ConnectionLost): message = "Connection lost for %s. Check if WinRM service listening on port %s is working correctly." args.append(device.zWinRMPort) elif isinstance(reason.value, SSLError): message = "Connection lost for %s. SSL Error: %s." args.append(', '.join(reason.value.args[0][0])) log.error(message, *args) return else: message = "Error on %s: %s" args.append(error) log.error(message, *args) self._send_event(message % tuple(args), device.id, 3, eventClass='/Status/Winrm')
def is_svg(_file): """ Check is a given file is SVG or not. A file is considered to be SVG if: - Its mimetype is "application/svg+xml" or "image/svg+xml". - Its mimetype is "text/html" or "application/xml" or "text/xml" or "text/plain" and it has the svg tag with xmlns http://www.w3.org/2000/svg :param _file: A GenericFile object that should be checked for SVG. :return: Boolean corresponding to whether the file is SVG. """ mime = _file.mime() if mime in ('application/svg+xml', 'image/svg+xml'): return True elif mime in ('application/xml', 'text/xml', 'text/html', 'text/plain'): tag = None with open(_file.fetch('filename'), "r") as f: # cElementTree needs the events as bytes in python2 items = cElementTree.iterparse(f, events=(str('start'),)) try: _, el = items.next() tag = el.tag except cElementTree.ParseError: return False return tag == '{http://www.w3.org/2000/svg}svg'
def load(self): if not fileExists(XML_CONFIG): return try: config = cet_parse(XML_CONFIG).getroot() except ParseError as pe: from time import time print("[PluginSort] Parse Error occured in configuration, backing it up and starting from scratch!") try: copyfile(XML_CONFIG, "/etc/enigma2/pluginsort.xml.%d" % (int(time()),)) except Error as she: print("[PluginSort] Uh oh, failed to create the backup... I hope you have one anyway :D") return for wheresection in config.findall('where'): where = wheresection.get('type') whereid = WHEREMAP.get(where, None) whereplugins = wheresection.findall('plugin') if whereid is None or not whereplugins: print("[PluginSort] Ignoring section %s because of invalid id (%s) or no plugins (%s)" % (where, repr(whereid), repr(whereplugins))) continue for plugin in whereplugins: name = plugin.get('name') try: weight = int(plugin.get('weight')) except ValueError as ve: print("[PluginSort] Invalid weight of %s received for plugin %s, ignoring" % (repr(plugin.get('weight')), repr(name))) else: self.plugins.setdefault(whereid, {})[name] = weight
def load(self): if not fileExists(XML_CONFIG): return try: config = cet_parse(XML_CONFIG).getroot() except ParseError as pe: from time import time print("[MenuSort] Parse Error occured in configuration, backing it up and starting from scratch!") try: copyfile(XML_CONFIG, "/etc/enigma2/menusort.xml.%d" % (int(time()),)) except Error as she: print("[MenuSort] Uh oh, failed to create the backup... I hope you have one anyway :D") return for node in config.findall('entry'): text = node.get('text', '').encode("UTF-8") weight = node.get("weight", None) hidden = node.get('hidden', False) hidden = hidden and hidden.lower() == "yes" try: weight = int(weight) except ValueError as ve: print("[MenuSort] Invalid value for weight on entry %s: %s" % (repr(text), repr(weight))) continue if not text or weight is None: print("[MenuSort] Invalid entry in xml (%s, %s), ignoring" % (repr(text), repr(weight))) continue self.weights[text] = (weight, hidden)
def _translateExceptions(original): try: yield None except ExpatError as e: raise XMLSyntaxError(original, e.args[0], e.lineno) except (cet.ParseError, et.ParseError) as e: raise XMLSyntaxError(original, e.args[0], e.lineno)
def parse_new_asx(data): # Copied from mopidy.audio.playlists try: for event, element in elementtree.iterparse(data): element.tag = element.tag.lower() # normalize except elementtree.ParseError: return for ref in element.findall('entry/ref[@href]'): yield fix_asf_uri(ref.get('href', '').strip()) for entry in element.findall('entry[@href]'): yield fix_asf_uri(entry.get('href', '').strip())
def parse(path, tag=REPORT_HOST): """Parse Nessus XML export from Workbench API into dicts. :param path: The file path. :param tag: The XML tag to iterate on. It should be WorkbenchParser.REPORT_HOST or WorkbenchParser.REPORT_ITEM. """ assert tag in [WorkbenchParser.REPORT_HOST, WorkbenchParser.REPORT_ITEM], u'Valid tag for parsing.' report_host = None host_properties = None report_items = [] if tag == WorkbenchParser.REPORT_HOST else None try: for event, elem in ET.iterparse(path, events=('start', 'end')): if event == 'start': if elem.tag == 'ReportHost': report_host = WorkbenchParser._from_report_host(elem) if event == 'end': if elem.tag == WorkbenchParser.REPORT_HOST: elem.clear() if tag == elem.tag: yield { 'report_host': report_host, 'host_properties': host_properties, 'report_items': report_items, } report_items = [] if elem.tag == WorkbenchParser.HOST_PROPERTIES: host_properties = WorkbenchParser._from_host_properties(elem) elem.clear() if elem.tag == WorkbenchParser.REPORT_ITEM: report_item = WorkbenchParser._from_report_item(elem) elem.clear() if tag == elem.tag: yield report_item elif tag == WorkbenchParser.REPORT_HOST: report_items.append(report_item) except ET.ParseError as e: logging.warn(u'Failed to parse Nessus XML: ' + e.msg) # TODO The service return malformed XML for empty set, for now we won't raise an exception for what should # TODO be a normal state. However, this might masked out real error from bubble up (unlikely). # raise TenableIOException(u'Failed to parse Nessus XML: ' + e.message)
def get_volume_profile_info(self, volName, cluster_id): ret_val = {} brickName = '' profile_info = {} for trial_cnt in xrange(0, 3): profile_cmd_op, profile_err = tendrl_glusterfs_utils.exec_command( "gluster volume profile %s info --xml" % volName ) if profile_err: time.sleep(5) if trial_cnt == 2: collectd.error( 'Failed to fetch profile info. The error is: %s' % ( profile_err ) ) return ret_val continue else: break try: profile_info = self._parseVolumeProfileInfo( ElementTree.fromstring(profile_cmd_op) ) return profile_info except ( AttributeError, KeyError, ValueError, ElementTree.ParseError ): collectd.error( 'Failed to collect iops details of brick %s in volume %s of ' 'cluster %s. The profile info is %s. Error %s' % ( brickName, volName, cluster_id, str(profile_info), traceback.format_exc() ) ) return ret_val
def read_zip(zip_name): zip_contents = [] tmp_list = [] final_list = [] out_list = [] hostname = "" try: # Open the zip archive. archive = zipfile.ZipFile(zip_name) for zip_file in archive.infolist(): zip_contents.append(zip_file.filename) print "[+] Processing %d registry acquisitions..." % len(zip_contents) for item in zip_contents: try: if '_w32registry.xml' not in item: continue filename = item.split('/') if len(filename) > 0: filename = filename.pop() else: continue # Get the hostname from the MIR xml filename. hostname = '-'.join(filename.split('-')[:-3]) xml_file = archive.open(item) # Catch potentially corrupt XML data. try: out_list = read_mir(xml_file, quiet=True) except(struct.error, et.ParseError), err: print "[-] Error reading XML data from host: %s, data looks corrupt. Continuing..." % hostname continue # Add the hostname to the entry list. if not out_list or len(out_list) == 0: continue else: for li in out_list: if "Last Modified" not in li[0]: li.insert(0, hostname) final_list.append(li) except IOError, err: print "[-] Error opening file: %s in MIR archive: %s" % (item, err) continue # Add the final header. final_list.insert(0, ("Hostname", "Last Modified", "Last Execution", "Path", "File Size", "File Executed", "Key Path")) return final_list except (IOError, zipfile.BadZipfile, struct.error), err: print "[-] Error reading zip archive: %s" % zip_name return None # Do the work.
def load_nets_from_xml(xfile): global total_discovered, runtime netnodes = [] netlist_dicts = [] clientlist = [] print "Reading network information from %s\n" %xfile # Open Kismet .netxml file and load into list of nodes try: with open(xfile, 'rt') as kismet: try: tree = xml.parse(kismet) except xml.ParseError as xmlerr: print "\n*** ERROR *** Problem parsing input file." print " Is it a Kismet netxml file?" print " Python says: %s\n" % xmlerr usage() sys.exit(2) except IOError as ioerr: print "\n*** ERROR *** Cannot read input file. Does it exist?" print "\tPython says: %s\n" % ioerr usage() sys.exit(2) for node in tree.iter('detection-run'): runtime = node.attrib.get('start-time') if runtime_exists(): print "This detection run (%s) has already been imported" %runtime sys.exit() netnodes = pop_xml_netlist(tree) # For each wireless network node, create a dictionary, and append it to # a list of network dictionaries for node in netnodes: netlist_dicts.append(populate_net_dict(node)) populate_client_list(node, clientlist) total_discovered = len(netnodes) print "" return netlist_dicts, clientlist # Check if the Kismet run being imported has a start-time that is already # in the database
def get_peer_status(mnode): """Parse the output of command 'gluster peer status'. Aargs: mnode (str): Node on which command has to be executed. Returns: NoneType: None if command execution fails or parse errors. list: list of dicts on success. Examples: >>> get_peer_status(mnode = 'abc.lab.eng.xyz.com') [{'uuid': '77dc299a-32f7-43d8-9977-7345a344c398', 'hostname': 'ijk.lab.eng.xyz.com', 'state': '3', 'hostnames' : ['ijk.lab.eng.xyz.com'], 'connected': '1', 'stateStr': 'Peer in Cluster'}, {'uuid': 'b15b8337-9f8e-4ec3-8bdb-200d6a67ae12', 'hostname': 'def.lab.eng.xyz.com', 'state': '3', 'hostnames': ['def.lab.eng.xyz.com'], 'connected': '1', 'stateStr': 'Peer in Cluster'} ] """ ret, out, _ = g.run(mnode, "gluster peer status --xml", log_level='DEBUG') if ret != 0: g.log.error("Failed to execute peer status command on node '%s'. " "Hence failed to parse the peer status.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster peer status xml output.") return None peer_status_list = [] for peer in root.findall("peerStatus/peer"): peer_dict = {} for element in peer.getchildren(): if element.tag == "hostnames": hostnames_list = [] for hostname in element.getchildren(): hostnames_list.append(hostname.text) element.text = hostnames_list peer_dict[element.tag] = element.text peer_status_list.append(peer_dict) return peer_status_list
def get_pool_list(mnode): """Parse the output of 'gluster pool list' command. Args: mnode (str): Node on which command has to be executed. Returns: NoneType: None if command execution fails, parse errors. list: list of dicts on success. Examples: >>> get_pool_list(mnode = 'abc.lab.eng.xyz.com') [{'uuid': 'a2b88b10-eba2-4f97-add2-8dc37df08b27', 'hostname': 'abc.lab.eng.xyz.com', 'state': '3', 'connected': '1', 'stateStr': 'Peer in Cluster'}, {'uuid': 'b15b8337-9f8e-4ec3-8bdb-200d6a67ae12', 'hostname': 'def.lab.eng.xyz.com', 'state': '3', 'hostnames': ['def.lab.eng.xyz.com'], 'connected': '1', 'stateStr': 'Peer in Cluster'} ] """ ret, out, _ = g.run(mnode, "gluster pool list --xml", log_level='DEBUG') if ret != 0: g.log.error("Failed to execute 'pool list' on node %s. " "Hence failed to parse the pool list.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster pool list xml output.") return None pool_list_list = [] for peer in root.findall("peerStatus/peer"): peer_dict = {} for element in peer.getchildren(): if element.tag == "hostname" and element.text == 'localhost': element.text = mnode if element.tag == "hostnames": hostnames_list = [] for hostname in element.getchildren(): hostnames_list.append(hostname.text) element.text = hostnames_list peer_dict[element.tag] = element.text pool_list_list.append(peer_dict) return pool_list_list
def get_tier_status(mnode, volname): """Parse the output of 'gluster tier status' command. Args: mnode (str): Node on which command has to be executed. volname (str): volume name Returns: NoneType: None if command execution fails, parse errors. dict: dict on success. Examples: >>> get_tier_status('abc.lab.eng.xyz.com', 'testvol') {'node': [{'promotedFiles': '0', 'demotedFiles': '0', 'nodeName': 'localhost', 'statusStr': 'in progress'}, {'promotedFiles': '0', 'demotedFiles': '0', 'nodeName': '10.70.47.16', 'statusStr': 'in progress'}], 'task-id': '2ed28cbd-4246-493a-87b8-1fdcce313b34', 'nodeCount': '4', 'op': '7'} """ cmd = "gluster volume tier %s status --xml" % volname ret, out, _ = g.run(mnode, cmd) if ret != 0: g.log.error("Failed to execute 'tier status' on node %s. " "Hence failed to get tier status.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster tier status xml output.") return None tier_status = {} tier_status["node"] = [] for info in root.findall("volRebalance"): for element in info.getchildren(): if element.tag == "node": status_info = {} for elmt in element.getchildren(): status_info[elmt.tag] = elmt.text tier_status[element.tag].append(status_info) else: tier_status[element.tag] = element.text return tier_status
def get_detach_tier_status(mnode, volname): """Parse the output of 'gluster volume tier detach status' command. Args: mnode (str): Node on which command has to be executed. volname (str): volume name Returns: NoneType: None if command execution fails, parse errors. dict: dict on success. Examples: >>> get_detach_tier_status('abc.lab.eng.xyz.com', "testvol") {'node': [{'files': '0', 'status': '3', 'lookups': '1', 'skipped': '0', 'nodeName': 'localhost', 'failures': '0', 'runtime': '0.00', 'id': '11336017-9561-4e88-9ac3-a94d4b403340', 'statusStr': 'completed', 'size': '0'}, {'files': '0', 'status': '3', 'lookups': '0', 'skipped': '0', 'nodeName': '10.70.47.16', 'failures': '0', 'runtime': '0.00', 'id': 'a2b88b10-eba2-4f97-add2-8dc37df08b27', 'statusStr': 'completed', 'size': '0'}], 'nodeCount': '4', 'aggregate': {'files': '0', 'status': '3', 'lookups': '1', 'skipped': '0', 'failures': '0', 'runtime': '0.0', 'statusStr': 'completed', 'size': '0'}} """ cmd = "gluster volume tier %s detach status --xml" % volname ret, out, _ = g.run(mnode, cmd) if ret != 0: g.log.error("Failed to execute 'detach tier status' on node %s. " "Hence failed to get detach tier status.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the detach tier status xml output.") return None tier_status = {} tier_status["node"] = [] for info in root.findall("volDetachTier"): for element in info.getchildren(): if element.tag == "node": status_info = {} for elmt in element.getchildren(): status_info[elmt.tag] = elmt.text tier_status[element.tag].append(status_info) elif element.tag == "aggregate": status_info = {} for elmt in element.getchildren(): status_info[elmt.tag] = elmt.text tier_status[element.tag] = status_info else: tier_status[element.tag] = element.text return tier_status
def tier_detach_stop_and_get_status(mnode, volname): """Parse the output of 'gluster volume tier detach stop' command. Args: mnode (str): Node on which command has to be executed. volname (str): volume name Returns: NoneType: None if command execution fails, parse errors. dict: dict on success. Examples: >>> tier_detach_stop_and_get_status('abc.lab.eng.xyz.com', "testvol") {'node': [{'files': '0', 'status': '3', 'lookups': '1', 'skipped': '0', 'nodeName': 'localhost', 'failures': '0', 'runtime': '0.00', 'id': '11336017-9561-4e88-9ac3-a94d4b403340', 'statusStr': 'completed', 'size': '0'}, {'files': '0', 'status': '3', 'lookups': '0', 'skipped': '0', 'nodeName': '10.70.47.16', 'failures': '0', 'runtime': '0.00', 'id': 'a2b88b12-eba2-4f97-add2-8dc37df08b27', 'statusStr': 'completed', 'size': '0'}], 'nodeCount': '4', 'aggregate': {'files': '0', 'status': '3', 'lookups': '1', 'skipped': '0', 'failures': '0', 'runtime': '0.0', 'statusStr': 'completed', 'size': '0'}} """ cmd = "gluster volume tier %s detach stop --xml" % volname ret, out, _ = g.run(mnode, cmd) if ret != 0: g.log.error("Failed to execute 'tier start' on node %s. " "Hence failed to parse the tier start.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster detach tier stop" " xml output.") return None tier_status = {} tier_status["node"] = [] for info in root.findall("volDetachTier"): for element in info.getchildren(): if element.tag == "node": status_info = {} for elmt in element.getchildren(): status_info[elmt.tag] = elmt.text tier_status[element.tag].append(status_info) elif element.tag == "aggregate": status_info = {} for elmt in element.getchildren(): status_info[elmt.tag] = elmt.text tier_status[element.tag] = status_info else: tier_status[element.tag] = element.text return tier_status
def get_quota_list(mnode, volname, path=None): """Parse the output of 'gluster quota list' command. Args: mnode (str): Node on which command has to be executed. volname (str): volume name Kwargs: path (str): Quota path Returns: NoneType: None if command execution fails, parse errors. dict: dict on success. Examples: >>> get_quota_list('abc.lab.eng.xyz.com', "testvol") {'/': {'used_space': '0', 'hl_exceeded': 'No', 'soft_limit_percent': '60%', 'avail_space': '2147483648', 'soft_limit_value': '1288490188', 'sl_exceeded': 'No', 'hard_limit': '2147483648'}} """ if not path: path = '' cmd = "gluster volume quota %s list %s --xml" % (volname, path) ret, out, _ = g.run(mnode, cmd) if ret != 0: g.log.error("Failed to execute 'quota list' on node %s. " "Hence failed to get the quota list.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster quota list xml output.") return None quotalist = {} for path in root.findall("volQuota/limit"): for elem in path.getchildren(): if elem.tag == "path": path = elem.text quotalist[path] = {} else: quotalist[path][elem.tag] = elem.text return quotalist
def get_quota_list_objects(mnode, volname, path=None): """Parse the output of 'gluster quota list-objects' command. Args: mnode (str): Node on which cmd has to be executed. volname (str): volume name Kwargs: path (str): Quota path Returns: NoneType: None if command execution fails, parse errors. dict: dict of dict on success. Examples: >>> get_quota_list_objects('abc.lab.eng.xyz.com', "testvol") {'/': {'available': '7', 'hl_exceeded': 'No', 'soft_limit_percent': '80%', 'soft_limit_value': '8', 'dir_count': '3', 'sl_exceeded': 'No', 'file_count': '0', 'hard_limit': '10'}} """ if not path: path = '' cmd = "gluster volume quota %s list-objects %s --xml" % (volname, path) ret, out, _ = g.run(mnode, cmd) if ret != 0: g.log.error("Failed to execute 'quota list' on node %s. " "Hence failed to get the quota list.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster quota list xml output.") return None quotalist = {} for path in root.findall("volQuota/limit"): for elem in path.getchildren(): if elem.tag == "path": path = elem.text quotalist[path] = {} else: quotalist[path][elem.tag] = elem.text return quotalist
def get_snap_status(mnode): """Parse the output of 'gluster snapshot status' command. Args: mnode (str): Node on which command has to be executed. Returns: NoneType: None if command execution fails, parse errors. list: list of dict on success. Each snap status will be in dict format Examples: >>> get_snap_status('abc.lab.eng.xyz.com') [{'volCount': '1', 'volume': {'brick': [{'path': '10.70.47.11: testvol_brick0', 'pid': '26747', 'lvUsage': '3.52', 'volumeGroup': 'RHS_vg0', 'lvSize': '9.95g'}, {'path': '10.70.47.16:/testvol_brick1', 'pid': '25497', 'lvUsage': '3.52', 'volumeGroup': 'RHS_vg0', 'lvSize': '9.95g'}], 'brickCount': '2'}, 'name': 'snap2', 'uuid': '56a39a92-c339-47cc-a8b2-9e54bb2a6324'}, {'volCount': '1', 'volume': {'brick': [{'path': '10.70.47.11:testvol_next_brick0', 'pid': '26719', 'lvUsage': '4.93', 'volumeGroup': 'RHS_vg1', 'lvSize': '9.95g'}], 'brickCount': '1'}, 'name': 'next_snap1', 'uuid': 'dcf0cd31-c0db-47ad-92ec-f72af2d7b385'}] """ ret, out, _ = g.run(mnode, "gluster snapshot status --xml") if ret != 0: g.log.error("Failed to execute 'snapshot status' on node %s. " "Hence failed to get the snapshot status.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster snapshot " "status xml output.") return None snap_status_list = [] for snap in root.findall("snapStatus/snapshots/snapshot"): snap_status = {} for element in snap.getchildren(): if element.tag == "volume": status = {} status["brick"] = [] for elmt in element.getchildren(): if elmt.tag == "brick": brick_info = {} for el in elmt.getchildren(): brick_info[el.tag] = el.text status["brick"].append(brick_info) else: status[elmt.tag] = elmt.text snap_status[element.tag] = status else: snap_status[element.tag] = element.text snap_status_list.append(snap_status) return snap_status_list
def get_snap_info(mnode): """Parse the output of 'gluster snapshot info' command. Args: mnode (str): Node on which command has to be executed. Returns: NoneType: None if command execution fails, parse errors. list: list of dicts on success. Examples: >>> get_snap_info('abc.lab.eng.xyz.com') [{'description': 'This is snap2', 'uuid': '56a39a92-c339-47cc-a8b2-9e54bb2a6324', 'volCount': '1', 'snapVolume': {'status': 'Stopped', 'name': 'df1882d3f86d48738e69f298096f3810'}, 'createTime': '2016-04-07 12:01:21', 'name': 'snap2'}, {'description': None, 'uuid': 'a322d93a-2732-447d-ab88-b943fa402fd2', 'volCount': '1', 'snapVolume': {'status': 'Stopped', 'name': '2c790e6132e447e79168d9708d4abfe7'}, 'createTime': '2016-04-07 13:59:43', 'name': 'snap1'}] """ ret, out, _ = g.run(mnode, "gluster snapshot info --xml") if ret != 0: g.log.error("Failed to execute 'snapshot info' on node %s. " "Hence failed to get the snapshot info.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster snapshot " "info xml output.") return None snap_info_list = [] for snap in root.findall("snapInfo/snapshots/snapshot"): snap_info = {} for element in snap.getchildren(): if element.tag == "snapVolume": info = {} for elmt in element.getchildren(): if elmt.tag == "originVolume": info["originVolume"] = {} for el in elmt.getchildren(): info[elmt.tag][el.tag] = el.text else: info[elmt.tag] = elmt.text snap_info[element.tag] = info else: snap_info[element.tag] = element.text snap_info_list.append(snap_info) return snap_info_list
def get_snap_config(mnode, volname=None): """Parse the output of 'gluster snapshot config' command. Args: mnode (str): Node on which command has to be executed. Kwargs: volname (str): volume name Returns: NoneType: None if command execution fails, parse errors. dict: on success. Examples: >>> get_snap_config('abc.com') {'volumeConfig': [{'softLimit': '230', 'effectiveHardLimit': '256', 'name': 'testvol', 'hardLimit': '256'}, {'softLimit': '230', 'effectiveHardLimit': '256', 'name': 'testvol_next', 'hardLimit': '256'}], 'systemConfig': {'softLimit': '90%', 'activateOnCreate': 'disable', 'hardLimit': '256', 'autoDelete': 'disable'}} """ ret, out, _ = g.run(mnode, "gluster snapshot config --xml") if ret != 0: g.log.error("Failed to execute 'snapshot config' on node %s. " "Hence failed to get the snapshot config.", mnode) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster snapshot " "config xml output.") return None snap_config = {} for config in root.findall("snapConfig/systemConfig"): sys_config = {} for element in config.getchildren(): sys_config[element.tag] = element.text snap_config["systemConfig"] = sys_config volume_config = [] for config in root.findall("snapConfig/volumeConfig/volume"): vol_config = {} for element in config.getchildren(): vol_config[element.tag] = element.text if volname is not None: if volname == vol_config["name"]: volume_config.append(vol_config) else: volume_config.append(vol_config) snap_config["volumeConfig"] = volume_config return snap_config
def get_heal_info(mnode, volname): """From the xml output of heal info command get the heal info data. Args: mnode : Node on which commands are executed volname : Name of the volume Returns: NoneType: None if parse errors. list: list of dictionaries. Each element in the list is the heal_info data per brick. """ cmd = "gluster volume heal %s info --xml" % volname ret, out, _ = g.run(mnode, cmd, log_level='DEBUG') if ret != 0: g.log.error("Failed to get the heal info xml output for the volume %s." "Hence failed to get the heal info summary." % volname) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster heal info xml output.") return None heal_info_data = [] for brick in root.findall("healInfo/bricks/brick"): brick_heal_info = {} brick_files_to_heal = [] file_to_heal_exist = False for element in brick.getchildren(): if element.tag == "file": file_to_heal_exist = True file_info = {} file_info[element.attrib['gfid']] = element.text brick_files_to_heal.append(file_info) else: brick_heal_info[element.tag] = element.text if file_to_heal_exist: brick_heal_info['file'] = brick_files_to_heal heal_info_data.append(brick_heal_info) return heal_info_data
def get_heal_info_split_brain(mnode, volname): """From the xml output of heal info split-brain command get the heal info split-brain data. Args: mnode : Node on which commands are executed volname : Name of the volume Returns: NoneType: None if parse errors. list: list of dictionaries. Each element in the list is the heal_info_split_brain data per brick. """ cmd = "gluster volume heal %s info split-brain --xml" % volname ret, out, _ = g.run(mnode, cmd, log_level='DEBUG') if ret != 0: g.log.error("Failed to get the heal info xml output for the volume %s." "Hence failed to get the heal info summary." % volname) return None try: root = etree.XML(out) except etree.ParseError: g.log.error("Failed to parse the gluster heal info xml output.") return None heal_info_split_brain_data = [] for brick in root.findall("healInfo/bricks/brick"): brick_heal_info_split_brain = {} brick_files_in_split_brain = [] is_file_in_split_brain = False for element in brick.getchildren(): if element.tag == "file": is_file_in_split_brain = True file_info = {} file_info[element.attrib['gfid']] = element.text brick_files_in_split_brain.append(file_info) else: brick_heal_info_split_brain[element.tag] = element.text if is_file_in_split_brain: brick_heal_info_split_brain['file'] = brick_files_in_split_brain heal_info_split_brain_data.append(brick_heal_info_split_brain) return heal_info_split_brain_data