misc
Loading...
Searching...
No Matches
check-lib.py
1import ast
2import argparse
3from pathlib import Path
4from stdlib_list import stdlib_list
5
6def find_imports(source_dir: Path) -> set[str]:
7 imported_modules = set()
8
9 for py_file in source_dir.rglob("*.py"):
10 with py_file.open("r", encoding="utf-8") as f:
11 try:
12 node = ast.parse(f.read(), filename=str(py_file))
13 for stmt in ast.walk(node):
14 if isinstance(stmt, ast.Import):
15 for alias in stmt.names:
16 imported_modules.add(alias.name.split(".")[0])
17 elif isinstance(stmt, ast.ImportFrom):
18 if stmt.module:
19 imported_modules.add(stmt.module.split(".")[0])
20 except SyntaxError:
21 print(f"⚠️ Syntax error in {py_file}")
22 return imported_modules
23
24def main():
25 parser = argparse.ArgumentParser(description="Used third-party libraries finder")
26 parser.add_argument("path", type=str, help="Path to Python source directory (e.g., ./src)")
27 parser.add_argument("--python-version", type=str, default="3.11", help="Python version (e.g., 3.11)")
28 args = parser.parse_args()
29
30 source_dir = Path(args.path).resolve()
31 std_libs = set(stdlib_list(args.python_version))
32 used_imports = find_imports(source_dir)
33
34 third_party = sorted([lib for lib in used_imports if lib not in std_libs])
35
36 print("✅ 外部ライブラリ(標準ライブラリを除く):")
37 for name in third_party:
38 print("-", name)
39
40if __name__ == "__main__":
41 main()