Created
June 29, 2012 23:46
-
-
Save alexras/3021457 to your computer and use it in GitHub Desktop.
Turn invalid XML output from hpacucli into valid XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from xml.dom.minidom import parse, parseString | |
| import os, sys, argparse, subprocess, uuid, re, time, getpass | |
| def hp_diagnostic_xml(output_filename): | |
| try: | |
| # Run a diagnostic, outputting to a temporary file | |
| while True: | |
| temp_filename = "/tmp/tmp%x.xml" % (uuid.uuid4()) | |
| if not os.path.exists(temp_filename): | |
| break | |
| proc = subprocess.Popen( | |
| 'hpacucli ctrl all diag file=%s xml=on' % (temp_filename), | |
| shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| (proc_out, proc_err) = proc.communicate() | |
| if proc.returncode != 0: | |
| sys.exit("hpacucli failed with error %d: %s", proc.returncode, | |
| proc_out) | |
| fp = open(temp_filename, 'r') | |
| raw_xml = fp.read() | |
| fp.close() | |
| # hpacucli doesn't escape ampersands in values sometimes, so we have to | |
| # make sure it does that before passing its output to the parser | |
| value_ampersand_regex = re.compile('value="(.*?)&(?!amp;)(.*?)"') | |
| def escape_matched_value(match_obj): | |
| return 'value="%s&%s"' % ( | |
| match_obj.group(1), match_obj.group(2)) | |
| (escaped_xml, num_escapes) = re.subn( | |
| value_ampersand_regex, escape_matched_value, raw_xml) | |
| print >>sys.stderr, "%d ampersands escaped" % (num_escapes) | |
| xml_doc = parseString(escaped_xml) | |
| # For some reason toprettyxml generates documents with a ton of | |
| # whitespace; strip that out before outputting | |
| doc_lines = [line for line in xml_doc.toprettyxml().split('\n') | |
| if len(line.strip()) > 0] | |
| if output_filename is not None: | |
| with open(output_filename, 'w') as out_fp: | |
| for line in doc_lines: | |
| out_fp.write(line) | |
| else: | |
| for line in doc_lines: | |
| print line | |
| finally: | |
| print >>sys.stderr, "Removing '%s'" % (temp_filename) | |
| if os.path.exists(temp_filename): | |
| os.remove(temp_filename) | |
| pass | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="Generates an XML file containing a valid version of " | |
| "hpacucli's XML diagnostic dump") | |
| parser.add_argument( | |
| "--output_filename", "-o", | |
| help=("the filename to which to write the diagnostic XML (default: " | |
| "writes to stdout)"), | |
| default=None) | |
| if getpass.getuser() != "root": | |
| sys.exit("Must run this script as root") | |
| args = parser.parse_args() | |
| return hp_diagnostic_xml(**vars(args)) | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment