-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelink.py
executable file
·187 lines (143 loc) · 5.92 KB
/
relink.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
#!/usr/bin/env python3
import argparse
# import collections
import typing
from pathlib import Path
# tgt, src
# for o/* items order is flipped so "o/src tgt"
NOREL = 1
items = [
"O/", "~/.config/dotfiles", NOREL,
"o/zsh", "~/.config/zsh",
"~/.zshrc", "~/.config/zsh/zshrc",
"o/bash", "~/.config/bash",
"~/.bashrc", "~/.config/bash/bashrc",
"~/.bash_profile", "~/.config/bash/bash_profile",
"~/.bash_logout", "~/.config/bash/bash_logout",
"o/vim", "~/.config/nvim",
"~/.vimrc", "~/.config/nvim/init.vim",
# "~/.vim/view", "~/.local/share/nvim/view", NOREL,
"o/tmux", "~/.config/tmux",
"~/.tmux.conf", "~/.config/tmux/tmux.conf",
"o/alacritty", "~/.config/alacritty",
"~/.local/bin/alacritty_quake_toggle", "~/.config/alacritty/alacritty_quake_toggle", NOREL,
"o/kde/khotkeysrc", "~/.config/khotkeysrc",
"o/git", "~/.config/git",
"~/.gitconfig", "~/.config/git/gitconfig",
"~/.gitignore", "~/.config/git/gitignore",
"~/.tigrc", "~/.config/git/tigrc",
"~/.local/bin/lazygit", "~/go/src/github.com/jesseduffield/lazygit/lazygit", NOREL,
"o/top", "~/.config/procps",
"o/htop", "~/.config/htop",
"o/arc", "~/.config/arc",
"~/.arcconfig", "~/.config/arc/arcconfig",
"~/.arcignore", "~/.config/arc/arcignore",
"~/.arcignore.symlink", "~/.config/arc/arcignore.symlink",
"o/ruff", "~/.config/ruff",
"o/qtile", "~/.config/qtile",
]
mkdirs = [
"~/.local/share/nvim/plugged",
"~/.local/share/nvim/shada",
"~/.local/share/nvim/swap",
"~/.local/share/nvim/view",
"~/.local/share/zsh",
]
zshplugs = (
"[ ! -d $H/.local/share/zsh/plug/wakatime ] && "
"git clone https://github.com/sobolevn/wakatime-zsh-plugin.git $H/.local/share/zsh/plug/wakatime"
)
def pairopts() -> typing.Generator:
item_a = None
item_b = None
for itm in items:
if item_a is None:
item_a = itm
elif item_b is None:
item_b = itm
else:
if isinstance(itm, int):
yield (item_a, item_b, itm)
item_a = item_b = None
else:
yield (item_a, item_b, 0)
item_a = itm
item_b = None
if item_a is not None and item_b is not None:
yield (item_a, item_b, 0)
item_a = item_b = None
def relink(fix: bool = True, verbose: bool = False) -> None:
if fix:
msg = "Fix arg is not yet supported here"
raise OSError(msg)
opath = None
for tgt, src, opt in pairopts():
src, tgt = Path(src), Path(tgt)
srco, tgto = src, tgt
if tgt.parts[0] == "O":
tgt = Path().absolute().joinpath(*tgt.parts[1:])
src, tgt = tgt, src
srco, tgto = tgto, srco
opath = tgt
if tgt.parts[0] == "o":
if opath is None:
msg = f"Failed: opath is {opath}"
raise OSError(msg)
tgt = Path(opath).expanduser().joinpath(*tgt.parts[1:])
src, tgt = tgt, src
srco, tgto = tgto, srco
src = src.expanduser()
tgt = tgt.expanduser()
if not src.exists():
print(f"EE src path {src} not found")
continue
if src.is_symlink():
print(f"EE src path {src} is link, but we do not expect this")
continue
# print('src', src, 'tgt', tgt)
# print(src, tgt)
srcl = src
if opt & NOREL == 0:
try:
rel = src.relative_to(tgt.parent)
srcl = rel
except ValueError:
print(f"WW unable to compute relative path {srco} => {tgto}")
process_link(src, srcl, srco, tgt, tgto, verbose)
print("finished")
def process_link(src: Path, srcl: Path, srco: Path, tgt: Path, tgto: Path, verbose: bool) -> None:
if tgt.is_symlink():
if tgt.readlink() == srcl:
if verbose:
print(f"OK {srco} => {tgto}")
else:
if tgt.resolve() == src.resolve():
print(f"II {srco} => {tgto}")
print(" replacing symlink to samefile")
print(f" old: {tgt.readlink()}")
print(f" new: {src}")
tgt.unlink()
tgt.symlink_to(srcl)
else:
print(f"EE {srco} => {tgto}")
print(f" current symlink target : {tgt.readlink()}")
print(f" expected symlink target: {src}")
else:
if tgt.exists():
print(f"EE {srco} => {tgto}")
print(" target already exists")
else:
print(f"II {srco} => {tgto}")
if not tgt.parent.exists():
print(f" creating parents {tgt.parent}")
tgt.parent.mkdir(parents=True)
print(f" creating link {srco} => {tgto}")
tgt.symlink_to(srcl)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--fix", type=bool, default=False)
parser.add_argument("-v", "--verbose", action="store_true", default=False)
args = parser.parse_args()
return relink(fix=args.fix, verbose=args.verbose)
if __name__ == "__main__":
main()