diff --git a/rdify b/rdify index 1e88c70..d6baf32 100755 --- a/rdify +++ b/rdify @@ -6,49 +6,70 @@ import os import shutil import subprocess import sys +import time from pathlib import Path def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) -def init(target, persist): - target = Path(target) - persist = Path(persist) - persist.mkdir(parents=True, exist_ok=True) - ramdisk = Path("/dev/shm/ramdisk") - ramdisk.mkdir(parents=True, exist_ok=True) - - full_dest_path = ramdisk.joinpath(target.name) - if target.is_file(): - shutil.copy2(target, ramdisk) - else: - shutil.copytree(target, full_dest_path) - shutil.move(target, persist) - - os.symlink(full_dest_path, target) +class RamDiskItem(): + def __init__(self, target, persist): + self.target = Path(target) + self.persist = Path(persist) + self.ramdisk = Path("/dev/shm/ramdisk") + self.full_rd_path = ramdisk.joinpath(target.name) + self.full_persist_path = persist.joinpath(target.name) -def sync(target, persist): - pass + def init(self): + self.persist.mkdir(parents=True, exist_ok=True) + self.ramdisk.mkdir(parents=True, exist_ok=True) -def loop_sync(target, persist): - pass -def cleanup(target, persist): - pass + if target.is_file(): + shutil.copy2(self.target, self.ramdisk) + else: + shutil.copytree(self.target, self.full_rd_path) + shutil.move(self.target, self.persist) + + os.symlink(self.full_rd_path, self.target) + + + def sync(self): + # TODO: actually look at output from this... + subprocess.run(["rsync", "-aP", "--delete", self.full_rd_path, self.full_persist_path]) + + def loop_sync(self, delay): + while True: + self.sync + time.sleep(delay) + + def cleanup(self): + self.sync() + shutil.move(self.full_persist_path, self.target.parent) + if self.full_rd_path.is_file(): + self.full_rd_path.unlink() + else: + shutil.rmtree(self.full_rd_path) + shutil.rmtree(self.full_persist_path) def main(): parser = argparse.ArgumentParser() # Arguments to specify the action to take action = parser.add_mutually_exclusive_group(required=True) - action.add_argument("-i", "--init", action="store_true", help="Initialize the target file/directory into ramdisk") - action.add_argument("-s", "--sync", action="store_true", help="Perform a single sync of the target from ramdisk to persistent storage") - action.add_argument("-l", "--loop-sync", type=int, help="Starts a repeating sync that loops every LOOP_SYNC seconds") - action.add_argument("-c", "--cleanup", action="store_true", help="clean up an existing ramdiskified target") + action.add_argument("-i", "--init", action="store_true", + help="Initialize the target file/directory into ramdisk") + action.add_argument("-s", "--sync", action="store_true", + help="Perform a single sync of the target from ramdisk to persistent storage") + action.add_argument("-l", "--loop_sync", type=int, + help="Starts a repeating sync that loops every LOOP_SYNC seconds") + action.add_argument("-c", "--cleanup", action="store_true", + help="clean up an existing ramdiskified target") # Arguments to specify what to act on parser.add_argument("target", help="The target file/directory to ramdiskify") - parser.add_argument("persist", nargs="?", default=".persist", help="The location to store the persistent copy while ramdiskified, defaults to '.persist'") + parser.add_argument("persist", nargs="?", default=".persist", + help="The location to store the persistent copy while ramdiskified, defaults to '.persist'") args = parser.parse_args() @@ -56,14 +77,16 @@ def main(): eprint(f"Target: {args.target} doesn't exist!") return + rd_item = RamDiskItem(args.target, args.persist) + if args.init: - init(args.target, args.persist) + rd_item.init() elif args.sync: - sync(args.target, args.persist) - elif args.loop-sync: - loop_sync(args.target, args.persist) + rd_item.sync() + elif args.loop_sync: + rd_item.loop_sync(args.loop_sync) elif args.cleanup: - cleanup(args.target, args.perist) + rd_item.cleanup(args.target, args.perist) else: pass