Merge branch 'master' of ssh://gitea.locusworks.net:7999/hcorse/scripts
This commit is contained in:
44
rdify
44
rdify
@@ -41,24 +41,43 @@ class RamDiskItem():
|
|||||||
return "PERSIST_ONLY" # Symlink broken, persistent copy exists
|
return "PERSIST_ONLY" # Symlink broken, persistent copy exists
|
||||||
case (True, False, _, False):
|
case (True, False, _, False):
|
||||||
return "UNRECOVERABLE" # Symlink broken, persistent copy missing
|
return "UNRECOVERABLE" # Symlink broken, persistent copy missing
|
||||||
|
case (False, False, _, _):
|
||||||
|
return "MISSING_TARGET" # Target missing
|
||||||
case _:
|
case _:
|
||||||
eprint("Unhandled state:", path_states)
|
eprint("Unhandled state:", path_states)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
self.persist.mkdir(parents=True, exist_ok=True)
|
match self.check_state():
|
||||||
self.ramdisk.mkdir(parents=True, exist_ok=True)
|
case "UNINITIALIZED":
|
||||||
|
self.persist.mkdir(parents=True, exist_ok=True)
|
||||||
|
self.ramdisk.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
if self.target.is_file():
|
if self.target.is_file():
|
||||||
shutil.copy2(self.target, self.ramdisk)
|
shutil.copy2(self.target, self.ramdisk)
|
||||||
else:
|
else:
|
||||||
shutil.copytree(self.target, self.full_rd_path, dirs_exist_ok=True)
|
shutil.copytree(self.target, self.full_rd_path, dirs_exist_ok=True)
|
||||||
shutil.move(self.target, self.persist)
|
shutil.move(self.target, self.persist)
|
||||||
|
|
||||||
os.symlink(self.full_rd_path, self.target)
|
os.symlink(self.full_rd_path, self.target)
|
||||||
|
case "RAMDISKIFIED":
|
||||||
|
pass
|
||||||
|
case "NONPERSISTENT":
|
||||||
|
self.persist.mkdir(parents=True, exist_ok=True)
|
||||||
|
self.sync()
|
||||||
|
case "PERSIST_ONLY":
|
||||||
|
if self.full_persist_path.is_file():
|
||||||
|
shutil.copy2(self.full_persist_path, self.ramdisk)
|
||||||
|
else:
|
||||||
|
shutil.copytree(self.full_persist_path, self.full_rd_path, dirs_exist_ok=True)
|
||||||
|
case _:
|
||||||
|
eprint(f"Cannot initialize: {self.check_state()}")
|
||||||
|
|
||||||
|
|
||||||
def sync(self):
|
def sync(self):
|
||||||
|
if self.check_state() != "RAMDISKIFIED":
|
||||||
|
self.init()
|
||||||
# TODO: actually look at output from this...
|
# TODO: actually look at output from this...
|
||||||
subprocess.run(["rsync", "-aP", "--delete", self.full_rd_path, self.rsync_destination])
|
subprocess.run(["rsync", "-aP", "--delete", self.full_rd_path, self.rsync_destination])
|
||||||
|
|
||||||
@@ -73,9 +92,8 @@ class RamDiskItem():
|
|||||||
return
|
return
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
if not self.target.is_symlink():
|
if self.check_state() != "RAMDISKIFIED":
|
||||||
eprint(f"Target: {self.target} isn't a symlink!")
|
self.init()
|
||||||
return
|
|
||||||
self.sync()
|
self.sync()
|
||||||
self.target.unlink()
|
self.target.unlink()
|
||||||
shutil.move(self.full_persist_path, self.target.parent)
|
shutil.move(self.full_persist_path, self.target.parent)
|
||||||
@@ -106,10 +124,6 @@ def main():
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# if not Path(args.target).exists():
|
|
||||||
# eprint(f"Target: {args.target} doesn't exist!")
|
|
||||||
# return
|
|
||||||
|
|
||||||
rd_item = RamDiskItem(args.target, args.persist)
|
rd_item = RamDiskItem(args.target, args.persist)
|
||||||
|
|
||||||
if args.diagnose:
|
if args.diagnose:
|
||||||
|
|||||||
Reference in New Issue
Block a user