A Python-based static analysis tool for evaluating the health, structure, maintainability, and complexity of Python codebases.
The analyzer uses Python's Abstract Syntax Tree (AST) to inspect source files, identify potential code-quality issues, detect duplicated functions, and generate codebase health reports.
-
π Recursively scans Python files in a directory
-
π Calculates line-level metrics
- Total lines
- Code lines
- Blank lines
- Comment lines
-
π§© Analyzes code structure
- Functions
- Classes
- Imports
- From imports
-
π Analyzes control flow
ifstatementsforloopswhileloopstryblocks
-
βοΈ Analyzes operations
- Function calls
- Return statements
- Raised exceptions
- Assertions
- π Function starting line
- π Function length
- π’ Number of arguments
- π§ Cyclomatic-style complexity
- β»οΈ Structural duplicate-function detection using AST normalization
-
β οΈ Detects long functions -
β οΈ Detects functions with too many arguments -
β οΈ Detects high-complexity functions -
π Detects
TODOcomments -
π Detects
FIXMEcomments -
β€οΈ Calculates a codebase health score
-
π·οΈ Assigns a health rating:
- π’ Excellent
- π΅ Good
- π Needs Improvement
- π΄ Poor
- π₯οΈ Detailed terminal reports
- π Machine-readable JSON reports
- β»οΈ Duplicate-code reporting in terminal and JSON output
- β
Automated test suite using
pytest - βοΈ Continuous integration using GitHub Actions
- π‘οΈ Handles Python files containing syntax errors without stopping the entire analysis
- π Python 3.10+
- π§ͺ
pytestfor running tests
Clone the repository:
git clone https://github.com/Azaucifer/codebase-health-analyzer.git
cd codebase-health-analyzerInstall the required dependencies:
pip install -r requirements.txtThe analyzer can be run from the project root using analyzer.py.
Provide the path to the Python project you want to analyze:
python analyzer.py C:/path/to/projectThe analyzer recursively scans the directory for Python files and generates a health report containing:
- π Line-level metrics
- π§© Code structure
- π Control-flow metrics
- π§ Function analysis
- π Complexity information
β οΈ Code-quality issues- β»οΈ Duplicate-function detection
- β€οΈ Health score and rating
To view the available command-line options:
python analyzer.py --helpUse the --json option to generate a machine-readable report:
python analyzer.py C:/path/to/project --jsonThe report is saved as:
codebase_report.json
This can be useful for π€ automation, further analysis, or integration with other tools.
For a project located at:
C:/Users/example/projects/my-python-project
run:
python analyzer.py C:/Users/example/projects/my-python-projectTo generate both the terminal analysis and JSON report:
python analyzer.py C:/Users/example/projects/my-python-project --jsonGenerated JSON reports are excluded from version control through .gitignore.
==================================================
CODEBASE HEALTH REPORT
==================================================
File: example.py
Lines
--------------------
Total lines: 120
Code lines: 85
Blank lines: 25
Comment lines: 10
Structure
--------------------
Functions: 8
Classes: 2
Imports: 5
From imports: 2
Control Flow
--------------------
If statements: 12
For loops: 4
While loops: 1
Try blocks: 2
Function Analysis
--------------------
process_data
Start Line: 24
Lines: 38
Arguments: 6
Complexity: 12
Quality Issues
--------------------
TODOs: 2
FIXMEs: 1
WARNING: process_data (Line 24): long function
WARNING: process_data (Line 24): too many arguments
WARNING: process_data (Line 24): high complexity (12)
Health Score
--------------------
Score: 72/100
Rating: Needs Improvement
The analyzer also identifies structurally identical functions.
For example:
Duplicate Code
--------------------
Duplicate groups: 1
Group 1
add() - one.py:1
calculate() - two.py:1
Functions can be detected as duplicates even when their function names and argument names differ, provided their underlying AST structure is equivalent.
The analyzer calculates a health score based on detected code-quality issues and structural characteristics of the analyzed codebase.
The score provides a high-level indication of codebase health and is accompanied by a health rating:
- π’ Excellent
- π΅ Good
- π Needs Improvement
- π΄ Poor
The health score is intended as a high-level analysis tool and is not a replacement for dedicated linters, testing tools, security scanners, or code review.
Using the --json option produces a machine-readable report that can be used by other tools or future automation.
Example structure:
{
"summary": {
"python_files": 4,
"total_lines": 1590,
"total_functions": 94,
"total_classes": 0,
"total_todos": 10,
"total_fixmes": 7,
"average_health_score": 87.5,
"rating": "Good"
},
"duplicates": [
{
"functions": [
{
"file": "one.py",
"name": "add",
"start_line": 1
},
{
"file": "two.py",
"name": "calculate",
"start_line": 1
}
]
}
],
"files": [
{
"file": "example.py",
"total_lines": 120,
"functions": 8,
"classes": 2,
"health_score": 84
}
]
}The exact values depend on the codebase being analyzed.
Run the complete test suite with:
python -m pytest test_analyzer.pyThe test suite currently contains 61 tests covering:
- π Line analysis
- π§ Complexity calculation
- π Function analysis
- π¦ Import analysis
- π Control-flow analysis
- ποΈ Class detection
- βοΈ Operation analysis
β οΈ Quality issue detection- β€οΈ Health score calculation
- β»οΈ Duplicate-function detection
- π‘οΈ Duplicate detection with syntax errors
- π» CLI behavior
- π JSON report generation
- π JSON duplicate-report integration
- π¨ Syntax error handling
GitHub Actions also runs the test suite across supported Python versions.
Codebase Health Analyzer was built to explore how static-analysis tools can inspect Python source code without executing it.
The project focuses on understanding:
- π§© Python's Abstract Syntax Tree
- π Code metrics and complexity
- π‘οΈ Automated code-quality analysis
- ποΈ Modular software architecture
- π» CLI application design
- π JSON-based reporting
- π§ͺ Automated testing
- βοΈ Continuous integration
codebase-health-analyzer/
β
βββ analysis/
β βββ __init__.py
β βββ ast_analysis.py
β βββ complexity.py
β βββ duplicate_detection.py
β βββ file_analysis.py
β βββ lines.py
β βββ quality.py
β
βββ cli/
β βββ __init__.py
β βββ arguments.py
β
βββ reporting/
β βββ json_report.py
β βββ terminal.py
β
βββ analyzer.py
βββ test_analyzer.py
βββ requirements.txt
βββ .gitignore
βββ README.md
The analyzer follows several stages:
Python Codebase
β
βΌ
π Find Python Files
β
βββββββββββββββββββββββ
βΌ βΌ
π§© Parse Source with AST π Analyze Lines
β
βββ Structure Analysis
βββ Control Flow Analysis
βββ Operation Analysis
βββ Function Analysis
βββ Duplicate Detection
β
βΌ
π‘οΈ Quality Analysis
β
βΌ
β€οΈ Health Score
β
βββ π₯οΈ Terminal Report
βββ π JSON Report
The project separates analysis, command-line handling, and reporting into dedicated modules:
analyzer.py
β
βββ cli/
β βββ arguments.py
β
βββ analysis/
β βββ file_analysis.py
β βββ lines.py
β βββ ast_analysis.py
β βββ complexity.py
β βββ quality.py
β βββ duplicate_detection.py
β
βββ reporting/
βββ terminal.py
βββ json_report.py
This separation keeps individual responsibilities isolated and makes the analyzer easier to π§ͺ test, π§ maintain, and π extend.
Contributions are welcome! π€
If you would like to contribute, please open an issue to discuss significant changes before starting work.
Small bug fixes, tests, documentation improvements, and focused feature contributions are welcome.
The project is actively being developed.
Current capabilities include:
- π Python source-code analysis using AST
- π Code and structural metrics
- π§ Function complexity analysis
- π‘οΈ Code-quality checks
- β€οΈ Health scoring
- β»οΈ Duplicate-function detection
- π₯οΈ Terminal reporting
- π JSON reporting
- π§ͺ Automated testing
- βοΈ Continuous integration with GitHub Actions
Development will focus on improvements that provide meaningful value to developers while keeping the analyzer focused and maintainable.
This project is currently intended as an open-source learning and development project.