import os
import re

ignore_keywords = ["temp", "cache", "archive", "__pycache__", "test"]
def find_package_usage(root_dir, package_name):
  # Matches: import package, import package.sub, or from package import ...
  pattern = re.compile(
    rf"^\s*(import\s+{package_name}\b|from\s+{package_name}\b)")
  results = []

  for root, dirs, filenames in os.walk(root_dir):
    # Skip hidden/virtual environment directories
    # Modify dirs in-place to skip unwanted folders
    dirs[:] = [
      d for d in dirs 
      if not d.startswith(".") 
      and not any(keyword in d for keyword in ignore_keywords)
    ]
      
    for filename in filenames:
      if filename.endswith('.py'):
        filepath = os.path.join(root, filename)
        try:
          with open(filepath, 'r', encoding='utf-8') as f:
            for line_num, line in enumerate(f, 1):
              if pattern.match(line):
                results.append((filepath, line_num, line.strip()))
                break # Found in this file, move to next file
        except Exception:
          pass # Skip unreadable files
          
  return results

# Set your project path and target package
proj_dir = "./Calculix2VTK" 
target_pkg = "numpy" 

matches = find_package_usage(proj_dir, target_pkg)

print(f"Modules using '{target_pkg}':")
for path, line_no, content in matches:
  print(f"-> {path} (line {line_no}: {content})")

