示例#1
0
    def test_logging(self):
        uut = ListLogPrinter()
        ts = datetime.today()

        uut.log_level = LOG_LEVEL.INFO
        uut.warn('Test value', timestamp=ts)
        uut.print('Test 2', timestamp=ts)  # Should go to INFO
        uut.debug('Test 2', timestamp=ts)  # Should not be logged

        self.assertEqual(uut.logs,
                         [LogMessage(LOG_LEVEL.WARNING,
                                     'Test value',
                                     timestamp=ts),
                          LogMessage(LOG_LEVEL.INFO,
                                     'Test 2',
                                     timestamp=ts)])

        self.assertRaises(TypeError, uut.log_message, 'message')
示例#2
0
    def test_logging(self):
        uut = ListLogPrinter()
        ts = datetime.today()
        ts_str = ts.strftime("%X")

        uut.log_level = LOG_LEVEL.INFO
        uut.warn("Test value", timestamp=ts)
        uut.print("Test 2", timestamp=ts)  # Should go to INFO
        uut.debug("Test 2", timestamp=ts)  # Should not be logged

        self.assertEqual(uut.logs,
                         [LogMessage(LOG_LEVEL.WARNING,
                                     "Test value",
                                     timestamp=ts),
                          LogMessage(LOG_LEVEL.INFO,
                                     "Test 2",
                                     timestamp=ts)])

        self.assertRaises(TypeError, uut.log_message, "message")
示例#3
0
    def setUp(self):
        current_dir = os.path.split(__file__)[0]
        self.collectors_test_dir = os.path.join(current_dir,
                                                'collectors_test_dir')

        self.log_printer = ListLogPrinter()
示例#4
0
 def setUp(self):
     self.logger = ListLogPrinter()
     self.section = Section('t')
示例#5
0
    def Analyze(self):
        """
        This method analyzes the document and sends back the result

        :return: The output is structure which has 3 items:
                 -  The exitcode from the analysis.
                 -  List of logs from the analysis.
                 -  List of information about each section that contains:

                    -  The name of the section.
                    -  Boolean which is true if all bears in the section
                       executed successfully.
                    -  List of results where each result is a string
                       dictionary which contains:
                       id, origin, message, file, line_nr, severity
        """
        retval = []
        if self.path == "" or self.config_file == "":
            return retval

        args = ["--config=" + self.config_file]

        log_printer = ListLogPrinter()
        exitcode = 0
        try:
            yielded_results = False
            (sections,
             local_bears,
             global_bears,
             targets) = gather_configuration(fail_acquire_settings,
                                             log_printer,
                                             arg_list=args)

            for section_name in sections:
                section = sections[section_name]

                if not section.is_enabled(targets):
                    continue

                if any([fnmatch(self.path, file_pattern)
                        for file_pattern in path_list(section["files"])]):

                    section["files"].value = self.path
                    section_result = execute_section(
                        section=section,
                        global_bear_list=global_bears[section_name],
                        local_bear_list=local_bears[section_name],
                        print_results=lambda *args: True,
                        log_printer=log_printer)
                    yielded_results = yielded_results or section_result[0]

                    retval.append(
                        DbusDocument.results_to_dbus_struct(section_result,
                                                            section_name))

            if yielded_results:
                exitcode = 1
        except BaseException as exception:  # pylint: disable=broad-except
            exitcode = exitcode or get_exitcode(exception, log_printer)

        logs = [log.to_string_dict() for log in log_printer.logs]
        return (exitcode, logs, retval)
示例#6
0
def main():
    arg_parser = get_args()
    args = arg_parser.parse_args()

    dir_path = create_dir(os.path.abspath(args.dir))

    if len(list(os.walk(dir_path))) > 0:
        copy_files(get_file(Constants.COALA_HTML_BASE), dir_path)

    if not args.noupdate:
        log_printer = ListLogPrinter()
        results, exitcode, file_dict = run_coala(log_printer=log_printer,
                                                 autoapply=False,
                                                 arg_parser=arg_parser)

        result_data = {"results": results}
        result_data["logs"] = log_printer.logs
        JSONEncoder = create_json_encoder(use_relpath=False)

        result_file = get_file(Constants.CONFIGS['results_file'], dir_path)
        file_data = get_file(Constants.CONFIGS['file_data'], dir_path)
        file_tree_data = get_file(Constants.CONFIGS['files'], dir_path)

        with open(file_tree_data, 'w') as fp:
            file_graph = build_file_graph(file_dict, dir_path)
            json.dump(file_graph,
                      fp,
                      cls=JSONEncoder,
                      sort_keys=True,
                      indent=2,
                      separators=(',', ': '))

        with open(result_file, 'w') as fp:
            json.dump(result_data,
                      fp,
                      cls=JSONEncoder,
                      sort_keys=True,
                      indent=2,
                      separators=(',', ': '))
        with open(file_data, 'w') as fp:
            json.dump(parse_file_dict(file_dict),
                      fp,
                      cls=JSONEncoder,
                      sort_keys=True,
                      indent=2,
                      separators=(',', ': '))
    if not args.nolaunch:
        # Launch server with reference point dir_path
        os.chdir(dir_path)
        if not os.path.exists('bower_components'):
            res = call(['bower', 'install'])
            if res != 0:
                print("Bower is required. Install from `http://bower.io/`")
                sys.exit(1)

        Handler = http.server.SimpleHTTPRequestHandler
        httpd = socketserver.TCPServer(("", Constants.PORT), Handler)
        print("serving at ", Constants.URL)
        print("Press Ctrl+C to end the coala-html session")
        httpd.serve_forever()
        webbrowser.open(Constants.URL, new=2)