Junk table dispatch for execute

This commit is contained in:
Reid 'arrdem' McKenzie 2022-07-28 20:46:56 -06:00
parent b9617079ab
commit 5a34f095d7

View file

@ -16,64 +16,75 @@ class Vfs(object):
def __init__(self, log=None): def __init__(self, log=None):
self._log = log or [] self._log = log or []
def _execute_exec(self, e):
_, dir, cmd = e
run(cmd, cwd=str(dir))
def _execute_link(self, e):
_, src, dest = e
if dest.is_file() or dest.is_symlink():
if dest.is_symlink() and dest.readlink() == src:
return
else:
_log.warn(f"Replacing {dest}")
dest.unlink()
elif dest.is_dir():
_log.warn(f"Replacing {dest}")
rmtree(dest)
assert not dest.exists(), f"{dest} should not exist"
dest.symlink_to(src)
def _execute_chmod(self, e):
_, dest, mode = e
dest.chmod(mode)
def _execute_mkdir(self, e):
_, dest = e
if dest.is_dir():
return
elif dest.exists() or dest.is_symlink():
dest.unlink()
dest.mkdir(exist_ok=True)
def _execute_unlink(self, e):
_, dest = e
# Note that a path which is a dangling symlink will NOT exist but WILL be a symlink
if not dest.exists() and not dest.is_symlink():
return
# Files and dirs just unlink
if dest.is_symlink() or dest.is_file():
dest.unlink()
# Dirs require recursion
elif dest.is_dir():
rmtree(dest)
# Don't succeed silently
else:
raise Exception(f"Couldn't unlink {dest}")
def _execute_unimplemented(self, e):
raise NotImplementedError()
def _entry_to_command(self, e):
return e
def execute(self, /, callback=None): def execute(self, /, callback=None):
for e in self._log: for e in self._log:
_log.debug(e) cmd = self._entry_to_command(e)
_log.debug(f"Executing %r as %r", e, cmd)
if callback: if callback:
callback(e) callback(cmd)
if e[0] == "exec": # Using self as a dispatch table lol
_, dir, cmd = e getattr(self, f"_execute_{cmd[0]}", self._execute_unimplemented)(cmd)
run(cmd, cwd=str(dir))
elif e[0] == "link": def _command_to_entry(self, cmd):
_, src, dest = e return cmd
if dest.is_file() or dest.is_symlink():
if dest.is_symlink() and dest.readlink() == src:
continue
else:
_log.warn(f"Replacing {dest}")
dest.unlink()
elif dest.is_dir():
_log.warn(f"Replacing {dest}")
rmtree(dest)
assert not dest.exists(), f"{dest} should not exist" def _append(self, cmd):
dest.symlink_to(src) self._log.append(self._command_to_entry(cmd))
elif e[0] == "copy":
raise NotImplementedError()
elif e[0] == "chmod":
_, dest, mode = e
dest.chmod(mode)
elif e[0] == "mkdir":
_, dest = e
if dest.is_dir():
continue
elif dest.exists() or dest.is_symlink():
dest.unlink()
dest.mkdir(exist_ok=True)
elif e[0] == "unlink":
_, dest = e
# Note that a path which is a dangling symlink will NOT exist but WILL be a symlink
if not dest.exists() and not dest.is_symlink():
continue
# Files and dirs just unlink
if dest.is_symlink() or dest.is_file():
dest.unlink()
# Dirs require recursion
elif dest.is_dir():
rmtree(dest)
# Don't succeed silently
else:
raise Exception(f"Couldn't unlink {dest}")
def _append(self, msg):
self._log.append(msg)
def link(self, src, dest): def link(self, src, dest):
self._append(("link", src, dest)) self._append(("link", src, dest))