#!/usr/bin/env python # Copyright (c) OpenMMLab. All rights reserved. # This tool is used to update model-index.yml which is required by MIM, and # will be automatically called as a pre-commit hook. The updating will be # triggered if any change of model information (.md files in configs/) has been # detected before a commit. import os import os.path as osp import re import sys from typing import List, Tuple import yaml MMSEG_ROOT = osp.abspath(osp.join(osp.dirname(__file__), '..')) def get_collection_name_list(md_file_list: List[str]) -> List[str]: """Get the list of collection names.""" collection_name_list: List[str] = [] for md_file in md_file_list: with open(md_file) as f: lines = f.readlines() collection_name = lines[0].split('#')[1].strip() collection_name_list.append(collection_name) return collection_name_list def get_md_file_list() -> Tuple[List[str], List[str]]: """Get the list of md files.""" md_file_list: List[str] = [] md_dir_list: List[str] = [] for root, _, files in os.walk(osp.join(MMSEG_ROOT, 'configs')): for file in files: if file.endswith('.md'): md_file_list.append(osp.join(root, file)) md_dir_list.append(root) break return md_file_list, md_dir_list def get_model_info(md_file: str, config_dir: str, collection_name_list: List[str]) -> Tuple[dict, str]: """Get model information from md file.""" datasets: List[str] = [] models: List[dict] = [] current_dataset: str = '' paper_name: str = '' paper_url: str = '' code_url: str = '' is_backbone: bool = False is_dataset: bool = False collection_name: str = '' with open(md_file) as f: lines: List[str] = f.readlines() i: int = 0 while i < len(lines): line: str = lines[i].strip() if len(line) == 0: i += 1 continue # get paper name and url if re.match(r'> \[.*\]+\([a-zA-Z]+://[^\s]*\)', line): paper_info = line.split('](') paper_name = paper_info[0][paper_info[0].index('[') + 1:] paper_url = paper_info[1][:len(paper_info[1]) - 1] # get code info if 'Code Snippet' in line: code_url = line.split('"')[1].split('"')[0] if line.startswith('