Esempio n. 1
0
def create_argparser():
    parser = argparse.ArgumentParser(
        description=
        'Advanced OELint - Check bitbake recipes against OECore styleguide')
    parser.add_argument("--suppress",
                        default=[],
                        action="append",
                        help="Rules to suppress")
    parser.add_argument("--output",
                        default=sys.stderr,
                        help="Where to flush the findings (default: stderr)")
    parser.add_argument("--fix",
                        action="store_true",
                        default=False,
                        help="Automatically try to fix the issues")
    parser.add_argument("--nobackup",
                        action="store_true",
                        default=False,
                        help="Don't create backup file when auto fixing")
    parser.add_argument("--addrules",
                        nargs="+",
                        default=[],
                        help="Additional non-default rulessets to add")
    parser.add_argument("--customrules",
                        nargs="+",
                        default=[],
                        help="Additional directories to parse for rulessets")
    parser.add_argument("--rulefile", default=None, help="Rulefile")
    parser.add_argument("--constantfile", default=None, help="Constantfile")
    parser.add_argument("--color",
                        action="store_true",
                        default=False,
                        help="Add color to the output based on the severity")
    parser.add_argument("--quiet",
                        action="store_true",
                        default=False,
                        help="Print findings only")
    parser.add_argument("files", nargs='+', help="File to parse")

    args = parser.parse_args()

    if args.rulefile:
        try:
            with open(args.rulefile) as i:
                set_rulefile(json.load(i))
        except (FileNotFoundError, json.JSONDecodeError):
            raise argparse.ArgumentTypeError("'rulefile' is not a valid file")

    if args.constantfile:
        try:
            with open(args.constantfile) as i:
                set_constantfile(json.load(i))
        except (FileNotFoundError, json.JSONDecodeError):
            raise argparse.ArgumentTypeError(
                "'constantfile' is not a valid file")

    if args.color:
        set_color(True)
    return args
Esempio n. 2
0
def arguments_post(args):  # noqa: C901 - complexity is still okay
    # Convert boolean symbols
    for _option in [
        'color',
        'exit_zero',
        'fix',
        'nobackup',
        'noinfo',
        'nowarn',
        'print_rulefile',
        'quiet',
        'relpaths',
    ]:
        try:
            setattr(args, _option, bool(getattr(args, _option)))
        except AttributeError:  # pragma: no cover
            pass  # pragma: no cover

    # Convert list symbols
    for _option in [
        'suppress',
        'constantmods',
    ]:
        try:
            if not isinstance(getattr(args, _option), list):
                setattr(args, _option, [x.strip() for x in (getattr(args, _option) or '').split('\n') if x])
        except AttributeError:  # pragma: no cover
            pass  # pragma: no cover

    if args.files == [] and not args.print_rulefile:
        raise argparse.ArgumentTypeError('no input files')

    if args.rulefile:
        try:
            with open(args.rulefile) as i:
                set_rulefile(json.load(i))
        except (FileNotFoundError, json.JSONDecodeError):
            raise argparse.ArgumentTypeError(
                '\'rulefile\' is not a valid file')

    if args.constantfile:
        try:
            with open(args.constantfile) as i:
                CONSTANTS.AddFromConstantFile(json.load(i))
        except (FileNotFoundError, json.JSONDecodeError):
            raise argparse.ArgumentTypeError(
                '\'constantfile\' is not a valid file')

    for mod in args.constantmods:
        try:
            with open(mod.lstrip('+-')) as _in:
                _cnt = json.load(_in)
            if mod.startswith('+'):
                CONSTANTS.AddConstants(_cnt)
            elif mod.startswith('-'):
                CONSTANTS.RemoveConstants(_cnt)
            else:
                CONSTANTS.OverrideConstants(_cnt)
        except (FileNotFoundError, json.JSONDecodeError):
            raise argparse.ArgumentTypeError(
                'mod file \'{file}\' is not a valid file'.format(file=mod))

    set_colorize(args.color)
    set_nowarn(args.nowarn)
    set_noinfo(args.noinfo)
    set_relpaths(args.relpaths)
    set_suppressions(args.suppress)
    if args.noid:
        # just strip id from message format if noid is requested
        args.messageformat = args.messageformat.replace('{id}', '')
        # strip any double : resulting from the previous operation
        args.messageformat = args.messageformat.replace('::', ':')
    set_messageformat(args.messageformat)
    return args