-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore_config
executable file
·137 lines (117 loc) · 3.36 KB
/
restore_config
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
import argparse
import subprocess
import sys
import os
# Ignore Non-DotFiles
ignore = ["restore_config", "scripts", "README.md", "LICENSE"]
# Available applications
choices = ["alacritty", "emacs", "neovim"]
# Each Distro Can Use Multiple Package Managers For Ex. PacMan and Pip
dependencies = {
"arch": [
{
"command": ["sudo", "pacman", "-Syu"],
"alacritty": ["alacritty"],
"emacs": [
"git",
"ripgrep",
"python",
"fd",
"find",
"zstd",
"aspel",
"aspell-en",
"texlive-most",
"texlab",
"glslang",
"ccls",
"python-pipenv",
"python-pytest",
"python-black",
"python-pyflakes",
"python-isort",
"nodejs",
"npm",
"yarn",
],
"neovim": ["neovim", "ranger", "ripgrep", "xclip"],
}
]
}
def run_command(args):
process = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
while True:
output = process.stdout.read(1)
if output == "" and process.poll() is not None:
break
if output != "":
sys.stdout.write(output)
sys.stdout.flush()
process.stdout.close()
def main():
# Initialize parser
parser = argparse.ArgumentParser()
# Adding optional argument
parser.add_argument(
"-s",
"--sync",
nargs="+",
choices=choices,
required=True,
help="Synchronizes DotFiles for provided programs",
)
parser.add_argument(
"-d",
"--deps",
choices=dependencies.keys(),
help="Dependencies for Linux distributions",
)
# Read arguments from command line
args = parser.parse_args()
rcm = subprocess.run(["which", "rcup"], capture_output=True, text=True)
if rcm.stderr != "":
raise Exception(
"RCM is not available, visit https://github.com/thoughtbot/rcm"
) from None
# Run package managers
if args.deps is not None:
iterator = iter(dependencies[args.deps])
manager = next(iterator)
while True:
progs = []
for prog in args.sync:
progs = [*progs, *manager[prog]]
run_command([*manager["command"], *progs])
confirm = (
str(input("Proceed if no errors are found? (Y/n): ")).lower().strip()
)
if confirm[0] == "y":
try:
manager = next(iterator)
except:
break
# Run scripts
for prog in args.sync:
path = f"./scripts/{prog}"
if os.path.isfile(path):
run_command(["sh", path])
confirm = (
str(input("Scripts have completed installation. Proceed? (Y/n): "))
.lower()
.strip()
)
if confirm[0] != "y":
exit()
# Generate rcm command
rcm = ["rcup", "-v", "-d", os.getcwd()]
for pattern in ignore:
rcm = [*rcm, *["-x", pattern]]
for prog in args.sync:
rcm = [*rcm, *["-t", prog]]
# Link configs
run_command(rcm)
if __name__ == "__main__":
main()