-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribute_symlinks.py
executable file
·221 lines (179 loc) · 5.04 KB
/
distribute_symlinks.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p python3
# vim: ft=python
# Terminology:
# - Link name: Where the symlink is stored
# - Link target: What the symlink points to
import argparse
import os
import shutil
import subprocess
import sys
from itertools import starmap
from os.path import abspath
from pathlib import Path
ROOT = "/"
USER = "multisn8"
def destinations():
# Named the same under ~/.config as well as $repo/config
literal = [
"alacritty",
"cargo/config.toml",
"evcxr",
"godot",
"helix",
"i3",
"keepassxc",
"layaway",
"nvim",
"sway",
"swaylock",
"waybar",
"zathura",
]
# Other mappings between ~/.config and $repo/config
config = {
"gtk-3.0": "gtk",
"gtk-4.0": "gtk",
"pipewire/pipewire.conf.d": "pipewire",
}
# Configs in ~ that are under $repo/config
home_config = {
".gitignore-global": "git/gitignore-global",
".gitconfig": "git/gitconfig",
".rgignore": "ripgrep/rgignore",
".zshrc": "zsh/zshrc",
".zlogin": "zsh/zlogin",
}
# Anything else that belongs in ~ and is under $repo
home = {
".background-image": "gfx/wallpaper/current",
}
# Anything else
root = {
"/etc/nixos": "system",
}
# merging them all
config |= dict(map(lambda name: (name, name), literal))
home |= kvmap(
lambda name, target: (
Path(".config") / name,
Path("config") / target,
),
config,
)
home |= valuemap(lambda target: Path("config") / target, home_config)
all = root
# note: using Path("~") rather than Path.home()
# so expanduser() later can, well, expand it accordingly
all |= keymap(lambda name: Path("~") / name, home)
return all
def distribute_symlinks(**cfg):
for name, target in destinations().items():
install_one(name, target, **cfg)
def install_one(
name,
target,
user=USER,
root=ROOT,
only_user=False,
actually_install=False,
verbose=False,
dry_run=False,
):
name = Path(name)
target = Path(target)
repo = Path(__file__).resolve().parent.parent
if only_user and name.is_absolute():
if verbose:
print("Skipping", name)
return
name = expanduser(name, root=root, user=user)
target = abspath(repo / target)
if verbose:
print(name, "->", target)
if dry_run:
return
try:
remove(name)
name.parent.mkdir(parents=True, exist_ok=True)
if actually_install:
copy(target, name)
else:
name.symlink_to(target)
except PermissionError:
print(
f"Skipping {name} due to missing perms",
file=sys.stderr,
)
def remove(path: Path):
if not (path.exists() or path.is_symlink()):
# can't delete something that doesn't exist
return
if path.is_file() or path.is_symlink():
path.unlink()
else:
shutil.rmtree(path)
def copy(source: Path, to: Path):
if source.is_file():
shutil.copy2(source, to)
else:
shutil.copytree(source, to)
def kvmap(op, subject):
"""
Applies `op`, a callable accepting the key and value as parameters,
to the dictionary `subject`.
"""
return dict(starmap(op, subject.items()))
def keymap(op, subject):
return kvmap(lambda k, v: (op(k), v), subject)
def valuemap(op, subject):
return kvmap(lambda k, v: (k, op(v)), subject)
def expanduser(path, root=ROOT, user=USER):
return Path(root) / str(path).replace("~", f"home/{user}")
def ensure_root(msg="Must be run as root.", fail_fast=True):
if os.geteuid() != 0:
print(
msg,
file=sys.stderr,
)
if fail_fast:
sys.exit(1)
def parse_args():
parser = argparse.ArgumentParser(
description="Puts symlinks where specified. Does not take any precautions against path traversal attacks."
)
parser.add_argument("--root", action="store", default=ROOT)
parser.add_argument("--user", action="store", default=USER)
parser.add_argument(
"--only-user",
action="store_true",
help="Do not copy system files like /etc/nixos.",
)
parser.add_argument(
"--actually-install", action="store_true", help="Copy instead of symlinking."
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Print every single entry that is processed.",
)
parser.add_argument(
"-n",
"--dry-run",
action="store_true",
help="Do not actually perform any changes.",
)
return parser.parse_args()
def main():
args = parse_args()
if not args.only_user:
ensure_root(
"Should be run as root, since it also symlinks /etc/nixos. "
"Prepare for some errors (or run with --only-user)",
fail_fast=False,
)
distribute_symlinks(**vars(args))
if __name__ == "__main__":
main()