diff --git a/kcc/cli.py b/kcc/cli.py index 58d0355..3a8a6fa 100644 --- a/kcc/cli.py +++ b/kcc/cli.py @@ -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() @@ -24,11 +25,14 @@ 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: @@ -36,6 +40,7 @@ def cli() -> None: 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() diff --git a/kcc/kconfig.py b/kcc/kconfig.py index 76faf98..b743b31 100644 --- a/kcc/kconfig.py +++ b/kcc/kconfig.py @@ -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] = {}, @@ -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)