-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·208 lines (159 loc) · 5.51 KB
/
setup.py
File metadata and controls
executable file
·208 lines (159 loc) · 5.51 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
#!/usr/bin/env python
""" setup script """
import argparse
import inspect
import os
import sys
def try_link(src, dst):
""" link src to dst and print msg by caller name """
caller = inspect.stack()[1][3]
try:
os.symlink(src, dst)
print "[Ok]%s : link '%s' to '%s'" % (caller, src, dst)
except OSError as exp:
print "[Skip]%s : link '%s' to '%s' (%s)" % (caller, src, dst, exp.strerror)
def setup_bash():
""" setup bash """
selfname = inspect.stack()[0][3]
cur = os.getcwd()
home = os.getenv("HOME")
cmd = """
if [ -e $HOME/.bashrc.private ]; then
. $HOME/.bashrc.private
fi
"""
if os.path.exists(home + "/.bashrc"):
for line in open(home + "/.bashrc"):
if line.find("$HOME/.bashrc.private") >= 0:
print "[OK]%s : .bashrc has source .bashrc.private" % (selfname)
break
else:
with open(home + "/.bashrc", "a+") as outfile:
print >> outfile, cmd
print "[OK]%s : insert source .bashrc.private to ~/.bashrc" % (selfname)
try_link(cur + "/share/bash/.bashrc.private", home + "/.bashrc.private")
elif os.path.exists(home + "/.profile"):
for line in open(home + "/.profile"):
if line.find("$HOME/.bashrc.private") >= 0:
print "[OK]%s : .profile has source .bashrc.private" % (selfname)
break
else:
outfile = open(home + "/.profile", "a+")
print outfile, cmd
outfile.close()
print "[OK]%s : insernt source .bashrc.private to ~/.profile" % (selfname)
try_link(cur + "/share/bash/.bashrc.private", home + "/.bashrc.private")
else:
print "[ERROR]%s : can't find '.profile' or '.bashrc.private'" % (selfname)
return
def setup_xtool():
""" setup xtool """
arch_list = ["arm", "aarch64"]
tool_list = [
"addr2line",
"ar",
"as",
"c++",
"c++filt",
"cpp",
"g++",
"gcc",
"gdb",
"gccbug",
"gcov",
"ld",
"nm",
"objcopy",
"objdump",
"ranlib",
"readelf",
"size",
"strings",
"strip",
]
cur = os.getcwd()
home = os.getenv("HOME")
for arch in arch_list:
for item in tool_list:
try_link(cur + "/bin/armbox", home + "/tool/bin/" + arch + "-" + item)
try_link(cur + "/bin/dis", home + "/tool/bin/dis")
try_link(cur + "/bin/config-query", home + "/tool/bin/config-query")
try_link(cur + "/bin/xtools-setup", home + "/tool/bin/xtools-setup")
def setup_python():
""" setup python """
cur = os.getcwd()
home = os.getenv("HOME")
try_link(cur + "/share/python/.pylintrc", home + "/.pylintrc")
def setup_vim():
""" setup vim """
cur = os.getcwd()
home = os.getenv("HOME")
try_link(cur + "/share/vim/.vimrc", home + "/.vimrc")
try_link(cur + "/share/vim/.vim", home + "/.vim")
def setup_git():
""" setup git """
cur = os.getcwd()
home = os.getenv("HOME")
try_link(cur + "/share/git/.gitconfig", home + "/.gitconfig")
try_link(cur + "/share/git/.gitignore", home + "/.gitignore")
def setup_gdb():
""" setup gdb """
cur = os.getcwd()
home = os.getenv("HOME")
try_link(cur + "/share/gdb/.gdbinit", home + "/.gdbinit")
try_link(cur + "/share/gdb/.gdbscript", home + "/.gdbscript")
def setup_misc():
""" setup misc """
cur = os.getcwd()
home = os.getenv("HOME")
try_link(cur + "/bin/suspend", home + "/tool/bin/")
config_path = home + "/.config/gtk-3.0/"
if not os.path.exists(config_path):
os.mkdir(config_path)
try_link(cur + "/share/ubuntu/gtk.css", config_path)
def setup_all(tools):
"""do all task. """
for key, func in tools.items():
print "setup '%s'" % (key)
func()
def get_choice(max_choice):
" Get integer input from prompt. 'q' for leave "
while True:
try:
choice = raw_input("Choice [q for quit] : ")
if choice == 'q':
choice = 0
break
elif choice.isalnum() and 1 <= int(choice) <= max_choice:
break
else:
pass
except ValueError:
pass
return int(choice)
def parse_argv():
""" parse command line option """
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
description=("Setup our Ubuntu working environment\n"
"Please execute the '%s' in this directory\n") % sys.argv[0])
return parser.parse_args()
def run():
""" main function """
tool_name = ["bash", "xtool", "python", "vim", "git", "gdb", "misc"]
tool_callback = [setup_bash, setup_xtool, setup_python,
setup_vim, setup_git, setup_gdb, setup_misc]
tools = dict(zip(tool_name, tool_callback))
for idx, item in enumerate(tools.keys()):
print "%-2d : %-10s" % (idx + 1, item)
print "%-2d : %-10s" % (len(tools.keys()) + 1, "all")
choice = get_choice(len(tools) + 1)
if choice >= 0 and choice <= len(tool_name):
tools[tools.keys()[choice - 1]]()
elif choice == len(tool_name) + 1:
setup_all(tools)
else:
print "Do nothing"
sys.exit(0)
if __name__ == "__main__":
parse_argv()
run()