-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
executable file
·237 lines (203 loc) · 7.43 KB
/
sync.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env -S uv run
from __future__ import annotations
import hashlib
import shlex
import shutil
import stat
import subprocess
from dataclasses import dataclass
from pathlib import Path
from urllib.request import urlopen
@dataclass(frozen=True)
class Symlink:
path: Path
target: str | Path
def create(self, force: bool) -> None:
print(f"creating {self.path} -> {self.target}")
target = Path(self.target).resolve()
if force:
self.path.unlink(missing_ok=True)
if self.path.exists():
if self.path.resolve() != target:
print(f" ERROR: {self.path} exists")
else:
self.path.parent.mkdir(parents=True, exist_ok=True)
self.path.symlink_to(target)
@dataclass(frozen=True)
class RemoteFile:
url: str
local_path: Path
def download(self, force: bool) -> None:
print(f"fetching {self.local_path}")
if self.local_path.exists() and not force:
print(" already exists")
else:
self.local_path.parent.mkdir(parents=True, exist_ok=True)
with urlopen(self.url) as response:
with self.local_path.open("wb") as local:
shutil.copyfileobj(response, local)
@dataclass(frozen=True)
class NvimApp:
local_dir: Path
image_url: str = (
"https://github.com/neovim/neovim/releases/download/stable/nvim.appimage"
)
sha256_url: str = "https://github.com/neovim/neovim/releases/download/stable/nvim.appimage.sha256sum"
@property
def _appimage_path(self) -> Path:
return self.local_dir / "nvim.appimage"
@property
def _appimage(self) -> RemoteFile:
return RemoteFile(
url=self.image_url,
local_path=self._appimage_path,
)
@property
def _sha256_path(self) -> Path:
return self.local_dir / "nvim.appimage.sha256sum"
@property
def _sha256(self) -> RemoteFile:
return RemoteFile(
url=self.sha256_url,
local_path=self._sha256_path,
)
def download(self, force: bool) -> Path:
"""Fetch nvim app, return path of executable."""
print("Downloading nvim...")
self.local_dir.mkdir(parents=True, exist_ok=True)
self._appimage.download(force)
self._sha256.download(force)
sha256_expected = self._sha256_path.read_text().split()[0]
sha256 = hashlib.sha256()
sha256.update(self._appimage_path.read_bytes())
if sha256.hexdigest() != sha256_expected:
raise RuntimeError("sha256 of nvim doesn't match")
stats = self._appimage_path.stat()
self._appimage_path.chmod(stats.st_mode | stat.S_IXUSR)
bare_img = subprocess.run(
args=[f"{self._appimage_path}", "--version"],
capture_output=True,
)
if bare_img.returncode == 0:
return self._appimage_path
print(" extracting image...")
_ = subprocess.run(
args=[f"./{self._appimage_path.name}", "--appimage-extract"],
check=True,
capture_output=True,
cwd=self.local_dir,
)
return self.local_dir / "squashfs-root/usr/bin/nvim"
@dataclass(frozen=True)
class FileInGitRepo:
url: str
local_clone: Path
file_in_clone: str | Path
substitute: Path | None = None
@property
def _fetched_path(self) -> Path:
return self.local_clone / self.file_in_clone
def fetch(self) -> Path:
print(f"fetching {self._fetched_path}")
if self.substitute is not None and self.substitute.exists():
print(f" using {self.substitute} instead")
return self.substitute
if self.local_clone.exists():
print(" updating local clone")
_ = subprocess.run(
args=shlex.split("git pull"),
cwd=self.local_clone,
)
else:
print(" cloning repository")
_ = subprocess.run(
args=shlex.split(
f"git clone --depth=1 '{self.url}' '{self.local_clone}'"
),
)
if self._fetched_path.exists():
return self._fetched_path
raise RuntimeError(f"{self._fetched_path} does not exist")
@dataclass(frozen=True)
class PyTool:
name: str
with_deps: tuple[str, ...] = ()
def install(self) -> None:
print(f"installing {self.name} with uv")
cmd = ["uv", "tool", "install", "--upgrade", self.name]
for dep in self.with_deps:
cmd.extend(("--with", dep))
_ = subprocess.run(cmd)
if __name__ == "__main__":
import argparse
@dataclass
class Args:
force: bool = False
parser = argparse.ArgumentParser()
_ = parser.add_argument(
"--force", "-f", action="store_true", help="replace existing files"
)
args = parser.parse_args(namespace=Args())
home = Path.home()
config = home / ".config"
remotes = [
RemoteFile(
url="https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim",
local_path=home / ".vim/autoload/plug.vim",
),
]
for remote in remotes:
remote.download(force=args.force)
links = [
# terminal
Symlink(path=home / ".alias", target="alias"),
Symlink(path=home / ".tmux.conf", target="tmux.conf"),
Symlink(path=config / "wezterm/wezterm.lua", target="wezterm.lua"),
# bash
Symlink(path=home / ".bashrc", target="bashrc"),
Symlink(path=config / "starship.toml", target="starship.toml"),
# latex
Symlink(path=home / ".latexmkrc", target="latexmkrc"),
# nvim
Symlink(path=config / "nvim/init.lua", target="nvim.lua"),
Symlink(path=config / "nvim/lua/plugins.lua", target="nvim_plugins.lua"),
Symlink(path=config / "nvim/snippets/_.snippets", target="nvim.snippets"),
Symlink(path=home / ".vimrc", target="vimrc"),
# zsh
Symlink(path=home / ".zshrc", target="zshrc"),
Symlink(path=home / ".p10k.zsh", target="p10k.zsh"), # config of p10k
*(
Symlink(path=home / ".zfunc" / tgt.name, target=tgt)
for tgt in Path("zfunc").iterdir()
),
]
zsh_gits = Path(".zsh_gits")
p10k_theme = FileInGitRepo(
url="https://github.com/romkatv/powerlevel10k.git",
local_clone=zsh_gits / "powerlevel10k",
file_in_clone="powerlevel10k.zsh-theme",
substitute=Path("/usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme"),
)
links.append(Symlink(path=home / ".p10ktheme", target=p10k_theme.fetch()))
zsh_hl = FileInGitRepo(
url="https://github.com/zsh-users/zsh-syntax-highlighting.git",
local_clone=zsh_gits / "zsh-syntax-highlighting",
file_in_clone="zsh-syntax-highlighting.zsh",
substitute=Path(
"/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
),
)
links.append(Symlink(path=home / ".zshsynthl", target=zsh_hl.fetch()))
py_packs = [
PyTool("basedpyright"),
PyTool("ruff"),
PyTool("fortls"),
]
if shutil.which("nvim") is None:
nvim = NvimApp(local_dir=Path(".nvim")).download(args.force)
links.append(Symlink(path=home / ".local/bin/nvim", target=nvim))
for link in links:
link.create(force=args.force)
# could try to install uv if not present
for pack in py_packs:
pack.install()