示例#1
0
def generate_flake8_report(config_file, paths, excludes, max_line_length=None):
    if LooseVersion(flake8.__version__) < '3.0':
        from ament_flake8.legacy import generate_flake8_report
        return generate_flake8_report(config_file, paths, excludes,
                                      max_line_length)

    flake8_argv = []
    if config_file is not None:
        flake8_argv.append('--config={0}'.format(config_file))
    if len(excludes) > 0:
        flake8_argv.append('--exclude={0}'.format(','.join(excludes)))

    if max_line_length is not None:
        flake8_argv.append('--max-line-length={0}'.format(max_line_length))

    style = get_flake8_style_guide(flake8_argv)

    # Monkey patch formatter to collect all errors
    format_func = style._application.formatter.format
    report = CustomReport()

    def custom_format(error):
        format_func(error)
        report.add_error(error)
        print('')
        print('%s:%d:%d: %s %s' %
              (error.filename, error.line_number, error.column_number,
               error.code, error.text))

    style._application.formatter.format = custom_format

    # Get the names of files checked
    report.report = style.check_files(paths)
    file_checkers = style._application.file_checker_manager.checkers
    report.files = [file_checker.filename for file_checker in file_checkers]

    assert report.report.total_errors == len(report.errors)
    return report
示例#2
0
文件: main.py 项目: ament/ament_lint
def generate_flake8_report(config_file, paths, excludes, max_line_length=None):
    if LooseVersion(flake8.__version__) < '3.0':
        from ament_flake8.legacy import generate_flake8_report
        return generate_flake8_report(config_file, paths, excludes, max_line_length)

    flake8_argv = []
    if config_file is not None:
        flake8_argv.append('--config={0}'.format(config_file))
    if excludes is not None:
        flake8_argv.append('--exclude={0}'.format(','.join(excludes)))

    if max_line_length is not None:
        flake8_argv.append('--max-line-length={0}'.format(max_line_length))

    style = get_flake8_style_guide(flake8_argv)

    # Monkey patch formatter to collect all errors
    format_func = style._application.formatter.format
    report = CustomReport()

    def custom_format(error):
        format_func(error)
        report.add_error(error)
        print('')
        print(
            '%s:%d:%d: %s %s' % (
                error.filename, error.line_number,
                error.column_number, error.code, error.text))
    style._application.formatter.format = custom_format

    # Get the names of files checked
    report.report = style.check_files(paths)
    file_checkers = style._application.file_checker_manager.checkers
    report.files = [file_checker.filename for file_checker in file_checkers]

    assert report.report.total_errors == len(report.errors)
    return report
示例#3
0
def main(argv=sys.argv[1:]):
    config_file = os.path.join(os.path.dirname(__file__), 'configuration',
                               'ament_flake8.ini')

    parser = argparse.ArgumentParser(
        description='Check code using flake8.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--config',
                        metavar='path',
                        default=config_file,
                        dest='config_file',
                        help='The config file')
    parser.add_argument(
        '--linelength',
        metavar='N',
        type=int,
        help='The maximum line length (default: specified in the config file)')
    parser.add_argument(
        'paths',
        nargs='*',
        default=[os.curdir],
        help='The files or directories to check. For directories files ending '
        "in '.py' will be considered.")
    parser.add_argument('--exclude',
                        metavar='filename',
                        nargs='*',
                        dest='excludes',
                        help='The filenames to exclude.')
    # not using a file handle directly
    # in order to prevent leaving an empty file when something fails early
    parser.add_argument('--xunit-file',
                        help='Generate a xunit compliant XML file')
    args = parser.parse_args(argv)

    if args.xunit_file:
        start_time = time.time()

    if not os.path.exists(args.config_file):
        print("Could not find config file '%s'" % args.config_file,
              file=sys.stderr)
        return 1

    # Filter out folders having AMENT_IGNORE
    if args.excludes is None:
        args.excludes = []
    for dirpath, dirnames, filenames in os.walk(os.getcwd()):
        if 'AMENT_IGNORE' in filenames:
            dirnames[:] = []
            args.excludes.append(dirpath)

    report = generate_flake8_report(args.config_file,
                                    args.paths,
                                    args.excludes,
                                    max_line_length=args.linelength)

    # print statistics about errors
    if report.total_errors:
        print('')
        report.print_statistics()

    # print summary
    print('')
    print('%d files checked' % len(report.files))
    if not report.total_errors:
        print('No problems found')
        rc = 0
    else:
        print('%d errors' % (report.total_errors))

        print('')
        error_type_counts = get_error_type_counts(report.get_error_codes())
        for k, v in error_type_counts.items():
            print("'%s'-type errors: %d" % (k, v))

        rc = 1

    print('')
    print('Checked files:')
    print(''.join(['\n* %s' % f for f in report.files]))

    # generate xunit file
    if args.xunit_file:
        folder_name = os.path.basename(os.path.dirname(args.xunit_file))
        file_name = os.path.basename(args.xunit_file)
        suffix = '.xml'
        if file_name.endswith(suffix):
            file_name = file_name[0:-len(suffix)]
            suffix = '.xunit'
            if file_name.endswith(suffix):
                file_name = file_name[0:-len(suffix)]
        testname = '%s.%s' % (folder_name, file_name)

        xml = get_xunit_content(report, testname, time.time() - start_time)
        path = os.path.dirname(os.path.abspath(args.xunit_file))
        if not os.path.exists(path):
            os.makedirs(path)
        with open(args.xunit_file, 'w') as f:
            f.write(xml)

    return rc
