-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.py
More file actions
222 lines (172 loc) · 6.18 KB
/
install.py
File metadata and controls
222 lines (172 loc) · 6.18 KB
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
from os import walk
import os
import stat
import time
import shutil
import json
import subprocess
import filecmp
# Global Vars
homedir = os.path.expanduser("~") + "/"
installdir = os.path.dirname(os.path.abspath(__file__)) + "/"
defaults = os.path.join(installdir, 'defaults') + "/"
templates = os.path.join(installdir, 'templates') + "/"
files = os.path.join(installdir, 'files') + "/"
# eventually should be seperate file
cd_install_string = \
'''
#! /bin/bash
#autogenerated install script
NAME={name}
DIR={dir}
FORCE={force}
INST=".le_installed"
if [ -d $DIR ]; then
cd $DIR
if [[ -a $INST && "$FORCE" = false ]] ; then
echo "$NAME already installed"
else
{cmd} && touch $INST && echo "$NAME (re)installed"
fi;
else
echo "$NAME's $DIR not found"
fi;
'''
# modifyig this argument will have no affect, its overwritten in run
to_ignore = []
old_rcs = ""
def _run_cmd(cmd, silent=False):
try:
return subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
if silent:
return None
print e.output
raise
def git_ensure(folder, url):
print _run_cmd("~/scripts/ensure_git %s %s" % (folder, url))
def cd_install(name, folder, cmd, force_install=False, count=''):
count = str(count)
if force_install:
reinstall = "true"
else:
reinstall = "false"
string = cd_install_string.format(
name=name, dir=folder, force=reinstall, cmd=cmd)
install_path = installdir + "/install_scripts/" + \
count + "_" + name + '_install.sh'
with open(install_path, 'w') as myfile:
myfile.write(string)
# added executable to file so it can be run like a scirpt
st = os.stat(install_path)
os.chmod(install_path, st.st_mode | stat.S_IEXEC)
def guarantee_folder(folder):
if folder == homedir:
return
if not os.path.exists(folder):
os.makedirs(folder)
def save_if_different(old, new):
if not os.path.exists(old):
return
if filecmp.cmp(old, new):
return
save(old)
def save(path, isFolder=False):
guarantee_folder(old_rcs)
if not os.path.exists(path):
return
print "copying existant ", path, " to " + old_rcs
name = os.path.basename(path)
print path, name
if isFolder:
shutil.copytree(path, old_rcs + "/" + name,
ignore=shutil.ignore_patterns(*to_ignore))
else:
name = name.replace(".", "")
shutil.copy(path, old_rcs + "/" + name)
def install_file(filename, file_set):
if file_set.get("ignore", False) or filename in to_ignore:
return
folder = file_set.get("install_dir", "~")
folder = folder.replace("~", homedir)
guarantee_folder(folder)
dest = folder + file_set.get("install_name", "." + filename)
src = files + filename
if file_set.get("save_original", True):
save_if_different(dest, src)
shutil.copy(src, dest)
def install_folder(foldername, folder_set):
if folder_set.get("ignore", False) or foldername in to_ignore:
return
folder = folder_set.get("install_dir", "~")
folder = folder.replace("~", homedir)
if folder_set.get("save_original", True):
save(folder, isFolder=True)
guarantee_folder(folder)
src_folder = "files/" + foldername
for filename in os.listdir(src_folder):
shutil.copy(src_folder + "/" + filename, folder + '/' + filename)
def check_bash_profile():
if not os.path.exists(homedir + ".bash_profile"):
if raw_input("You don't have a bash profile would you like to copy over the default one? (y/n)") == 'y':
shutil.copy(defaults + "bash_profile", homedir + ".bash_profile")
def check_git_config():
if None == _run_cmd("git config --global user.name", silent=True):
username = raw_input(
"No git user.NAME found: Setting it now. What would you like it set to?\n")
_run_cmd("git config --global user.name " + username)
if None == _run_cmd("git config --global user.email", silent=True):
email = raw_input(
"No git user.EMAIL found: Setting it now. What would you like it set to?\n")
_run_cmd("git config --global user.email " + email)
def check_name_file():
name_path = homedir + "/.name"
if not os.path.exists(name_path):
print "do you want to choose what appears before the ~ or allow it to be the hostname?"
if "y" == raw_input("y/n"):
name = raw_input("type the name then click enter:")
with open(name_path, "w") as myfile:
myfile.write(name)
def run():
settings = {}
files_to_install = []
folders_to_install = []
# generate time as a string that can exist as a folder name
temp_name = time.ctime()
temp_name = temp_name.replace(" ", '_')
temp_name = temp_name.replace(":", "_")
global old_rcs
old_rcs = "oldrcs/" + temp_name
check_name_file()
check_bash_profile()
check_git_config()
# load file settings
with open("install_settings.json", 'r') as jsonFile:
settings = json.loads(jsonFile.read())
global to_ignore
to_ignore = settings.get("to_ignore", [".DS_Store"])
##### Copy Files and Folder #######
# saving specific files not in ./files
for original_file in settings.get("to_save", []):
save(original_file)
# load all files and directores in ./files
for (dirpath, dirnames, filenames) in walk("./files"):
if (dirpath == "./files"):
files_to_install = filenames
folders_to_install = dirnames
for filename in files_to_install:
install_file(filename, settings.get(filename, {}))
for foldername in folders_to_install:
install_folder(foldername, settings.get(foldername, {}))
##### End Copy Files and Folder #######
##### Ensure Git / install ######
for cmds in settings.get('git_ensure', []):
git_ensure(cmds["folder"], cmds["url"])
count = 0
for cmds in settings.get("cd_install", []):
cd_install(cmds["name"], cmds["folder"], cmds["cmd"],
cmds.get('force', False), count=count)
count += 1
##### end Ensure Git / install ######
if __name__ == "__main__":
run()