summaryrefslogtreecommitdiff
path: root/apply/apply_cmd.py
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-04-25 20:12:25 +0200
committerJulian T <julian@jtle.dk>2021-04-25 20:12:25 +0200
commit7e93444f28a6790ab66fbdd8cc7eed9ca8970d48 (patch)
treefec5eb7eb328948d5c30d2e1d56e4c570d5dcc4c /apply/apply_cmd.py
parent0c24ead1cb0126a1847c2ed971649e9ee25e920e (diff)
Add kind of working delete command
Diffstat (limited to 'apply/apply_cmd.py')
-rw-r--r--apply/apply_cmd.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/apply/apply_cmd.py b/apply/apply_cmd.py
new file mode 100644
index 0000000..4cffce4
--- /dev/null
+++ b/apply/apply_cmd.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+import os
+from pathlib import Path
+import argparse
+
+from .resolv import Resolver
+from .writer import Writer
+from .state import StateFile
+
+
+class DirReader:
+ def __init__(self, config, resolv):
+ self.resolv = resolv
+ self.config = config
+
+ def read_module(self, name):
+ module_root = name
+
+ # Walk down and link dir og file
+ for root, dirs, files in os.walk(module_root):
+ rootrel = os.path.relpath(root, start=module_root)
+
+ # Check if we can just link this folder
+ if rootrel not in self.config["do_not_link"]:
+ ok = self.resolv.do_folder_link(name, Path(rootrel))
+
+ if ok:
+ # Do not go further down
+ dirs.clear()
+ continue
+
+ # Link files
+ for f in files:
+ self.resolv.do_link(name, Path(rootrel, f))
+
+
+def cmd_args(parser: argparse.ArgumentParser):
+ parser.add_argument("modules", nargs="*", help="modules to apply")
+ parser.add_argument("-a", "--reapply", help="reapply modules",
+ action="store_true")
+
+
+def cmd_func(args, config):
+ writer = Writer()
+ state = StateFile(args.apply_dir)
+ resolv = Resolver(args.apply_dir, writer, state,
+ args.override_existing)
+
+ writer.attach_hook(state.dump_state)
+
+ if args.reapply:
+ args.modules = state.saved
+
+ reader = DirReader(config, resolv)
+ for module in args.modules:
+ reader.read_module(module)
+ state.add_saved_module(module)
+
+ writer.apply(dry_run=args.dry_run)
+
+
+cmd_help = "Apply modules from current directory"