Match the config file in workdir recursively given the pattern. Additionally, if the pattern itself points to an existing file, it will be directly returned.
(workdir: Union[str, List[str]],
pattern: Union[str, List[str]])
| 20 | logger = get_logger() |
| 21 | |
| 22 | def match_cfg_file(workdir: Union[str, List[str]], |
| 23 | pattern: Union[str, List[str]]) -> List[Tuple[str, str]]: |
| 24 | """Match the config file in workdir recursively given the pattern. |
| 25 | |
| 26 | Additionally, if the pattern itself points to an existing file, it will be |
| 27 | directly returned. |
| 28 | """ |
| 29 | def _mf_with_multi_workdirs(workdir, pattern, fuzzy=False): |
| 30 | if isinstance(workdir, str): |
| 31 | workdir = [workdir] |
| 32 | files = [] |
| 33 | for wd in workdir: |
| 34 | files += match_files(wd, pattern, fuzzy=fuzzy) |
| 35 | return files |
| 36 | |
| 37 | if isinstance(pattern, str): |
| 38 | pattern = [pattern] |
| 39 | pattern = [p + '.py' if not p.endswith('.py') else p for p in pattern] |
| 40 | files = _mf_with_multi_workdirs(workdir, pattern, fuzzy=False) |
| 41 | if len(files) != len(pattern): |
| 42 | nomatched = [] |
| 43 | ambiguous = [] |
| 44 | ambiguous_return_list = [] |
| 45 | err_msg = ('The provided pattern matches 0 or more than one ' |
| 46 | 'config. Please verify your pattern and try again. ' |
| 47 | 'You may use tools/list_configs.py to list or ' |
| 48 | 'locate the configurations.\n') |
| 49 | for p in pattern: |
| 50 | files_ = _mf_with_multi_workdirs(workdir, p, fuzzy=False) |
| 51 | if len(files_) == 0: |
| 52 | nomatched.append([p[:-3]]) |
| 53 | elif len(files_) > 1: |
| 54 | ambiguous.append([p[:-3], '\n'.join(f[1] for f in files_)]) |
| 55 | ambiguous_return_list.append(files_[0]) |
| 56 | if nomatched: |
| 57 | table = [['Not matched patterns'], *nomatched] |
| 58 | err_msg += tabulate.tabulate(table, |
| 59 | headers='firstrow', |
| 60 | tablefmt='psql') |
| 61 | if ambiguous: |
| 62 | table = [['Ambiguous patterns', 'Matched files'], *ambiguous] |
| 63 | warning_msg = 'Found ambiguous patterns, using the first matched config.\n' |
| 64 | warning_msg += tabulate.tabulate(table, |
| 65 | headers='firstrow', |
| 66 | tablefmt='psql') |
| 67 | logger.warning(warning_msg) |
| 68 | return ambiguous_return_list |
| 69 | |
| 70 | raise ValueError(err_msg) |
| 71 | return files |
| 72 | |
| 73 | |
| 74 | def try_fill_in_custom_cfgs(config): |
no test coverage detected