|
| 1 | +import os |
| 2 | +import re |
| 3 | +import argparse |
| 4 | + |
| 5 | + |
| 6 | +def search_files(directory, keywords, output_file): |
| 7 | + pattern = re.compile("|".join(rf"^[^(\n]*{re.escape(kw)}[^(\n]*\(" for kw in keywords)) |
| 8 | + |
| 9 | + configs = set() |
| 10 | + prefixes = set() |
| 11 | + count = 0 |
| 12 | + |
| 13 | + for root, _, files in os.walk(directory): |
| 14 | + for file in files: |
| 15 | + file_path = os.path.join(root, file) |
| 16 | + print(f"Retrieving from {file}...") |
| 17 | + try: |
| 18 | + with open(file_path, "r", encoding="utf-8") as f: |
| 19 | + for line in f: |
| 20 | + if pattern.search(line): |
| 21 | + count += 1 |
| 22 | + configs.add(line.strip()) |
| 23 | + prefix = line[: line.index("(")].strip() |
| 24 | + prefixes.add(prefix) |
| 25 | + except (UnicodeDecodeError, PermissionError) as e: |
| 26 | + print(f"Error reading {file_path}: {e}") |
| 27 | + continue |
| 28 | + print(f"Retrieved {count} configs") |
| 29 | + print(f"Get {len(configs)} unique configs") |
| 30 | + print(f"APIs: {sorted(prefixes)}") |
| 31 | + |
| 32 | + with open(output_file, "w", encoding="utf-8") as out_f: |
| 33 | + for line in sorted(configs): |
| 34 | + out_f.write(f"{line}\n") |
| 35 | + print(f"Saved to {output_file}") |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + default_directory = "tester/api_config/5_accuracy" |
| 40 | + default_keywords = [] |
| 41 | + default_output = "tester/api_config/api_config_retrieved.txt" |
| 42 | + |
| 43 | + parser = argparse.ArgumentParser() |
| 44 | + parser.add_argument( |
| 45 | + "--directory", |
| 46 | + default=default_directory, |
| 47 | + ) |
| 48 | + parser.add_argument("--keywords", nargs="+", default=default_keywords) |
| 49 | + parser.add_argument( |
| 50 | + "--output", |
| 51 | + default=default_output, |
| 52 | + ) |
| 53 | + args = parser.parse_args() |
| 54 | + |
| 55 | + if not os.path.isdir(args.directory): |
| 56 | + print(f"Error: Directory {args.directory} does not exist") |
| 57 | + elif not args.keywords: |
| 58 | + print("Error: Please provide at least one keyword") |
| 59 | + else: |
| 60 | + search_files(args.directory, args.keywords, args.output) |
0 commit comments