#!/usr/bin/python import subprocess import sys import os import argparse if __name__ == "__main__": parser = argparse.ArgumentParser(description="Backup Dir Size helper") parser.add_argument('PATH') args = parser.parse_args() # check parameter # if not args.PATH.replace("/", "").isalnum(): print("Illegal Path: {} (must be alphanum + /)".format(args.PATH)) sys.exit(1) elif not args.PATH.startswith("/"): print("Path mus be absolute ({})".format(args.PATH)) sys.exit(1) elif not os.path.isdir(args.PATH): print("Path does not exist ({}".format(args.PATH)) sys.exit(1) savepath = os.path.join("/", args.PATH.replace("/", "-")) currentSize = 0 if os.path.isfile(savepath): with open(savepath) as f: currentSize = int(f.read()) # check # p = subprocess.run(["du", args.PATH], capture_output=True, encoding="utf-8") size = int(p.stdout.split("\n")[-2].split("\t")[0]) if currentSize and currentSize == size: sys.exit(2) else: print("Directory has changed {} to {}".format(currentSize, size)) with open(savepath, "w") as f: f.write(str(size)) sys.exit(0)