-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathbootstrap.py
More file actions
executable file
·373 lines (322 loc) · 12 KB
/
bootstrap.py
File metadata and controls
executable file
·373 lines (322 loc) · 12 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
"""Bootstrap script for dotfiles setup and configuration."""
import argparse
import logging
import os
import platform
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Optional
FORMAT = "%(message)s"
try:
from rich.logging import RichHandler
logging.basicConfig(
level=logging.INFO, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)
except ImportError:
logging.basicConfig(
level=logging.INFO, format=FORMAT, datefmt="[%X]", stream=sys.stdout
)
logger = logging.getLogger(__name__)
class DotfilesBootstrap:
"""Handles dotfiles synchronization and system configuration."""
def __init__(self):
self.dotfiles_dir = Path(__file__).parent.resolve()
self.home_dir = Path.home()
def update(self) -> None:
"""Update the dotfiles repository from origin."""
logger.info("Updating")
subprocess.run(
["git", "pull", "origin", "main"], cwd=self.dotfiles_dir, check=True
)
def git_config(self) -> None:
"""Configure Git settings."""
logger.info("Configuring Git")
# Configure global .gitignore
logger.info("Configuring global .gitignore")
gitignore_global = self.home_dir / ".gitignore_global"
subprocess.run(
["git", "config", "--global", "core.excludesfile", str(gitignore_global)],
check=True,
)
# Configure Araxis Merge if available
araxis_path = Path("/Applications/Araxis Merge.app/Contents/Utilities/")
if araxis_path.exists():
logger.info("Configuring Araxis Merge")
subprocess.run(
["git", "config", "--global", "diff.guitool", "araxis"], check=True
)
subprocess.run(
["git", "config", "--global", "merge.guitool", "araxis"], check=True
)
subprocess.run(
[
"git",
"config",
"--global",
"mergetool.araxis.path",
"/Applications/Araxis Merge.app/Contents/Utilities/compare",
],
check=True,
)
# Configure Sublime Merge if available
smerge_path = Path(
"/Applications/Sublime Merge.app/Contents/SharedSupport/bin/"
)
if smerge_path.exists():
logger.info("Configuring Sublime Merge")
smerge_cmd = (
"/Applications/Sublime\\ Merge.app/Contents/SharedSupport/bin/smerge "
'mergetool "$BASE" "$LOCAL" "$REMOTE" -o "$MERGED"'
)
subprocess.run(
["git", "config", "--global", "mergetool.smerge.cmd", smerge_cmd],
check=True,
)
subprocess.run(
["git", "config", "--global", "mergetool.smerge.trustExitCode", "true"],
check=True,
)
subprocess.run(
["git", "config", "--global", "merge.guitool", "smerge"], check=True
)
# Configure delta if available
delta_path = shutil.which("delta")
if delta_path:
logger.info("Configuring delta")
subprocess.run(
["git", "config", "--global", "core.pager", "delta"], check=True
)
subprocess.run(
[
"git",
"config",
"--global",
"interactive.diffFilter",
"delta --color-only",
],
check=True,
)
def tool_config(self) -> None:
"""Configure development tools."""
logger.info("Configuring Tools")
# Configure fish theme if available
fish_path = shutil.which("fish")
if fish_path:
logger.info("Configuring fish theme")
solarized_script = self.dotfiles_dir / ".config" / "fish" / "solarized.fish"
subprocess.run([fish_path, str(solarized_script)], check=True)
def sync(self) -> None:
"""Synchronize dotfiles to home directory."""
logger.info("Syncing")
# Rsync dotfiles to home
exclude_patterns = [
".git/",
".gitignore",
"Preferences.sublime-settings",
"README.md",
"bootstrap.sh",
"bootstrap.py",
"installers/",
"os/",
"scripts/",
"sublime/",
"tmux.terminfo",
]
rsync_cmd = ["rsync"]
for pattern in exclude_patterns:
rsync_cmd.extend(["--exclude", pattern])
rsync_cmd.extend(
[
"--filter=:- .gitignore",
"--no-perms",
"-avh",
f"{self.dotfiles_dir}/",
str(self.home_dir),
]
)
subprocess.run(rsync_cmd, check=True)
# Touch .localrc so fish can source it
localrc = self.home_dir / ".localrc"
localrc.touch(exist_ok=True)
# Copy Sublime configurations
sublime_merge_dir = (
self.home_dir / "Library" / "Application Support" / "Sublime Merge"
)
if sublime_merge_dir.exists():
sublime_merge_src = self.dotfiles_dir / "sublime" / "merge"
sublime_merge_dst = sublime_merge_dir / "Packages" / "User"
if sublime_merge_src.exists():
subprocess.run(
["rsync", "-a", f"{sublime_merge_src}/", str(sublime_merge_dst)],
check=True,
)
sublime_text_dir = (
self.home_dir / "Library" / "Application Support" / "Sublime Text"
)
if sublime_text_dir.exists():
sublime_text_src = self.dotfiles_dir / "sublime" / "text"
sublime_text_dst = sublime_text_dir / "Packages" / "User"
if sublime_text_src.exists():
subprocess.run(
["rsync", "-a", f"{sublime_text_src}/", str(sublime_text_dst)],
check=True,
)
def directories(self) -> None:
"""Create necessary directories."""
(self.home_dir / "vim" / "undo").mkdir(parents=True, exist_ok=True)
(self.home_dir / ".ssh").mkdir(parents=True, exist_ok=True)
(self.home_dir / ".gnupg").mkdir(parents=True, exist_ok=True)
def install(self) -> None:
"""Install and update software."""
# Install neovim plugins
if shutil.which("nvim"):
logger.info("Installing neovim plugins")
subprocess.run(["nvim", "--headless", "+Lazy! sync", "+qa"], check=False)
if shutil.which("pip3"):
subprocess.run(["pip3", "install", "neovim"], check=False)
# Install/update tmux plugins
tpm_dir = self.home_dir / ".tmux" / "plugins" / "tpm"
if not tpm_dir.exists():
logger.info("Installing tmux plugins")
subprocess.run(
["git", "clone", "https://github.com/tmux-plugins/tpm", str(tpm_dir)],
check=True,
)
subprocess.run([str(tpm_dir / "bin" / "install_plugins")], check=True)
else:
logger.info("Updating tmux plugins")
subprocess.run([str(tpm_dir / "bin" / "update_plugins"), "all"], check=True)
# Install/update Rust
if shutil.which("rustup"):
logger.info("Updating Rust")
subprocess.run(["rustup", "update"], check=True)
else:
logger.info("Installing Rust")
rustup_script = Path("/tmp/rustup-init.sh")
subprocess.run(
[
"curl",
"--proto",
"=https",
"--tlsv1.2",
"-sSf",
"https://sh.rustup.rs",
"-o",
str(rustup_script),
],
check=True,
)
rustup_script.chmod(0o755)
subprocess.run([str(rustup_script), "-y"], check=True)
# Update Homebrew
if shutil.which("brew"):
logger.info("Updating Homebrew")
subprocess.run(["brew", "update"], check=True)
subprocess.run(["brew", "upgrade"], check=True)
def permissions(self) -> None:
"""Fix file permissions for sensitive directories."""
logger.info("Fixing Permissions")
# Fix .ssh permissions
ssh_dir = self.home_dir / ".ssh"
if ssh_dir.exists():
subprocess.run(["chown", "-R", os.getlogin(), str(ssh_dir)], check=True)
# Set directory permissions to 700
for item in ssh_dir.rglob("*"):
if item.is_dir():
item.chmod(0o700)
# Set private key permissions to 600
for item in ssh_dir.glob("id_rsa*"):
if item.is_file():
item.chmod(0o600)
# Set public key permissions to 644
for item in ssh_dir.glob("*.pub"):
if item.is_file():
item.chmod(0o644)
# Fix .gnupg permissions
gnupg_dir = self.home_dir / ".gnupg"
if gnupg_dir.exists():
subprocess.run(["chown", "-R", os.getlogin(), str(gnupg_dir)], check=True)
for item in gnupg_dir.rglob("*"):
if item.is_file():
item.chmod(0o600)
elif item.is_dir():
item.chmod(0o700)
def os_config(self) -> None:
"""Configure OS-specific settings."""
logger.info("Configuring OS")
system = platform.system()
if system == "Darwin":
logger.info("Configuring macOS")
macos_script = self.dotfiles_dir / "os" / "macos.py"
if macos_script.exists():
subprocess.run([str(macos_script)], check=True)
elif system == "Linux":
logger.info("Configuring Linux")
linux_script = self.dotfiles_dir / "os" / "linux.sh"
if linux_script.exists():
subprocess.run([str(linux_script)], check=True)
def do_sync_operation(self) -> None:
"""Perform sync operation with related configurations."""
self.sync()
self.git_config()
self.tool_config()
self.directories()
self.permissions()
def do_all(self) -> None:
"""Perform all operations."""
self.update()
self.sync()
self.git_config()
self.directories()
self.permissions()
self.install()
self.os_config()
def main() -> int:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Bootstrap script for dotfiles setup and configuration.",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-s",
"--sync",
action="store_true",
help="Synchronizes dotfiles to home directory",
)
parser.add_argument(
"-i", "--install", action="store_true", help="Install (extra) software"
)
parser.add_argument(
"-c", "--config", action="store_true", help="Configures your system"
)
parser.add_argument(
"-a", "--all", action="store_true", help="Does all of the above"
)
args = parser.parse_args()
# If no arguments provided, show help.
if not any([args.sync, args.install, args.config, args.all]):
parser.print_help()
return os.EX_USAGE
bootstrap = DotfilesBootstrap()
try:
if args.all:
bootstrap.do_all()
else:
if args.sync:
bootstrap.do_sync_operation()
if args.install:
bootstrap.install()
if args.config:
bootstrap.os_config()
except subprocess.CalledProcessError as e:
logger.error(f"Command failed with exit code {e.returncode}")
return os.EX_SOFTWARE
except Exception as e:
logger.error(f"Error: {e}")
return os.EX_SOFTWARE
return os.EX_OK
if __name__ == "__main__":
sys.exit(main())