def check_namespace(parsed_module: Statement) -> bool: try: namespace = parsed_module.search('namespace')[0].arg except: return False if 'urn:cisco' in namespace: return False if 'urn:' in namespace: return True for ns, _ in NS_MAP: if ns in namespace: return True return False
def check_revision(parsed_module: Statement) -> bool: try: revision = parsed_module.search('revision')[0].arg except: return False revision_parts = [int(i) for i in revision.split('-')] try: date(*revision_parts) except: if revision_parts[1:] == [2, 29]: revision_parts[2] = 28 try: date(*revision_parts) except: return False else: return False return True
def check_dependencies(dep_type: t.Literal['import', 'include'], parsed_module: Statement, directory: str, yang_models_dir: str) -> t.Tuple[t.Set[str], t.Set[str]]: all_modules: t.Set[str] = set() missing_modules: t.Set[str] = set() for dependency in parsed_module.search(dep_type): name = dependency.arg all_modules.add(name) revisions = dependency.search('revision-date') revision = revisions[0].arg if revisions else '*' pattern = '{}.yang'.format(name) pattern_with_revision = '{}@{}.yang'.format(name, revision) if not find_first_file(directory, pattern, pattern_with_revision, yang_models_dir): # TODO: if the matched filename doesn't include the revision, maybe we should check it? if revision != '*': missing_modules.add('{}@{}'.format(name, revision)) else: missing_modules.add(name) return all_modules, missing_modules