-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstow.py
67 lines (54 loc) · 2.24 KB
/
stow.py
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
60
61
62
63
64
65
66
67
import os
import subprocess
from dotbot import Plugin
class Stow(Plugin):
_directive = "stow"
def can_handle(self, directive):
return directive == self._directive
def handle(self, directive, data):
if directive != self._directive:
raise ValueError("Stow cannot handle directive %s" % directive)
return self._process(data)
def _process(self, packages):
success = True
defaults = self._context.defaults().get("stow", {})
if isinstance(packages, str):
options = defaults.copy()
options["package"] = packages
success = self._stow(**options)
elif isinstance(packages, list):
for package in packages:
options = defaults.copy()
options["package"] = package
success = self._stow(**options) and success
elif isinstance(packages, dict):
for package, value in packages.items():
options = defaults.copy()
if isinstance(value, dict):
options["package"] = package
options.update(value)
else:
options.update({"package": package, "target": value})
success = self._stow(**options) and success
if success:
self._log.info("All packages have been stowed")
else:
self._log.error("Some packages were not successfully stowed")
return success
def _stow(self, package=".", target=None, restow=True, adopt=False, **kwargs):
options = [
"--dir={}".format(self._context.base_directory()),
"--target={}".format(os.path.expandvars(os.path.expanduser(target)))
if target
else None,
"--restow" if restow else "--stow",
"--adopt" if adopt else None,
]
for ptn_option in (o for o in ("ignore", "defer", "override") if o in kwargs):
ptns = kwargs.get(ptn_option)
if not isinstance(ptns, list):
ptns = [ptns]
for ptn in ptns:
options.append("--{}={}".format(ptn_option, ptn))
cmd = ["stow"] + [o for o in options if o] + [package]
return subprocess.call(cmd) == 0