#!/usr/bin/env python3 """ Analysis Report Viewer This script provides utilities to view and filter the codebase analysis results. """ import json import sys from typing import Dict, List, Optional def load_analysis_report(file_path: str = "analysis_report.json") -> Dict: """Load the analysis report from JSON file""" try: with open(file_path, 'r') as f: return json.load(f) except FileNotFoundError: print(f"Analysis report not found: {file_path}") print("Run 'python3 scripts/utilities/backup_and_analyze.py' first to generate the report.") sys.exit(1) def filter_files_by_type(report: Dict, file_type: str) -> List[Dict]: """Filter files by type""" return [f for f in report['files'] if f['file_type'] == file_type] def filter_files_by_action(report: Dict, action: str) -> List[Dict]: """Filter files by suggested action""" return [f for f in report['files'] if f['suggested_action'] == action] def show_summary(report: Dict): """Show analysis summary""" print("=== Codebase Analysis Summary ===") print(f"Analysis timestamp: {report['timestamp']}") print(f"Total files: {report['total_files']}") print(f"Files with dependencies: {report['dependency_stats']['total_files_with_dependencies']}") print(f"Average dependencies per file: {report['dependency_stats']['average_dependencies_per_file']:.2f}") print() print("=== File Type Breakdown ===") for file_type, count in sorted(report['file_type_counts'].items()): print(f"{file_type:20}: {count:6}") print() print("=== Suggested Actions ===") for action, count in sorted(report['suggested_action_counts'].items()): print(f"{action:20}: {count:6}") print() def show_files_by_type(report: Dict, file_type: str): """Show files of a specific type""" files = filter_files_by_type(report, file_type) print(f"=== Files of type: {file_type} ({len(files)} files) ===") for file_info in sorted(files, key=lambda x: x['path']): print(f"{file_info['path']:50} | {file_info['size']:8} bytes | {file_info['suggested_action']}") print() def show_files_by_action(report: Dict, action: str): """Show files with a specific suggested action""" files = filter_files_by_action(report, action) print(f"=== Files with action: {action} ({len(files)} files) ===") for file_info in sorted(files, key=lambda x: x['path']): print(f"{file_info['path']:50} | {file_info['file_type']:15} | {file_info['size']:8} bytes") print() def show_dependencies(report: Dict, file_path: Optional[str] = None): """Show dependency information""" if file_path: # Show dependencies for specific file if file_path in report['dependency_graph']: deps = report['dependency_graph'][file_path] print(f"=== Dependencies for {file_path} ===") for dep in sorted(deps): print(f" -> {dep}") else: print(f"No dependencies found for {file_path}") else: # Show files with most dependencies dep_counts = [(f, len(deps)) for f, deps in report['dependency_graph'].items()] dep_counts.sort(key=lambda x: x[1], reverse=True) print("=== Files with Most Dependencies (top 20) ===") for file_path, count in dep_counts[:20]: print(f"{file_path:50} | {count:3} dependencies") print() def main(): """Main function with command-line interface""" if len(sys.argv) < 2: print("Usage: python3 scripts/utilities/view_analysis.py [options]") print() print("Commands:") print(" summary - Show analysis summary") print(" type - Show files of specific type") print(" action - Show files with specific action") print(" deps [file_path] - Show dependency information") print() print("Available file types:") print(" python_source, python_test, python_cache, documentation,") print(" configuration, migration, backup, deployment, data, unknown") print() print("Available actions:") print(" keep, move, archive, delete, consolidate, rename") sys.exit(1) command = sys.argv[1] report = load_analysis_report() if command == "summary": show_summary(report) elif command == "type" and len(sys.argv) > 2: show_files_by_type(report, sys.argv[2]) elif command == "action" and len(sys.argv) > 2: show_files_by_action(report, sys.argv[2]) elif command == "deps": file_path = sys.argv[2] if len(sys.argv) > 2 else None show_dependencies(report, file_path) else: print(f"Unknown command: {command}") sys.exit(1) if __name__ == "__main__": main()