示例#4
0
文件: main.py 项目: ament/ament_lint
def main(argv=sys.argv[1:]):
    config_file = os.path.join(
        os.path.dirname(__file__), 'configuration', 'ament_flake8.ini')

    parser = argparse.ArgumentParser(
        description='Check code using flake8.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        '--config',
        metavar='path',
        default=config_file,
        dest='config_file',
        help='The config file')
    parser.add_argument(
        '--linelength', metavar='N', type=int,
        help='The maximum line length (default: specified in the config file)')
    parser.add_argument(
        'paths',
        nargs='*',
        default=[os.curdir],
        help='The files or directories to check. For directories files ending '
             "in '.py' will be considered.")
    parser.add_argument(
        '--exclude',
        metavar='filename',
        nargs='*',
        dest='excludes',
        help='The filenames to exclude.')
    # not using a file handle directly
    # in order to prevent leaving an empty file when something fails early
    parser.add_argument(
        '--xunit-file',
        help='Generate a xunit compliant XML file')
    args = parser.parse_args(argv)

    if args.xunit_file:
        start_time = time.time()

    if not os.path.exists(args.config_file):
        print("Could not find config file '%s'" % args.config_file, file=sys.stderr)
        return 1

    report = generate_flake8_report(
        args.config_file, args.paths, args.excludes,
        max_line_length=args.linelength)

    # print statistics about errors
    if report.total_errors:
        print('')
        report.print_statistics()

    # print summary
    print('')
    print('%d files checked' % len(report.files))
    if not report.total_errors:
        print('No problems found')
        rc = 0
    else:
        print('%d errors' % (report.total_errors))

        print('')
        error_type_counts = get_error_type_counts(report.get_error_codes())
        for k, v in error_type_counts.items():
            print("'%s'-type errors: %d" % (k, v))

        rc = 1

    print('')
    print('Checked files:')
    print(''.join(['\n* %s' % f for f in report.files]))

    # generate xunit file
    if args.xunit_file:
        folder_name = os.path.basename(os.path.dirname(args.xunit_file))
        file_name = os.path.basename(args.xunit_file)
        suffix = '.xml'
        if file_name.endswith(suffix):
            file_name = file_name[0:-len(suffix)]
            suffix = '.xunit'
            if file_name.endswith(suffix):
                file_name = file_name[0:-len(suffix)]
        testname = '%s.%s' % (folder_name, file_name)

        xml = get_xunit_content(report, testname, time.time() - start_time)
        path = os.path.dirname(os.path.abspath(args.xunit_file))
        if not os.path.exists(path):
            os.makedirs(path)
        with open(args.xunit_file, 'w') as f:
            f.write(xml)

    return rc