Last active
November 5, 2024 08:54
-
-
Save lucywoodman/1e192ae3bcb9d1c3c9df568bbdab92c7 to your computer and use it in GitHub Desktop.
Env Scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from pathlib import Path | |
from collections import defaultdict | |
def scan_env_usage(): | |
# Load current .env variables | |
env_vars = set() | |
if os.path.exists('.env'): | |
with open('.env', 'r') as f: | |
for line in f: | |
line = line.strip() | |
if line and not line.startswith('#'): | |
key = line.split('=')[0].strip() | |
env_vars.add(key) | |
# Initialize results | |
usage_count = defaultdict(int) | |
file_references = defaultdict(list) | |
# Define patterns to search for | |
patterns = [ | |
r'process\.env\.([A-Z_][A-Z0-9_]*)', # JavaScript/Node | |
r'os\.environ\.get\([\'"]([A-Z_][A-Z0-9_]*)[\'"]', # Python | |
r'\$\{([A-Z_][A-Z0-9_]*)\}', # Shell/Docker | |
r'env\([\'"]([A-Z_][A-Z0-9_]*)[\'"]', # PHP/Laravel | |
r'ENV\[[\'"]([A-Z_][A-Z0-9_]*)[\'"]' # Ruby | |
] | |
# File extensions to scan | |
extensions = {'.js', '.jsx', '.ts', '.tsx', '.py', '.php', '.rb', '.env', '.yaml', '.yml', '.sh', '.Dockerfile'} | |
def scan_file(file_path): | |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: | |
content = f.read() | |
for pattern in patterns: | |
matches = re.findall(pattern, content) | |
for var_name in matches: | |
usage_count[var_name] += 1 | |
file_references[var_name].append(str(file_path)) | |
# Scan all files recursively | |
for path in Path('.').rglob('*'): | |
if path.suffix in extensions and 'node_modules' not in str(path) and 'venv' not in str(path) and 'vendor' not in str(path): | |
try: | |
scan_file(path) | |
except Exception as e: | |
print(f"Error scanning {path}: {e}") | |
# Generate report | |
print("\nEnvironment Variable Usage Report") | |
print("=" * 50) | |
# Variables in .env but not used in code | |
unused_vars = env_vars - set(usage_count.keys()) | |
if unused_vars: | |
print("\nUnused variables in .env:") | |
for var in sorted(unused_vars): | |
print(f"- {var}") | |
# Variables used in code but not in .env | |
missing_vars = set(usage_count.keys()) - env_vars | |
if missing_vars: | |
print("\nVariables used in code but missing from .env:") | |
for var in sorted(missing_vars): | |
print(f"- {var} (used in {usage_count[var]} places)") | |
for ref in file_references[var][:3]: # Show first 3 references | |
print(f" → {ref}") | |
# Most frequently used variables | |
print("\nMost frequently used variables:") | |
for var, count in sorted(usage_count.items(), key=lambda x: x[1], reverse=True)[:10]: | |
print(f"- {var}: {count} references") | |
if __name__ == '__main__': | |
scan_env_usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment