26Y07b_util

Young-Kyoo Kim·2026년 7월 7일
#!/usr/bin/env python3

import subprocess
from pathlib import Path


def read_file(path):
    try:
        return Path(path).read_text()
    except Exception:
        return ""


def read_kv_file(path):
    result = {}

    txt = read_file(path)

    for line in txt.splitlines():

        if ":" not in line:
            continue

        k, v = line.split(":", 1)

        result[k.strip()] = v.strip()

    return result


def kb_to_gb(kb):

    return kb / 1024 / 1024


def bytes_to_gb(v):

    return v / 1024 / 1024 / 1024


def run(cmd):

    try:

        return subprocess.check_output(
            cmd,
            shell=True,
            stderr=subprocess.DEVNULL,
            text=True
        )

    except Exception:

        return ""


def human_gb(v):

    return f"{v:8.2f} GB"


def pct(v, total):

    if total == 0:
        return 0

    return v * 100 / total

0개의 댓글