diff options
author | Julian T <julian@jtle.dk> | 2021-04-24 17:57:00 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-04-24 17:57:00 +0200 |
commit | 62d63afb0783b42f9f6bdbecc81b701b9d629093 (patch) | |
tree | 51e9cc191ed851648c13ef0d51a24c21e0953736 /apply/writer.py | |
parent | 1e1f021f22f82a2e261a8fcf802d5bd744e2489b (diff) |
Move apply.py into a python package
Diffstat (limited to 'apply/writer.py')
-rw-r--r-- | apply/writer.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/apply/writer.py b/apply/writer.py new file mode 100644 index 0000000..d6a5550 --- /dev/null +++ b/apply/writer.py @@ -0,0 +1,57 @@ +import shutil +from pathlib import Path + + +class Writer: + def __init__(self): + self.links_todo = {} + self.dirs_todo = [] + self.delete_todo = [] + + def create_link(self, target: Path, linkpath: Path): + if linkpath in self.links_todo: + prev = self.links_todo[linkpath] + print(f"Link {linkpath} to {target} replaces previus {prev}") + self.links_todo[linkpath] = target + + def create_dir(self, path: Path): + if path in self.dirs_todo: + return + + self.dirs_todo.append(path) + + def delete(self, path: Path): + if path in self.delete_todo: + return + + self.delete_todo.append(path) + + def apply_dir(self, path: Path, dry_run): + print(f"mkdir {path}") + if not dry_run: + path.mkdir() + + def apply_delete(self, path: Path, dry_run): + if path.is_dir(): + print(f"rmtree {path}!!!") + if not dry_run: + shutil.rmtree(str(path)) + else: + print(f"remove {path}") + if not dry_run: + path.unlink() + + def apply_link(self, linkpath: Path, target: Path, dry_run): + print(f"link {linkpath} -> {target}") + if not dry_run: + linkpath.symlink_to(target) + + def apply(self, dry_run=True): + for d in self.delete_todo: + self.apply_delete(d, dry_run) + + for d in self.dirs_todo: + self.apply_dir(d, dry_run) + + for link, target in self.links_todo.items(): + self.apply_link(link, target, dry_run) |