Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup and iterate files if a path is provided as input #266

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions oelint_adv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re
import sys
from typing import List

from oelint_parser.cls_stash import Stash
from oelint_parser.constants import CONSTANTS
Expand Down Expand Up @@ -60,7 +61,7 @@ def create_argparser():
help='Print loaded rules as a rulefile and exit')
parser.add_argument('--exit-zero', action='store_true', default=False,
help='Always return a 0 (non-error) status code, even if lint errors are found')
parser.add_argument('files', nargs='*', help='File to parse')
parser.add_argument('files', nargs='*', help='File(s) to lint')

return parser

Expand All @@ -69,9 +70,45 @@ def parse_arguments():
return create_argparser().parse_args() # pragma: no cover


def find_recipes(root_directory: str) -> List[str]:
"""Walks a `root_directory` and gathers `.bb` / `.bbappend` files.
"""
recipes = []
for root, _, files in os.walk(root_directory):
for file in files:
if file.endswith('.bb') or file.endswith('.bbappend'):
recipes.append('/'.join((root, file)))

return recipes


def expand_paths(roots: List[str]) -> List[str]:
"""If an item in `roots` is a path, walks that path and gathers all `.bb` and
`.bbappend` files.

Args:
roots: list of files or paths

Returns:
a list of absolute file paths
"""
recipes = []
for root in roots:
if os.path.isdir(root):
recipes.extend(find_recipes(root))
else:
recipes.append(root)

return recipes


def arguments_post(args):

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

if args.files == ['.']:
args.files = expand_paths(args.files)

if args.rulefile:
try:
Expand Down Expand Up @@ -103,16 +140,13 @@ def arguments_post(args):
raise argparse.ArgumentTypeError(
'mod file \'{file}\' is not a valid file'.format(file=mod))

if args.color:
set_colorize(True)
if args.nowarn:
set_nowarn(True)
if args.noinfo:
set_noinfo(True)
if args.relpaths:
set_relpaths(True)
set_noid(args.noid)
set_colorize(args.color)
set_nowarn(args.nowarn)
set_noinfo(args.noinfo)
set_relpaths(args.relpaths)
set_suppressions(args.suppress)

return args


Expand Down Expand Up @@ -178,7 +212,8 @@ def run(args):
issues = []
fixedfiles = []
groups = group_files(args.files)
for group in groups:
for i_group, group in enumerate(groups):
print(f'oelint processing file {i_group+1} out of {len(groups)}') # noqa: T001
stash = Stash(args)
for f in group:
try:
Expand Down
19 changes: 8 additions & 11 deletions oelint_adv/cls_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,14 @@ def load_rules(args, add_rules=(), add_dirs=()):
try:
mod = importlib.import_module(name)
for m in inspect.getmembers(mod, inspect.isclass):
try:
if issubclass(m[1], Rule):
inst = m[1]()
_potential_ids = [
inst.ID] + ['{a}.{b}'.format(a=inst.ID, b=x) for x in inst.Appendix]
if any(_potential_ids):
if _rule_file and not any(x in _rule_file for x in _potential_ids):
continue # pragma: no cover
res.append(inst)
except Exception: # pragma: no cover
pass # pragma: no cover
if issubclass(m[1], Rule):
inst = m[1]()
_potential_ids = [
inst.ID] + ['{a}.{b}'.format(a=inst.ID, b=x) for x in inst.Appendix]
if any(_potential_ids):
if _rule_file and not any(x in _rule_file for x in _potential_ids):
continue # pragma: no cover
res.append(inst)
except Exception as e: # pragma: no cover
if not args.quiet: # pragma: no cover
print( # noqa: T001 this print is fine here
Expand Down
2 changes: 1 addition & 1 deletion tests/test_user_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_relpaths(self, input):
os.chdir(_cwd)

def test_missing_file_args(self):
with pytest.raises(argparse.ArgumentTypeError):
with pytest.raises(SystemExit):
_args = self._create_args({})

@pytest.mark.parametrize('input',
Expand Down