"""Educational demonstration of filesystem access by pip packages."""

import os


def _human_readable_size(size_bytes):
    """Convert bytes to a human-readable string (KB, MB, GB)."""
    if size_bytes >= 1 << 30:
        return f"{size_bytes / (1 << 30):.2f} GB"
    if size_bytes >= 1 << 20:
        return f"{size_bytes / (1 << 20):.2f} MB"
    if size_bytes >= 1 << 10:
        return f"{size_bytes / (1 << 10):.2f} KB"
    return f"{size_bytes} B"


def demonstrate_vulnerability():
    """Walk the user's home directory and report every file path and size.

    This function is purely educational. It demonstrates that any installed
    Python package can freely read your filesystem without any additional
    permissions or prompts.

    Nothing is sent anywhere — all output goes to stdout.
    """
    home = os.path.expanduser("~")

    print("=" * 72)
    print(
        "\u26a0\ufe0f  THIS PACKAGE JUST ACCESSED YOUR FILESYSTEM.\n"
        "   Imagine if it had silently exfiltrated this data instead.\n"
        "   Be careful what you pip install."
    )
    print("=" * 72)
    print()

    total_files = 0
    total_size = 0

    for dirpath, _dirnames, filenames in os.walk(home):
        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            try:
                size = os.path.getsize(filepath)
            except OSError:
                continue
            total_files += 1
            total_size += size
            print(f"  {filepath}  ({_human_readable_size(size)})")

    print()
    print("-" * 72)
    print(
        f"Total: {total_files:,} files, "
        f"{_human_readable_size(total_size)}"
    )
    print("-" * 72)
