Bugfix: remove all instances when optimizing

Fixes #7
This commit is contained in:
Reid 'arrdem' McKenzie 2022-11-28 20:44:30 -07:00
parent 712731f4fe
commit 6480a51a82
2 changed files with 8 additions and 6 deletions

View file

@ -194,6 +194,10 @@ def load_state(statefile: Path) -> Vfs:
return oldfs return oldfs
def remove_all(l, it):
return [i for i in l if i != it]
def simplify(old_fs: Vfs, new_fs: Vfs, /, exec_idempotent=True) -> Vfs: def simplify(old_fs: Vfs, new_fs: Vfs, /, exec_idempotent=True) -> Vfs:
"""Try to reduce a new VFS using diff from the original VFS.""" """Try to reduce a new VFS using diff from the original VFS."""
@ -202,15 +206,12 @@ def simplify(old_fs: Vfs, new_fs: Vfs, /, exec_idempotent=True) -> Vfs:
initial_new_steps = len(new_fs._log) initial_new_steps = len(new_fs._log)
# Scrub anything in the new log that's in the old log # Scrub anything in the new log that's in the old log
for txn in list(old_fs._log): for txn in old_fs._log:
# Except for execs which are stateful # Except for execs which are stateful
if txn[0] == "exec" and not exec_idempotent: if txn[0] == "exec" and not exec_idempotent:
continue continue
try: new_fs._log = remove_all(new_fs._log, txn)
new_fs._log.remove(txn)
except ValueError:
pass
# Dedupe the new log while preserving order. # Dedupe the new log while preserving order.
keys = set() keys = set()
@ -220,6 +221,7 @@ def simplify(old_fs: Vfs, new_fs: Vfs, /, exec_idempotent=True) -> Vfs:
if key not in keys: if key not in keys:
keys.add(key) keys.add(key)
deduped.append(op) deduped.append(op)
new_fs._log = deduped new_fs._log = deduped
log.info(f"Optimized out {initial_new_steps - len(new_fs._log)} steps") log.info(f"Optimized out {initial_new_steps - len(new_fs._log)} steps")

View file

@ -49,8 +49,8 @@ def stow(fs: Vfs, src_dir: Path, dest_dir: Path, skip=[]):
dest = dest_root / src.relative_to(src_root) dest = dest_root / src.relative_to(src_root)
if src.is_symlink(): if src.is_symlink():
fs.link(src.readlink().resolve(), dest) fs.link(src.readlink().resolve(), dest)
elif src.is_dir(): elif src.is_dir():
fs.mkdir(dest) fs.mkdir(dest)
fs.chmod(dest, src.stat().st_mode) fs.chmod(dest, src.stat().st_mode)