summaryrefslogtreecommitdiff
path: root/apply/resolv.py
blob: 00732737e3e1c4f572cc82b888f63b53c4ad6965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from pathlib import Path
from .writer import Writer
from .state import StateFile, FileState


class Resolver:
    def __init__(self,
                 applydir,
                 writer: Writer,
                 state: StateFile,
                 override: bool):

        self.applydir = Path(applydir)
        self.writer = writer
        self.override = override
        self.state = state

    def check_parent(self, path: Path, modulename):
        """
        Check if parents exists, and if we created them mark them
        with module name
        """

        parent = path.parent
        exists = parent.exists()
        if (not exists) or parent in self.state.dirs:
            self.check_parent(parent, modulename)

            # Add to state
            self.state.add_dir(parent, modulename)

            if not exists:
                self.writer.create_dir(parent)

    def do_link(self, module, ppath: Path):
        dest = Path(self.applydir, ppath)
        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
            raise Exception(f"Destination {ppath} already exists")

        # Save the link in the statefile
        self.state.add_link(dest, module)

        self.check_parent(dest, module)

        target_abs = Path.cwd().joinpath(Path(module, ppath))
        if dest_state == FileState.Owned and dest_state.link_intact()\
                and dest_state.links_to() == target_abs:
            return

        if dest_state != FileState.Unused and self.override:
            self.writer.delete(dest)
        self.writer.create_link(target_abs, dest)

    def do_folder_link(self, module, ppath: Path) -> bool:
        self.do_link(module, ppath)
        return True