Simple tool I wrote in an hour or two (I'm not very fast, but I eventually get there - also for the "spending 6 hours to save 6 seconds" meme).
Would've written this in shellscript given that I'm calling external programs to do the actual measuring but shellscript doesn't math very well, and so I figured I might as well write the entire thing in Python so I can do the measuring and mathing in a single language (though I suppose it is arguable whether shelling out to call an external program still counts as "single language").
It may be a simple tool but I'm sure holes can still be poked in it. Tested on Python 3.11.9 (Cygwin provides the stat
binary for Windows), but given its simplicity it probably works on any version (with string interpolation, but it should also be pretty simple to modify it to not require that).
#!/usr/bin/env python
import os
import subprocess
import sys
from typing import List
def main(args: List[str]) -> int:
argc = len(args)
if argc != 2 and argc != 3:
print(f"Usage: {args[0]} [file]")
print(f" {args[0]} [oldfile] [newfile]")
return 1
if argc == 2:
result = subprocess.run(f"stat {args[1]} --printf=%s", shell=True, capture_output=True)
if result.returncode != 0:
print(f"Bad file: {args[1]}")
return 2
print(f"{args[1]} is {int(result.stdout)} bytes")
return 0
if argc == 3:
result_oldfile = subprocess.run(f"stat {args[1]} --printf=%s", shell=True, capture_output=True)
if result_oldfile.returncode != 0:
print(f"Bad file: {args[1]}")
return 2
result_newfile = subprocess.run(f"stat {args[2]} --printf=%s", shell=True, capture_output=True)
if result_newfile.returncode != 0:
print(f"Bad file: {args[2]}")
return 2
size_oldfile = int(result_oldfile.stdout)
size_newfile = int(result_newfile.stdout)
delta = size_newfile / size_oldfile * 100
if delta > 100:
print(f"{args[2]} is {round(delta - 100, 2)}% larger than {args[1]}")
elif delta == 100:
print(f"{args[2]} is the same size as {args[1]}")
else:
print(f"{args[2]} is {round(100 - delta, 2)}% smaller than {args[1]}")
return 0
return 3
if __name__ == "__main__":
args = sys.argv
args[0] = os.path.basename(args[0])
sys.exit(main(args))