-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.py
executable file
·137 lines (111 loc) · 3.87 KB
/
configure.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
#!/usr/bin/env python3
"""Create and remove symbolic links for configuration files."""
import shutil
import subprocess
import sys
from argparse import ArgumentParser
from pathlib import Path
HOME = Path.home()
agents = [
HOME / "conf.d" / "opt" / "dev.tshaka.remap.plist",
]
static_executables = [
("can", ["make"]),
("note", ["go", "build", "-o", "../note.exe", "."]),
]
def is_executable(path):
if path.is_dir():
# Link bash libraries in std.
if path.stem == "std":
return True
return False
elif path.stem == ".DS_Store":
return False
return True
def build_executables():
executables = HOME / "conf.d" / "bin"
for program, build in static_executables:
subprocess.run(build, check=True, cwd=executables / program)
def link_executables():
target_directory = HOME / "bin"
target_directory.mkdir(exist_ok=True)
source_directory = HOME / "conf.d" / "bin"
for source in filter(is_executable, source_directory.iterdir()):
target = target_directory / source.stem
try:
target.symlink_to(source)
except FileExistsError:
pass
def link_configuration_files():
restore_config = False
config = HOME / ".config"
config_backup = HOME / ".config~"
if config.exists():
shutil.move(config, config_backup)
restore_config = True
target_directory = HOME
source_directory = HOME / "conf.d" / "etc"
for source in source_directory.iterdir():
target = target_directory / source.stem
try:
target.symlink_to(source)
except FileExistsError:
pass
if restore_config:
for source in config_backup.iterdir():
target = config / source.stem
shutil.move(source, target)
if config_backup.is_symlink():
config_backup.unlink()
elif config_backup.is_dir():
config_backup.rmdir()
else:
raise ValueError(f"{config_backup} is not a directory or symbolic link")
def load_launch_agents():
if sys.platform == "darwin":
target_directory = HOME / "Library" / "LaunchAgents"
for agent in agents:
shutil.copy(agent, target_directory)
target = (target_directory / agent.stem).as_posix()
subprocess.run(["launchctl", "load", target], check=True)
def unlink_executables():
target_directory = HOME / "bin"
if not target_directory.exists():
return
source_directory = HOME / "conf.d" / "bin"
for source in source_directory.iterdir():
target = target_directory / source.stem
if target.exists():
target.unlink()
def unlink_configuration_files():
target_directory = HOME
source_directory = HOME / "conf.d" / "etc"
for source in source_directory.iterdir():
target = target_directory / source.stem
if target.exists():
target.unlink()
def unload_launch_agents():
if sys.platform == "darwin":
target_directory = HOME / "Library" / "LaunchAgents"
for agent in agents:
if agent.exists():
target = target_directory / agent.stem
subprocess.run(["launchctl", "unload", target.as_posix()], check=True)
target.unlink()
def main():
parser = ArgumentParser(prog="configure", description="configure current system")
parser.add_argument("-i", action="store_true", help="install configuration files")
parser.add_argument("-u", action="store_true", help="uninstall configuration files")
arguments = parser.parse_args()
if arguments.i:
build_executables()
link_executables()
link_configuration_files()
elif arguments.u:
unlink_executables()
unlink_configuration_files()
else:
parser.print_help(file=sys.stderr)
return 2
if __name__ == "__main__":
sys.exit(main())