-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
247 lines (194 loc) · 6.61 KB
/
install.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
238
239
240
241
242
243
244
245
246
247
import ntpath
import os
import sys
from collections import OrderedDict
from functools import partial
from time import sleep
import urllib.request
from install.colors import print_title, ok_indent, ask, failure_indent, indent, newline
from install.fileutil import concat_path_and_normalize
if sys.version_info < (3, 4):
print('Python 2 is not supported for the install script')
exit(1)
from install import argparser
from pathlib import Path
from install.utils import symlink_file, copy_file, write_local_git_config, read_local_git_config, \
present_git_config, is_mac, check_if_already_configured, read_ssh_keys, link_zsh, is_windows, copy_default_ssh_folder
from install.xdg import XDG, load_xdg_defaults
from install.argparser import usage
SOURCE_DIR = os.getcwd()
INSTALL_TARGET = str(Path.home())
load_xdg_defaults()
xdg = XDG(INSTALL_TARGET)
def install_xdg_defaults(force=False):
print_title('XDG Defaults')
for x, y in vars(xdg).items():
if not os.path.exists(y):
os.makedirs(y)
ok_indent(f'XDG {x} folder created ({y})')
def install_xdg_config_home(force=False):
print_title('Dot files')
files = [
'zsh',
'vim',
'git'
]
for file in files:
symlink_file(
os.path.join(SOURCE_DIR, file),
os.path.join(xdg.config, file)
)
def install_zsh(force=False):
link_zsh(SOURCE_DIR)
def install_bash(force=False):
print_title('Bash')
if not ask('Do you want install bash configs?'):
return
paths = [
('bash/bashrc', '.bashrc'),
('bash/bash_profile', '.bash_profile'),
]
for s, t in paths:
copy_file(
concat_path_and_normalize(SOURCE_DIR, s),
concat_path_and_normalize(INSTALL_TARGET, t)
)
def install_git(force=False):
print_title('Git')
local_git_config = concat_path_and_normalize(xdg.config, 'git/config.local')
installed = False
if os.path.exists(local_git_config):
installed = True
email, user_name = read_local_git_config(local_git_config)
present_git_config(email, user_name)
prefix = 're' if installed else ''
if force or ask(f'Do you want to {prefix}configure git?'):
newline()
full_name = input(indent('* What\'s your full name? '))
email = input(indent('* What\'s your e-mail address? '))
newline()
write_local_git_config(local_git_config, full_name, email)
newline()
ok_indent('Git config installed')
def install_vscode(force=False):
print_title('Visual Studio Code Settings')
if not is_mac():
prefix = '~/.config'
else:
prefix = '~/Library/Application\ Support'
if check_if_already_configured(concat_path_and_normalize(prefix, 'Code/User/settings.json')):
ok_indent('Visual Studio Code Settings already installed!')
return
if not ask('Install Visual Studio Code Settings'):
return
symlink_file(
concat_path_and_normalize(SOURCE_DIR, 'vscode/settings.json'),
concat_path_and_normalize(prefix, 'Code/User/settings.json'),
)
def install_ssh_config(force=False):
ssh_config_file = os.path.join(str(Path.home()), '.ssh', 'config')
if not os.path.exists(ssh_config_file):
copy_default_ssh_folder(
SOURCE_DIR,
ssh_config_file
)
write=False
contents = ''
target_str = 'Include {}/ssh/config'.format(SOURCE_DIR)
with open(ssh_config_file, 'r') as _file:
contents = _file.read()
if target_str not in contents:
write=True
print('I should install ssh config include')
if write:
with open(ssh_config_file, 'w') as _file:
_file.write(target_str)
_file.write('\n')
_file.write(contents)
def create_code_dir(force=False):
print_title('Code directory')
k = os.path.join(str(Path.home()), 'code')
if os.path.exists(k):
ok_indent('Code already created!')
else:
os.mkdir(k)
ok_indent('Created code directory')
def install_utils(force=False):
print_title('CLI Utilities')
file_path = concat_path_and_normalize(SOURCE_DIR, 'dotbin/z.sh')
if not os.path.exists(file_path) and ask('Install Z auto jump'):
url = 'https://raw.githubusercontent.com/rupa/z/master/z.sh'
urllib.request.urlretrieve(url, file_path)
os.chmod(file_path, 0o744)
ok_indent('Z installed')
else:
ok_indent('Z already installed')
pass
def install_vim_plug(force=False):
print_title('Vim-Plug')
vim_plug_path = concat_path_and_normalize(
xdg.cache,
'vim/autoload/plug.vim'
)
if os.path.exists(vim_plug_path):
ok_indent('Vim-Plug already installed!')
return
vim_plug_addr = 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
parent_folder = ntpath.dirname(vim_plug_path)
if not os.path.exists(parent_folder):
os.makedirs(parent_folder)
urllib.request.urlretrieve(vim_plug_addr, vim_plug_path)
ok_indent('Vim-Plug installed!')
def ssh(force=False):
if not is_windows():
return
print_title('SSH')
ssh_agent_running = os.environ.get('SSH_AUTH_SOCK') or False
if ssh_agent_running:
ok_indent('SSH-Agent is running.')
else:
failure_indent('SSH-Agent is not running.')
os.path.exists('.ssh')
read_ssh_keys()
def local_files():
print_title('Local files')
files = [
(xdg.config, 'vim/vimrc.local'),
(xdg.config, 'zsh/.zshrc.local'),
]
for parent, _file in files:
p = concat_path_and_normalize(parent, _file)
if not os.path.exists(p):
with open(p, 'w'):
pass
ok_indent(f'Local file {p} created')
else:
ok_indent(f'Local file {p} already created!')
execution = OrderedDict([
('xdg-defaults', install_xdg_defaults),
('dot-files', install_xdg_config_home),
('zsh', install_zsh),
('git', install_git),
('bash', install_bash),
('vim-plug', install_vim_plug),
('vscode', install_vscode),
('code', create_code_dir),
('ssh', ssh),
('ssh-config', install_ssh_config),
('local', local_files),
('utils', install_utils),
])
_usage = partial(usage, execution)
def execute_all_steps():
for step in execution.values():
step()
sleep(0.3)
def main(command: str):
if command == 'all':
execute_all_steps()
elif command in execution.keys():
execution[command](True)
else:
print(_usage(command))
if __name__ == '__main__':
main(**vars(argparser.get_arg_parser(execution)))