Skip to content

Latest commit

Β 

History

64 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🩺 Codebase Health Analyzer

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.

✨ Features

πŸ” Code Analysis

  • πŸ“ 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

    • if statements
    • for loops
    • while loops
    • try blocks
  • βš™οΈ Analyzes operations

    • Function calls
    • Return statements
    • Raised exceptions
    • Assertions

🧠 Function Analysis

  • πŸ” Function starting line
  • πŸ“ Function length
  • πŸ”’ Number of arguments
  • 🧠 Cyclomatic-style complexity
  • ♻️ Structural duplicate-function detection using AST normalization

πŸ›‘οΈ Code Quality

  • ⚠️ Detects long functions

  • ⚠️ Detects functions with too many arguments

  • ⚠️ Detects high-complexity functions

  • πŸ“ Detects TODO comments

  • πŸ“ Detects FIXME comments

  • ❀️ Calculates a codebase health score

  • 🏷️ Assigns a health rating:

    • 🟒 Excellent
    • πŸ”΅ Good
    • 🟠 Needs Improvement
    • πŸ”΄ Poor

πŸ“Š Reporting

  • πŸ–₯️ Detailed terminal reports
  • πŸ“„ Machine-readable JSON reports
  • ♻️ Duplicate-code reporting in terminal and JSON output

πŸ§ͺ Reliability

  • βœ… Automated test suite using pytest
  • βš™οΈ Continuous integration using GitHub Actions
  • πŸ›‘οΈ Handles Python files containing syntax errors without stopping the entire analysis

πŸ“¦ Requirements

  • 🐍 Python 3.10+
  • πŸ§ͺ pytest for running tests

βš™οΈ Installation

Clone the repository:

git clone https://github.com/Azaucifer/codebase-health-analyzer.git
cd codebase-health-analyzer

Install the required dependencies:

pip install -r requirements.txt

πŸš€ Usage

The analyzer can be run from the project root using analyzer.py.

πŸ”Ž Analyze a Python Codebase

Provide the path to the Python project you want to analyze:

python analyzer.py C:/path/to/project

The 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

❓ Display Help

To view the available command-line options:

python analyzer.py --help

πŸ“„ Generate a JSON Report

Use the --json option to generate a machine-readable report:

python analyzer.py C:/path/to/project --json

The report is saved as:

codebase_report.json

This can be useful for πŸ€– automation, further analysis, or integration with other tools.

πŸ’‘ Example

For a project located at:

C:/Users/example/projects/my-python-project

run:

python analyzer.py C:/Users/example/projects/my-python-project

To generate both the terminal analysis and JSON report:

python analyzer.py C:/Users/example/projects/my-python-project --json

Generated JSON reports are excluded from version control through .gitignore.

πŸ“‹ Example Output

==================================================
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

♻️ Duplicate Code

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.

❀️ Health Score

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.

πŸ“¦ JSON Output

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.

πŸ§ͺ Testing

Run the complete test suite with:

python -m pytest test_analyzer.py

The 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.

🎯 Why This Project?

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

πŸ“ Project Structure

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

πŸ”„ How It Works

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

πŸ—οΈ Architecture

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.

🌍 Open Source

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.

πŸ“Œ Current Status

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.

πŸ“œ License

This project is currently intended as an open-source learning and development project.

About

A Python-based tool for analyzing codebase health, maintainability, structure, testing, and code complexity.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages