예제 #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
파일: check.py 프로젝트: lowell80/ksconf
 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
예제 #3
0
 def __call__(self, string):
     from argparse import ArgumentTypeError
     # the special argument "-" means sys.std{in,out}
     if string == '-':
         if 'r' in self._mode:
             cfp = ConfFileProxy("<stdin>",
                                 "r",
                                 stream=sys.stdin,
                                 is_file=False)
             if self._action == "load":
                 try:
                     d = cfp.data
                     del d
                 except ConfParserException as e:
                     raise ArgumentTypeError(
                         "failed to parse <stdin>: {}".format(e))
             return cfp
         elif 'w' in self._mode:
             return ConfFileProxy("<stdout>",
                                  "w",
                                  stream=sys.stdout,
                                  is_file=False)
         else:
             raise ValueError('argument "-" with mode {}'.format(
                 self._mode))
     if self._accept_dir and os.path.isdir(string):
         return ConfDirProxy(string,
                             self._mode,
                             parse_profile=self._parse_profile)
     if self._action == "none":
         return ConfFileProxy(string,
                              self._mode,
                              parse_profile=self._parse_profile)
     else:
         try:
             encoding = detect_by_bom(string)
             stream = open(string, self._mode, encoding=encoding)
             cfp = ConfFileProxy(string,
                                 self._mode,
                                 stream=stream,
                                 parse_profile=self._parse_profile,
                                 is_file=True)
             if self._action == "load":
                 # Force file to be parsed by accessing the 'data' property
                 d = cfp.data
                 del d
             return cfp
         except IOError as e:
             message = "can't open '%s': %s"
             debug_traceback()
             raise ArgumentTypeError(message % (string, e))
         except ConfParserException as e:
             debug_traceback()
             raise ArgumentTypeError("failed to parse '%s': %s" %
                                     (string, e))
         except TypeError as e:
             debug_traceback()
             raise ArgumentTypeError("Parser config error '%s': %s" %
                                     (string, e))
예제 #4
0
 def __call__(self, string):
     from argparse import ArgumentTypeError
     # the special argument "-" means sys.std{in,out}
     if string == '-':
         if 'r' in self._mode:
             cfp = ConfFileProxy("<stdin>",
                                 "r",
                                 stream=sys.stdin,
                                 is_file=False)
             if self._action == "load":
                 try:
                     d = cfp.data
                     del d
                 except ConfParserException as e:
                     raise ArgumentTypeError(
                         "failed to parse <stdin>: {}".format(e))
             return cfp
         elif 'w' in self._mode:
             return ConfFileProxy("<stdout>",
                                  "w",
                                  stream=sys.stdout,
                                  is_file=False)
         else:
             raise ValueError('argument "-" with mode {}'.format(
                 self._mode))
     if self._accept_dir and os.path.isdir(string):
         return ConfDirProxy(string,
                             self._mode,
                             parse_profile=self._parse_profile)
     if self._action == "none":
         return ConfFileProxy(string,
                              self._mode,
                              parse_profile=self._parse_profile)
     else:
         try:
             # Another possible option is using:  seekable()  but that only works ONCE the stream is open
             if os.path.isfile(string):
                 encoding = detect_by_bom(string)
                 stream = open(string, self._mode, encoding=encoding)
                 cfp = ConfFileProxy(string,
                                     self._mode,
                                     stream=stream,
                                     parse_profile=self._parse_profile,
                                     is_file=True)
             else:
                 # Could be an explicit link to /dev/stdin; or /dev/fd/63; a bash <(cmd) input
                 # Assume UTF-8 here because that's the default encoding expected by Splunk
                 stream = open(string, self._mode, encoding="utf-8")
                 cfp = ConfFileProxy(string,
                                     self._mode,
                                     stream=stream,
                                     parse_profile=self._parse_profile,
                                     is_file=False)
             if self._action == "load":
                 # Force file to be parsed by accessing the 'data' property
                 d = cfp.data
                 del d
             return cfp
         except IOError as e:
             message = "can't open '%s': %s"
             debug_traceback()
             raise ArgumentTypeError(message % (string, e))
         except ConfParserException as e:
             debug_traceback()
             raise ArgumentTypeError("failed to parse '%s': %s" %
                                     (string, e))
         except TypeError as e:
             debug_traceback()
             raise ArgumentTypeError("Parser config error '%s': %s" %
                                     (string, e))