Esempio n. 1
0
def print_rulefile(args):
    rules = load_rules(args, add_rules=args.addrules,
                       add_dirs=args.customrules)
    ruleset = {}
    for r in rules:
        ruleset.update(r.get_rulefile_entries())
    print(json.dumps(ruleset, indent=2))  # noqa: T001 - it's here for a reason
Esempio n. 2
0
def main():
    args = create_argparser()
    rules = [
        x for x in load_rules(
            args, add_rules=args.addrules, add_dirs=args.customrules)
    ]
    # filter out suppressions
    rules = [
        x for x in rules if not any(y in args.suppress for y in x.GetIDs())
    ]
    _loadedIDs = []
    for r in rules:
        _loadedIDs += r.GetIDs()
    if not args.quiet:
        print("Loaded rules:\n\t{}".format("\n\t".join(sorted(_loadedIDs))))
    stash = Stash(args)
    issues = []
    fixedfiles = []
    for f in args.files:
        try:
            stash.AddFile(f)
        except FileNotFoundError as e:
            if not args.quiet:
                print("Can't open/read: {}".format(e))

    stash.Finalize()

    for f in list(set(stash.GetRecipes() + stash.GetLoneAppends())):
        for r in rules:
            if not r.OnAppend and f.endswith(".bbappend"):
                continue
            if r.OnlyAppend and not f.endswith(".bbappend"):
                continue
            if args.fix:
                fixedfiles += r.fix(f, stash)
            issues += r.check(f, stash)
    fixedfiles = list(set(fixedfiles))
    for f in fixedfiles:
        _items = [f] + stash.GetLinksForFile(f)
        for i in _items:
            items = stash.GetItemsFor(filename=i, nolink=True)
            if not args.nobackup:
                os.rename(i, i + ".bak")
            with open(i, "w") as o:
                o.write("".join([x.Raw for x in items]))
                if not args.quiet:
                    print("{}:{}:{}".format(os.path.abspath(i), "debug",
                                            "Applied automatic fixes"))

    issues = sorted(set(issues), key=lambda x: x[0])

    if args.output != sys.stderr:
        args.output = open(args.output, "w")
    args.output.write("\n".join([x[1] for x in issues]) + "\n")
    if args.output != sys.stderr:
        args.output.close()
    sys.exit(len(issues))
Esempio n. 3
0
def run(args):
    try:
        rules = load_rules(args, add_rules=args.addrules,
                           add_dirs=args.customrules)
        _loaded_ids = []
        for r in rules:
            _loaded_ids += r.get_ids()
        if not args.quiet:
            print('Loaded rules:\n\t{rules}'.format(  # noqa: T001 - it's here for a reason
                rules='\n\t'.join(sorted(_loaded_ids))))
        issues = []
        fixedfiles = []
        groups = group_files(args.files)
        for group in groups:
            stash = Stash(args)
            for f in group:
                try:
                    stash.AddFile(f)
                except FileNotFoundError as e:  # pragma: no cover
                    if not args.quiet:  # pragma: no cover
                        print('Can\'t open/read: {e}'.format(e=e))  # noqa: T001 - it's fine here; # pragma: no cover

            stash.Finalize()

            _files = list(set(stash.GetRecipes() + stash.GetLoneAppends()))
            for _, f in enumerate(_files):
                for r in rules:
                    if not r.OnAppend and f.endswith('.bbappend'):
                        continue
                    if r.OnlyAppend and not f.endswith('.bbappend'):
                        continue
                    if args.fix:
                        fixedfiles += r.fix(f, stash)
                    issues += r.check(f, stash)
            fixedfiles = list(set(fixedfiles))
            for f in fixedfiles:
                _items = [f] + stash.GetLinksForFile(f)
                for i in _items:
                    items = stash.GetItemsFor(filename=i, nolink=True)
                    if not args.nobackup:
                        os.rename(i, i + '.bak')  # pragma: no cover
                    with open(i, 'w') as o:
                        o.write(''.join([x.RealRaw for x in items]))
                        if not args.quiet:
                            print('{path}:{lvl}:{msg}'.format(path=os.path.abspath(i),  # noqa: T001 - it's fine here; # pragma: no cover
                                                    lvl='debug', msg='Applied automatic fixes'))

        return sorted(set(issues), key=lambda x: x[0])
    except Exception:
        import traceback

        # pragma: no cover
        print('OOPS - That shouldn\'t happen - {files}'.format(files=args.files))   # noqa: T001 - it's here for a reason
        # pragma: no cover
        traceback.print_exc()
    return []
Esempio n. 4
0
    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("files", nargs='+', help="File to parse")
    return parser


if __name__ == '__main__':
    args = create_argparser().parse_args()
    rules = [
        x for x in load_rules(add_rules=args.addrules)
        if str(x) not in args.suppress
    ]
    print("Loaded rules: {}".format(",".join(sorted([str(x) for x in rules]))))
    stash = Stash()
    issues = []
    fixedfiles = []
    for f in args.files:
        try:
            stash.AddFile(f)
        except FileNotFoundError:
            print("Can't open/read {}".format(f))

    for f in stash.GetRecipes():
        for r in rules:
            if args.fix: