Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.
Open
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
11 changes: 8 additions & 3 deletions kcc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

def run(*,
config_file: str = '',
query: bool = False):
query: bool = False,
missings: bool = False):
if query:
must_be_set, must_be_set_or_module, must_be_unset = \
KernelSelfProtectionProject()
Expand All @@ -24,18 +25,22 @@ def run(*,
must_be_unset.update(MUST_BE_UNSET)

kconfig = Kconfig.default()
results = kconfig.check(config_file)
if not results:
results, missing_keys = kconfig.check(config_file)
if not results and not missing_keys:
return
for opt, msg in results.items():
print(opt, msg)
if missings:
for key in missing_keys:
print(key)
return 1

def cli() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('config_file', nargs='?', type=argparse.FileType('r'),
default=sys.stdin)
parser.add_argument('--query', default=False, action='store_true')
parser.add_argument('--missings', default=False, action='store_true')
args = parser.parse_args()
ret = run(**vars(args))
args.config_file.close()
Expand Down
19 changes: 17 additions & 2 deletions kcc/kconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Kconfig(object):
Y_MUST_N = "is set but is required to be not set (%s)"
M_MUST_Y = "is set as =m but is required to be set to y (%s)"
M_MUST_N = "is set as =m but is required to be not set (%s)"
MISSING_MUST_BE_SET = "\nIs not in config file and must be set:"

def __init__(self, *,
must_be_set: Dict[str, str] = {},
Expand All @@ -40,12 +41,26 @@ def default(cls):

def check(self, stream: Iterable[str]) -> Dict[str, str]:
results: Dict[str, str] = {}
missing = []
missing_key_mbs = list(self.must_be_set.keys())
missing_key_mbsom = list(self.must_be_set_or_module.keys())

for line in stream:
line = line.strip()
results.update(self.check_line(line))
return results
line = line.lstrip("#").rstrip("=y").lstrip(" ").rstrip(" is not set").rstrip(" ")
for mbs_key in missing_key_mbs:
if mbs_key == line:
missing_key_mbs.remove(mbs_key)
for mbsom_key in missing_key_mbsom:
if mbsom_key == line:
missing_key_mbsom.remove(mbsom_key)

missing = missing_key_mbs + missing_key_mbsom
missing.insert(0,self.MISSING_MUST_BE_SET)
return results, missing

def check_line(self, line: str) -> Dict[str, str]:
line = line.strip()
match = re.search("^# (CONFIG_.*) is not set", line)
if match:
notset = match.group(1)
Expand Down