Junk table dispatch for execute

This commit is contained in:
Reid 'arrdem' McKenzie 2022-07-28 20:46:56 -06:00
parent 95043de7e4
commit 438df41f91

View file

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