diff options
author | Julian T <julian@jtle.dk> | 2021-04-24 20:28:44 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-04-24 20:32:37 +0200 |
commit | 0c24ead1cb0126a1847c2ed971649e9ee25e920e (patch) | |
tree | 20a9e5e791b72696f3784fa4580530a5f3d16810 | |
parent | 62d63afb0783b42f9f6bdbecc81b701b9d629093 (diff) |
Add status subcommand
-rw-r--r-- | apply/__main__.py | 3 | ||||
-rwxr-xr-x | apply/apply.py | 1 | ||||
-rw-r--r-- | apply/resolv.py | 16 | ||||
-rw-r--r-- | apply/state.py | 29 | ||||
-rw-r--r-- | apply/status.py | 24 | ||||
-rw-r--r-- | shell.nix | 2 |
6 files changed, 58 insertions, 17 deletions
diff --git a/apply/__main__.py b/apply/__main__.py index 0612cdf..4f7f6a4 100644 --- a/apply/__main__.py +++ b/apply/__main__.py @@ -2,6 +2,7 @@ import argparse import yaml from . import apply as apply_cmd +from . import status def parse_config(path): @@ -12,7 +13,7 @@ def parse_config(path): return config -sub_cmds = {"apply": apply_cmd} +sub_cmds = {"apply": apply_cmd, "status": status} parser = argparse.ArgumentParser() parser.add_argument("--apply-dir", "-a", default=None, diff --git a/apply/apply.py b/apply/apply.py index 8042a1d..974d800 100755 --- a/apply/apply.py +++ b/apply/apply.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -import yaml import os from pathlib import Path import argparse diff --git a/apply/resolv.py b/apply/resolv.py index eba5ed1..93d0566 100644 --- a/apply/resolv.py +++ b/apply/resolv.py @@ -1,7 +1,6 @@ from pathlib import Path from .writer import Writer from .state import StateFile, FileState -import os class Resolver: @@ -16,17 +15,6 @@ class Resolver: self.override = override self.state = state - def check_location(self, path: Path) -> FileState: - if not path.exists(): - return FileState.Unused - - if path.is_symlink(): - dest = Path(os.path.realpath(str(path))) - if Path.cwd() in dest.parents: - return FileState.create_owned(dest) - - return FileState.Used - def check_parent(self, path: Path, packagename): """ Check if parents exists, and if we created them mark them @@ -46,7 +34,7 @@ class Resolver: def do_link(self, package, ppath: Path): dest = Path(self.applydir, ppath) - dest_state = self.check_location(dest) + dest_state = FileState.check_location(dest) if not self.override and not dest_state.can_write(): # Check if it's a pointer to the correct location @@ -58,7 +46,7 @@ class Resolver: self.check_parent(dest, package) target_abs = Path.cwd().joinpath(Path(package, ppath)) - if dest_state == FileState.Owned \ + if dest_state == FileState.Owned and dest_state.link_intact()\ and dest_state.links_to() == target_abs: return diff --git a/apply/state.py b/apply/state.py index 0bae459..1c05633 100644 --- a/apply/state.py +++ b/apply/state.py @@ -2,6 +2,7 @@ from pathlib import Path import json from enum import Enum import hashlib +import os def add_or_create(dictio, key, value): @@ -27,12 +28,40 @@ class FileState(Enum): return self.links_to_path + def link_intact(self) -> bool: + return Path(self.links_to()).exists() + + def check_string(self, dotdir: Path) -> str: + if self == FileState.Unused: + return "Free" + elif self == FileState.Owned: + status = "Intact" if self.link_intact() else "Broken" + return f"Owned by {self.owner(dotdir)} ({status})" + else: + return "Occupied" + + def owner(self, dotdir: Path) -> str: + to = Path(self.links_to()) + return to.relative_to(dotdir).parts[0] + @staticmethod def create_owned(links_to: Path) -> "FileState": s = FileState.Owned s.links_to_path = links_to return s + @staticmethod + def check_location(path: Path) -> "FileState": + if path.is_symlink(): + dest = Path(os.path.realpath(str(path))) + if Path.cwd() in dest.parents: + return FileState.create_owned(dest) + + if not path.exists(): + return FileState.Unused + + return FileState.Used + class StateFile: links = {} diff --git a/apply/status.py b/apply/status.py new file mode 100644 index 0000000..514252b --- /dev/null +++ b/apply/status.py @@ -0,0 +1,24 @@ +import argparse +from .state import StateFile, FileState +from pathlib import Path +from tabulate import tabulate + + +def cmd_args(_: argparse.ArgumentParser): + pass + + +def cmd_func(args, config): + state = StateFile(args.apply_dir) + rows = [] + for link in state.links: + st = FileState.check_location(Path(link)) + rows.append((link, st.check_string(Path.cwd()))) + + print(tabulate(rows, headers=["Link", "Status"]), "\n") + + rows = state.dirs.items() + print(tabulate(rows, headers=["Directory", "Owned by"])) + + +cmd_help = "Check status on owned links" @@ -2,6 +2,6 @@ pkgs.mkShell { buildInputs = with pythonPackages; [ - pyyaml + pyyaml tabulate ]; } |