Пример #1
0
    def run(self, args):
        formatter = SplunkSimpleXmlFormatter()
        # Should we read a list of conf files from STDIN?
        if len(args.xml) == 1 and args.xml[0] == "-":
            files = _stdin_iter()
        else:
            files = args.xml
        c = Counter()
        exit_code = EXIT_CODE_SUCCESS
        for fn in files:
            c["checked"] += 1
            if not os.path.isfile(fn):
                self.stderr.write("Skipping missing file:  {0}\n".format(fn))
                c["missing"] += 1
                continue
            try:
                if formatter.format_xml(fn, fn, args.indent):
                    self.stderr.write(
                        "Replaced file {0} with formatted content\n".format(
                            fn))
                    c["changed"] += 1
                else:
                    if not args.quiet:
                        self.stderr.write("Already formatted {0}\n".format(fn))
                    c["no-action"] += 1
                self.stderr.flush()
            except etree.ParseError as e:
                self.stderr.write("Error parsing file {0}:  {1}\n".format(
                    fn, e))
                self.stderr.flush()
                c["error"] += 1
                exit_code = EXIT_CODE_BAD_CONF_FILE
            except Exception as e:  # pragma: no cover
                self.stderr.write(
                    "Unhandled top-level exception while parsing {0}.  "
                    "Aborting.\n{1}\n".format(fn, e))
                debug_traceback()
                c["error"] += 1
                exit_code = EXIT_CODE_INTERNAL_ERROR
                break

        if not exit_code and c["changed"] > 0:
            exit_code = EXIT_CODE_FORMAT_APPLIED

        if True:  # show stats or verbose
            self.stdout.write(
                "Completed formatting {0[checked]} files.  rc={1} Breakdown:\n"
                "   {0[changed]} files were formatted successfully.\n"
                "   {0[no-action]} files were already formatted.\n"
                "   {0[error]} files failed.\n".format(c, exit_code))
        return exit_code
Пример #2
0
 def run(self, args):
     # Should we read a list of conf files from STDIN?
     if len(args.conf) == 1 and args.conf[0] == "-":
         confs = _stdin_iter()
     else:
         confs = args.conf
     c = Counter()
     exit_code = EXIT_CODE_SUCCESS
     for conf in confs:
         c["checked"] += 1
         if not os.path.isfile(conf):
             self.stderr.write("Skipping missing file:  {0}\n".format(conf))
             c["missing"] += 1
             continue
         try:
             parse_conf(conf, profile=PARSECONF_STRICT_NC)
             c["okay"] += 1
             if not args.quiet:
                 self.stdout.write("Successfully parsed {0}\n".format(conf))
                 self.stdout.flush()
         except ConfParserException as e:
             self.stderr.write("Error in file {0}:  {1}\n".format(conf, e))
             self.stderr.flush()
             exit_code = EXIT_CODE_BAD_CONF_FILE
             # TODO:  Break out counts by error type/category (there's only a few of them)
             c["error"] += 1
         except Exception as e:  # pragma: no cover
             self.stderr.write(
                 "Unhandled top-level exception while parsing {0}.  "
                 "Aborting.\n{1}\n".format(conf, e))
             debug_traceback()
             exit_code = EXIT_CODE_INTERNAL_ERROR
             c["error"] += 1
             break
     if True:  # show stats or verbose
         self.stdout.write(
             "Completed checking {0[checked]} files.  rc={1} Breakdown:\n"
             "   {0[okay]} files were parsed successfully.\n"
             "   {0[error]} files failed.\n".format(c, exit_code))
     return exit_code