"""Export explicitly reviewed, authorized source files into a readable JSON file.
Run from PyCharm with the project root as working directory.
"""
import json
import re
from pathlib import Path
ROOT = Path.cwd().resolve()
OUTPUT = ROOT / 'project_export.json'
SKIP_DIRS = {'.git', '.venv', 'venv', '.idea', '__pycache__', 'build', 'dist', 'data', 'logs', 'output', 'outputs', 'secrets', 'node_modules', '.pytest_cache'}
SUFFIXES = {'.py', '.sql', '.md', '.toml'}
SENSITIVE = re.compile(r'(?i)(password|passwd|secret|token|api[_-]?key|credential|\\\\[\w.-]+\\|(?:oracle|postgresql)(?:\+\w+)?://)')
def main():
if not (ROOT / 'src').is_dir() and not (ROOT / 'pyproject.toml').is_file():
raise SystemExit('Set the PyCharm working directory to the project root.')
files = []
for path in sorted(ROOT.rglob('*')):
rel = path.relative_to(ROOT)
if any(p.startswith('.') or p.lower() in SKIP_DIRS for p in rel.parts):
continue
if path.is_symlink() or any(p.is_symlink() for p in path.parents if p != ROOT) or not path.is_file():
continue
if path.suffix.lower() not in SUFFIXES or path.resolve() == Path(__file__).resolve():
continue
if path.stat().st_size > 500_000:
print('SKIP large:', rel); continue
try:
content = path.read_text(encoding='utf-8-sig')
except (UnicodeError, OSError):
print('SKIP unreadable:', rel); continue
if SENSITIVE.search(content) or re.search(r'(?i)(config|connection|settings|auth)', path.stem):
print('SKIP potentially sensitive:', rel); continue
print('Candidate:', rel)
files.append({'path': rel.as_posix(), 'content': content})
print(f'\n{len(files)} candidate files. Review each file for proprietary code, internal identifiers and sensitive information.')
print('Only export material explicitly authorized for external transfer. Automated checks are incomplete.')
if not files or input('Type EXPORT only after authorization and review: ').strip() != 'EXPORT':
print('Cancelled.'); return
with OUTPUT.open('x', encoding='utf-8') as f:
json.dump({'format': 'reviewed-source-v1', 'files': files}, f, ensure_ascii=False, indent=2)
print('Created:', OUTPUT)
print('Review the resulting JSON before any external transfer.')
if __name__ == '__main__':
main()