-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·207 lines (187 loc) · 7.37 KB
/
setup.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
#!/usr/bin/env python
import subprocess
import os
import fileinput
vimscript=False
home_dir = os.path.expanduser("~")
working_dir = os.getcwd()
nvim_install_dir = home_dir + "/bin"
nvim_conf_path = home_dir + "/.config/nvim"
if vimscript:
nvim_init_path = nvim_conf_path + "/init.vim"
nvim_init_target = working_dir + "/nvim/init.vim"
else:
nvim_init_path = nvim_conf_path
nvim_init_target = working_dir + "/nvim-lua"
vimplug_url = 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
tmux_conf_target = working_dir + "/tmux/tmux.conf"
tmux_conf_path = home_dir + "/.tmux.conf"
bashrc_path = home_dir + "/.bashrc"
zshrc_path = home_dir + "/.zshrc"
i3_config_path = home_dir + "/.config/i3/config"
i3_config_target = working_dir + "/i3/config"
def e(cmd):
if type(cmd) == type([]):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
else:
proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
return proc.stdout.read()
def version(program='git'):
try:
if program == 'git':
return e('git --version')
elif program == 'zsh':
return e('zsh --version')
elif program == 'curl':
return e('curl --version')
except:
return None
def require(program='git'):
if program == 'git':
if version('git') == None:
print("Git not found! Install it and try again.")
exit()
elif program == 'zsh':
if version('zsh') == None:
print("Zsh not found! Install it and try again.")
exit()
elif program == 'curl':
if version('curl') == None:
print("Curl not found! Install it and try again.")
exit()
def makedir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
return True
return False
def create_nvim_conf_path():
if vimscript:
print("Creating Nvim configuration path", nvim_conf_path, "...", end=' ')
if not makedir(nvim_conf_path): print("already exists")
else: print()
def create_nvim_init_symlink():
print("Creating a symbolic link", nvim_init_path, "to target", nvim_init_target, "...", end=' ')
if not os.path.isfile(nvim_init_path):
e('ln -s ' + nvim_init_target + ' ' + nvim_init_path)
print()
else:
print("already exists")
def install_nvim_appimage(install_path):
require('curl')
print("Creating nvim install path", install_path, "...", end=' ')
if not makedir(install_path): print("already exists")
print("Installing Nvim Appimage...", end=' ')
if not os.path.isfile(install_path + '/nvim.appimage'):
print()
try:
e('curl -LO https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage')
except:
print("Problem with fetching, check if curl works accordingly.")
exit()
e('mv nvim.appimage ' + install_path)
e('chmod u+x ' + install_path + '/nvim.appimage')
e('ln -s ' + install_path + '/nvim.appimage ' + install_path + '/nvim')
else:
print("already exists")
def install_vim_plug():
require('curl')
autoload_dir = nvim_conf_path + '/autoload'
vimplug_file_path = autoload_dir + '/plug.vim'
print("Fetching plug.vim at", vimplug_url, "...", end=' ')
if not os.path.isfile(vimplug_file_path):
print()
try:
e('curl -LO ' + vimplug_url)
except:
print("Problems with fetching, check if curl works accordingly.")
exit()
makedir(autoload_dir)
e('mv plug.vim ' + autoload_dir)
else:
print("already exists")
if not os.path.isfile(vimplug_file_path):
print("A problem with fetching the vim.plug file")
exit()
def install_packer():
require('git')
try:
e('git clone --depth 1 https://github.com/wbthomason/packer.nvim {}/.local/share/nvim/site/pack/packer/start/packer.nvim'.format(home_dir))
except:
print("Problems with cloning https://github.com/wbthomason/packer.nvim")
print("Please check the URL.")
def set_tmux_conf_symlink():
print("Setting tmux config file symlink", tmux_conf_path, "target to", tmux_conf_target, "...", end=' ')
if os.path.isfile(tmux_conf_path):
print("already set")
else:
e('ln -s ' + tmux_conf_target + ' ' + tmux_conf_path)
print()
def shellrc_not_set(shell='bash'):
if shell == 'bash':
with open(bashrc_path, 'r') as bashrc:
content = bashrc.readlines()
if len([line for line in content if "# toolsrc set" in line])==0: return True
elif shell == 'zsh':
with open(zshrc_path, 'r') as bashrc:
content = bashrc.readlines()
if len([line for line in content if "# toolsrc set" in line])==0: return True
return False
def set_shellrc(shell='bash'):
if shell == 'bash':
print("Setting bashrc ...", end=' ')
if shellrc_not_set('bash'):
bashrc = open(bashrc_path, "a")
bashrc.write("# toolsrc set (don't remove this comment line unless you remove all that is set by toolsrc setup script)\n")
bashrc.write("TOOLSRC_DIR=" + working_dir + "\n")
bashrc.write("for BASHFILE in $(ls $TOOLSRC_DIR/bash)\n")
bashrc.write("do\n")
bashrc.write(" source $TOOLSRC_DIR/bash/$BASHFILE\n")
bashrc.write("done\n")
bashrc.write("# end of toolsrc settings\n")
else:
print("already set")
if shell == 'zsh':
require('zsh')
print("Setting up zsh")
if not os.path.exists(home_dir + "/.oh-my-zsh"):
require('curl')
require('git')
print("Installing oh-my-zsh...")
try:
e('wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh')
e('sh install.sh')
#e(['sh','-c','"$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"'])
print("oh-my-zsh installed, zsh is set as default and will be actived next time you login")
except:
print("oh-my-zsh install failed")
else:
print("oh-my-zsh already installed, remove ~/.oh-my-zsh in order to reinstall.")
if shellrc_not_set('zsh'):
rc = open(zshrc_path, "a")
rc.write("# toolsrc set (don't remove this comment line unless you remove all that is set by toolsrc setup script)\n")
rc.write("TOOLSRC_DIR=" + working_dir + "\n")
rc.write("for SHFILE in $(ls $TOOLSRC_DIR/zsh)\n")
rc.write("do\n")
rc.write(" source $TOOLSRC_DIR/zsh/$SHFILE\n")
rc.write("done\n")
rc.write("# end of toolsrc settings\n")
else:
print("zshrc already set by toolsrc setup")
def set_i3_config_symlink():
print("Setting i3 config file symlink", i3_config_path, "target to", i3_config_target, "...", end=' ')
if os.path.isfile(i3_config_path):
print("Already set. Removing...")
e('rm ' + i3_config_path)
e('ln -s ' + i3_config_target + ' ' + i3_config_path)
print()
create_nvim_conf_path()
create_nvim_init_symlink()
install_nvim_appimage(nvim_install_dir)
if vimscript:
install_vim_plug()
else:
install_packer()
set_tmux_conf_symlink()
set_shellrc('bash')
set_shellrc('zsh')
set_i3_config_symlink()