summaryrefslogtreecommitdiff
path: root/apply/state.py
diff options
context:
space:
mode:
Diffstat (limited to 'apply/state.py')
-rw-r--r--apply/state.py55
1 files changed, 49 insertions, 6 deletions
diff --git a/apply/state.py b/apply/state.py
index 1c05633..9dafe9d 100644
--- a/apply/state.py
+++ b/apply/state.py
@@ -4,6 +4,8 @@ from enum import Enum
import hashlib
import os
+from .writer import Writer
+
def add_or_create(dictio, key, value):
if key in dictio:
@@ -66,7 +68,8 @@ class FileState(Enum):
class StateFile:
links = {}
dirs = {}
- attr_to_save = ["links", "applydir", "dirs"]
+ saved = []
+ attr_to_save = ["links", "applydir", "dirs", "saved"]
def __init__(self, applydir):
# Generate unique string for each possible applydir
@@ -86,6 +89,7 @@ class StateFile:
self.links = state.get("links", {})
self.dirs = state.get("dirs", {})
self.applydir = state.get("applydir", self.applydir)
+ self.saved = state.get("saved", [])
def save_to_dict(self):
all_attr = self.__dict__
@@ -94,13 +98,52 @@ class StateFile:
res[key] = all_attr[key]
return res
- def dump_state(self):
+ def dump_state(self, dry_run):
+ if dry_run:
+ return
with self.statefile.open("w") as f:
json.dump(self.save_to_dict(), f)
- def add_dir(self, path: Path, packagename: str):
+ def add_dir(self, path: Path, modulename: str):
# Add to state
- add_or_create(self.dirs, str(path), packagename)
+ add_or_create(self.dirs, str(path), modulename)
+
+ def add_link(self, dest, modulename):
+ self.links[str(dest)] = modulename
+
+ def add_saved_module(self, modulename):
+ if modulename not in self.saved:
+ self.saved.append(modulename)
+
+ def is_saved(self, modulename):
+ return modulename in self.saved
+
+ def remove_saved_module(self, modulename):
+ if modulename in self.saved:
+ self.saved.remove(modulename)
+
+ def __delete_dir(self, writer: Writer, path: str, module):
+ self.dirs[path].remove(module)
+
+ if len(self.dirs[path]) == 0:
+ writer.remove(Path(path))
+
+ def __delete_link(self, writer: Writer, path: str):
+ del self.links[path]
+
+ if FileState.check_location(Path(path)) == FileState.Owned:
+ writer.delete(Path(path))
+
+ def remove_by_condition(self, cond, writer: Writer):
+ writer.attach_hook(self.dump_state)
+ links = list(self.links.keys())
+ for link in links:
+ if cond(self.links[link]):
+ self.__delete_link(writer, link)
- def add_link(self, dest, packagename):
- self.links[str(dest)] = packagename
+ dirs = list(self.dirs.keys())
+ for directory in dirs:
+ modules = self.dirs[directory]
+ for module in modules:
+ if cond(module):
+ self.__delete_dir(writer, directory, module)