-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
executable file
·191 lines (152 loc) · 5.79 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
#! env python
from subprocess import call, check_output
from os import path
import os
import sys
import re
def get_user_input(msg, default=None, validate=None):
while True:
choice = input(msg)
# Did we pass in a default
if default:
if choice == "":
return default
# Did we ask to validate the input
if not validate:
return choice
if re.search(validate, choice):
return choice
print("Invalid entry, Try again")
def yes_no(msg, default=None):
while True:
result = get_user_input(msg, default)
if re.match("^(Y|y)$", result):
return True
if re.match("^(N|n)$", result):
return False
def rename(file, rename_list):
for old, new in rename_list.items():
# print ("Rename: '%s' '%s'" % (old, new))
if file == old:
return new
return file
def skipable(file, skip_regex):
for regex in skip_regex:
# print ("regex: '%s' '%s'" % (regex, file))
if re.search(regex, file):
return True
return False
def search(file, needle):
try:
with open(file, 'r') as haystack:
for line in haystack:
if re.search(needle, line):
return True
return False
except IOError as e:
return False
def append(target, source):
# If the target file already has the appends
if search(target, '# dev-tools'):
return
with open(source, 'r') as src:
with open(target, "a") as dest:
for line in src:
dest.write(line)
def edit(file, regex, replace):
output = []
found = False
replace = "%s\n" % replace
# Open the file and search for the regex
with open(file, 'r') as src:
for line in src:
if re.search(regex, line):
output.append(replace)
found = True
else:
output.append(line)
# If the regex was found in the file, write out the new file
if found:
with open(file, 'w') as src:
src.truncate()
for line in output:
src.write(line)
if __name__ == "__main__":
# Get my name
prog_name = sys.argv[0].lstrip("./")
# Ensure we don't get run on shared user accounts!
user = os.environ.get('USER', '')
if user != 'thrawn':
question = "\n-- Current user '%s' != 'thrawn' Continue (Y/N) ? " % user
if not yes_no(question, "Y"):
sys.exit(-1)
# Default install directory to ~/bin
home_dir = os.environ.get('HOME', '')
if home_dir == '':
print("-- Could not determine your home directory")
sys.exit(-1)
bin_path = os.path.join(home_dir, "bin")
# If the user supplied a dest directory, use that instead
if len(sys.argv) > 1:
print("-- Installing into %s" % sys.argv[1])
bin_path = sys.argv[1]
if not path.exists(bin_path):
question = "\n-- '%s' doesn't exist, create it (Y/N) ? " % bin_path
if not yes_no(question, "Y"):
sys.exit(-1)
os.makedirs(bin_path)
# Get a listing of all the programs in the bin/ directory
list = os.listdir(os.getcwd() + "/bin")
for file_name in list:
# Skip some files
if skipable(file_name, ['.swp', '^\.']):
continue
cmd = "ln -s %s/%s %s/%s" % (os.getcwd() + "/bin",
file_name, bin_path, file_name)
print(" -- ", cmd)
call(cmd, shell=True)
# Copy the dotfiles
cwd = os.getcwd()
call('mkdir -p ~/.vim', shell=True)
call('mkdir -p ~/.vimswap', shell=True)
call('cd vim; tar -vcf - . | $( cd ~/.vim; tar -vxf - )', shell=True)
call('ln -s %s/.vimrc ~/.vimrc' % cwd, shell=True)
call('ln -s %s/.gvimrc ~/.gvimrc' % cwd, shell=True)
# SSH
call('mkdir -p ~/.ssh', shell=True)
call('cd ssh; cp config ~/.ssh', shell=True)
call('chmod u+rwx,go-rwx ~/.ssh', shell=True)
if path.exists("/Library"):
call('chown -R %s:staff ~/.ssh' % user, shell=True)
else:
call('chown %s.%s -R ~/.ssh' % (user, user), shell=True)
call('cp .bash_profile ~/.bash_profile', shell=True)
call('cp .bash_prompt ~/.bash_prompt', shell=True)
call('git config --global color.diff auto', shell=True)
call('git config --global color.status auto', shell=True)
call('git config --global color.branch auto', shell=True)
call('git config --global user.name "Derrick J. Wippler"', shell=True)
call('git config --global user.email [email protected]', shell=True)
call('git config --global push.default current', shell=True)
# Add this so go mod will pull without `unknown revision` errors
call('git config --global url."[email protected]:".insteadOf https://github.com/', shell=True)
bashprompt = os.path.join(home_dir, ".bash_prompt")
bashrc = os.path.join(home_dir, ".bash_profile")
# Setup .bashrc
if path.exists("/Library"):
# OSX
os.chdir(check_output(["git", "--exec-path"]).rstrip(b'\n'))
os.system('sudo ln -s %s/bin/git-* .' % home_dir)
os.chdir(cwd)
print("--- OSX only ---")
print("-- brew install coreutils && brew install git && brew doctor")
print("-- Fix the paths by modifying /etc/paths")
# print("\n\n")
# print("Choose a color for the bash prompt hostname")
# print(" 1 = Red, 2 = Green, 3 = Yellow, 4 = Blue ")
# print(" 5 = Pink, 6 = Cyan, 7 = White, 8 = Black ")
# color = get_user_input("Color (default=4) > ", '3', '^\d$')
# edit(bashprompt, "^hostStyle=", "hostStyle=\"\e[1;3%sm\";" % color)
# print("Choose a name to report in iterm tabs")
# tab = get_user_input("Tab (default=\h) > ", '\h')
# edit(bashrc, "^title=", "title='\\033]0;%s\\007'" % tab)