Created
March 4, 2022 08:35
-
-
Save mortie/35fdaf022cfaa1314f0dfc3bc8885424 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pathlib import Path | |
import time | |
import sys | |
dirpath = sys.argv[1] | |
def fsize(f): | |
try: | |
return f.stat().st_size | |
except: | |
return 0 | |
def sum_dir(path): | |
while True: | |
try: | |
return sum(fsize(file) for file in Path(path).rglob('*')) | |
except: pass | |
def pretty_size(size): | |
sz = abs(size) | |
if sz > 1024 * 1024 * 1024: | |
return f"{size / (1024 * 1024 * 1024)} GiB" | |
elif sz > 1024 * 1024: | |
return f"{size / (1024 * 1024)} MiB" | |
elif sz > 1024: | |
return f"{size / 1024} kiB" | |
else: | |
return f"{size} B" | |
def calc_rate(duration, delta_size): | |
bps = delta_size / duration | |
return f"{pretty_size(bps)}/s" | |
prev_size = sum_dir(dirpath) | |
prev_time = time.time() | |
print(pretty_size(prev_size)) | |
while True: | |
time.sleep(4) | |
size = sum_dir(dirpath) | |
t = time.time() | |
print(pretty_size(size), calc_rate(t - prev_time, size - prev_size)) | |
prev_size = size | |
prev_time = t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment