-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsetup.py
executable file
·439 lines (367 loc) · 12 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/python
import os, sys, re
import shutil
import platform
import fileinput
from optparse import OptionParser
from xml.etree import ElementTree
from datetime import date
import urllib
import zipfile
import tarfile
import shutil
import pwd
version = '4.2'
class powertool:
# fixme!
def __init__(self, user):
self.user = user
self.home = os.getenv('HOME')
self.conf = {}
def setup_mutt(self, distrib, version, group):
name = self.conf['name']
# host = 'smtp.' + domain
now = date.today()
term = "cs%d%d" % (now.year % 100, (now.month + 1) / 2)
if self.conf.has_key('email'):
email = self.conf['email']
else:
email = self.name_to_mail(name)
if self.conf.has_key('epass'):
epass = self.conf['epass']
else:
epass = 'MW%s' % term
domain = email.split('@')[1]
maildir = 'Mail/Inbox/cur'
if not os.path.exists(self.home + '/' + maildir):
os.makedirs(self.home + '/' + maildir)
for pkg in group:
rc = '%s/.%src' % (self.home, pkg)
if os.path.exists(rc):
if not self.conf.has_key('email') and not self.conf.has_key('epass'):
continue
print 'updating %src ...' % pkg
else:
print 'generating %src ...' % pkg
if pkg == 'msmtp':
if os.path.exists(rc):
for line in fileinput.input(rc, 1):
#os.system('sed -i "s/user\s.*/user %s/" %s' % (email, rc))
#os.system('sed -i "s/from\s.*/from %s/" %s' % (email, rc))
s = line.split()
if len(s) > 0 and (s[0] == 'user' or s[0] == 'from'):
if self.conf.has_key('email'):
print line.replace(s[1], email),
else:
print line,
#os.system('sed -i "s/password\s.*/password %s/" %s' % (epass, rc))
elif len(s) > 0 and (s[0] == 'password'):
if self.conf.has_key('epass'):
print line.replace(s[1], epass),
else:
print line,
#os.system('sed -i "s/host\s.*/host smtp.%s/" %s' % (domain, rc))
elif len(s) > 0 and (s[0] == 'host'):
print line.replace(s[1], 'smtp.%s' % domain),
else:
print line,
continue
fd = open(rc, 'w+')
fd.write('defaults\n\n')
fd.write('account %s\n' % domain)
fd.write('host smtp.%s\n' % domain)
fd.write('user %s\n' % email)
fd.write('from %s\n' % email)
fd.write('password %s\n' % epass)
fd.write('auth login\n\n')
fd.write('account default: %s' % domain)
fd.close()
elif pkg == 'fetchmail':
if os.path.exists(rc):
for line in fileinput.input(rc, 1):
#os.system('sed -i "s/user\s.*/user %s/" %s' % (email, rc))
#os.system('sed -i "s/poll\s.*/poll pop.%s with protocol pop3/"' % domain)
s = line.split()
if len(s) > 0 and (s[0] == 'user'):
if self.conf.has_key('email'):
print line.replace(s[1], email),
else:
print line,
elif len(s) > 0 and (s[0] == 'poll'):
if self.conf.has_key('email'):
print line.replace(s[1], 'pop.%s' % domain),
else:
print line,
#os.system('sed -i "s/password\s.*/password %s/" %s' % (epass, rc))
elif len(s) > 0 and (s[0] == 'password'):
if self.conf.has_key('epass'):
print line.replace(s[1], epass),
else:
print line,
else:
print line,
continue
fd = open(rc, 'w+')
fd.write('set daemon 600\n')
fd.write('poll pop.%s with protocol pop3\n' % domain)
fd.write('uidl\n')
fd.write('user "%s"\n' % email)
fd.write('password "%s"\n' % epass)
fd.write('keep\n')
fd.write('mda "/usr/bin/procmail -d %T"\n')
fd.close()
elif pkg == 'procmail':
if os.path.exists(rc):
continue
fd = open(rc, 'w+')
fd.write('MAILDIR=$HOME/Mail\n')
fd.write('DEFAULT=$MAILDIR/Inbox/\n')
fd.close()
elif pkg == 'mutt':
if os.path.exists(rc):
for line in fileinput.input(rc, 1):
#os.system('sed -i "s/my_hdr\s\+From:.*/my_hdr From: %s/" %s' % (email, rc))
#os.system('sed -i "s/my_hdr\s\+Reply-To:.*/my_hdr Reply-To: %s/" %s' % (email, rc))
s = line.split()
if len(s) > 0 and (s[0] == 'my_hdr'):
if self.conf.has_key('email'):
print line.replace(s[2], email),
else:
print line,
else:
print line,
continue
fd = open(rc, 'w+')
# smtp setting
fd.write('# smtp setting\n')
fd.write('set sendmail = /usr/bin/msmtp\n')
fd.write('# set use_from = yes\n')
fd.write('# set envelope_from = yes\n')
fd.write('\n')
# general setting
fd.write('# general setting\n')
fd.write('my_hdr From: %s\n' % email)
#fd.write('my_hdr Reply-To: %s\n' % mail)
#fd.write('my_hdr Reply-To: \"%s\" <%s>\n' % (name, mail))
fd.write('\n')
fd_rc = open('app/mutt/muttrc.common')
for line in fd_rc:
fd.write(line)
fd_rc.close()
fd.close()
# signature
fd = open(self.home + '/Mail/signature', 'w+')
fd.write('Regards,\n%s\n' % name)
fd.close()
else:
print '%s not configured' % pkg
continue
os.chmod(rc, 0600)
def name_to_mail(self, name):
return name.lower().replace(' ', '.') + '@maxwit.com'
def setup_git(self, distrib, version, group):
fname = self.conf['name']
if self.conf.has_key('email'):
email = self.conf['email']
else:
email = self.name_to_mail(fname)
# fixme
#os.system('app/git/setup.sh "%s" "%s"' % (fname, email))
rc = '%s/.gitconfig' % (self.home)
fd_rc = open(rc, 'w+')
fd_rc.write('[color]\n')
fd_rc.write('\tui = auto\n')
fd_rc.write('[user]\n')
fd_rc.write('\tname = %s\n' % fname)
fd_rc.write('\temail = %s\n' % email)
fd_rc.write('[sendemail]\n')
fd_rc.write('\tsmtpserver = /usr/bin/msmtp\n')
fd_rc.write('[push]\n')
fd_rc.write('\tdefault = matching')
fd_rc.close()
def setup_vim(self, distrib, version, group):
dst = open(self.home + '/.vimrc', 'w+')
src = open('app/vim/vimrc')
for line in src:
dst.write(line)
src.close()
dst.close()
def setup_serial(self, distrib, version, group):
dst = open(self.home + '/.kermrc', 'w+')
src = open('app/kermit/kermrc')
for line in src:
dst.write(line)
src.close()
dst.close()
def extract_file(self, file, path, type):
print 'extract ' + file
if type == 'tar':
tfile = tarfile.open(file)
for tarinfo in tfile:
tfile.extract(tarinfo.name, path)
tfile.close()
elif type == 'zip':
zfile = zipfile.ZipFile(file,'r')
zfile.extractall(path)
def setup_eclipse(self, distrib, version, curr_arch):
path = '/maxwit/source'
if not os.path.exists(path):
os.mkdir(path)
path = self.home + '/build/'
if not os.path.exists(path):
os.mkdir(path)
file_name = ''
url = ''
if curr_arch == 'x86_64':
file_name = 'eclipse_64.tar.gz'
url = 'http://mirrors.ustc.edu.cn/eclipse/technology/epp/downloads/release/kepler/SR2/eclipse-jee-kepler-SR2-linux-gtk-x86_64.tar.gz'
else:
file_name = 'eclipse.tar.gz'
url = 'http://mirrors.neusoft.edu.cn/eclipse/technology/epp/downloads/release/kepler/SR2/eclipse-jee-kepler-SR2-linux-gtk.tar.gz'
if not os.path.exists('/maxwit/source/' + file_name):
urllib.urlretrieve(url, '/maxwit/source/' + file_name)
if os.path.exists(path + 'eclipse'):
shutil.rmtree(path + 'eclipse')
self.extract_file('/maxwit/source/' + file_name, path, 'tar')
fd_rc = open('/tmp/eclipse.desktop', 'w+')
fd_rc.write('[Desktop Entry]\n')
fd_rc.write('Encoding=UTF-8\n')
fd_rc.write('Name=EclipseJEE\n')
fd_rc.write('Terminal=false\n')
fd_rc.write('StartupNotify=true\n')
fd_rc.write('Type=Application\n')
fd_rc.write('Categories=Application;Development;\n')
fd_rc.write('Comment=Eclipse Integrated Development Environment\n')
fd_rc.write('Icon=' + path + 'eclipse/icon.xpm\n')
fd_rc.write('Exec=env UBUNTU_MENUPROXY= ' + path + 'eclipse/eclipse\n')
fd_rc.close()
os.system('sudo cp /tmp/eclipse.desktop /usr/share/applications/')
os.system('sudo chmod u+x /usr/share/applications/eclipse.desktop')
if not os.path.exists('/maxwit/source/PyDev203.4.1.zip'):
url = 'http://jaist.dl.sourceforge.net/project/pydev/pydev/PyDev%203.4.1/PyDev%203.4.1.zip'
urllib.urlretrieve(url, '/maxwit/source/PyDev203.4.1.zip')
self.extract_file('/maxwit/source/PyDev203.4.1.zip', path + 'eclipse', 'zip')
def traverse(self, node, path):
if not os.path.exists(path):
print 'creating "%s"' % path
os.mkdir(path)
#else:
# print "skipping \"%s\"" % path
lst = node.getchildren()
for n in lst:
self.traverse(n, path + '/' + n.attrib['name'])
# population the target directory
def populate(self):
for fn in ['home.xml', 'maxwit.xml']:
tree = ElementTree.parse('tree/'+fn)
root = tree.getroot()
top = root.attrib['name'].strip()
if top[0] == '$':
top = os.getenv(top[1:])
if top == None:
print '"%s" does NOT exists!' % top
exit()
elif top == '/maxwit':
mounted = False
fd_chk = open('/proc/mounts')
for line in fd_chk:
mount = line.split(' ')
if mount[1] == top:
mounted = True
break
fd_chk.close()
if mounted == False:
print '"%s" NOT mounted!' % top
exit()
if not os.access(top, 7):
print 'Fail to access "' + top + '", permission denied!'
exit()
readme = top + '/readme'
if not os.path.exists(readme):
fd = open(readme, 'w+')
fd.write('MaxWit PowerTool v%s\n' % version)
fd.close()
print 'Populating %s ...' % top
self.traverse(root, top)
print
def get_user_info(self):
for struct_pwd in pwd.getpwall():
if struct_pwd.pw_name == self.user:
full_name = struct_pwd.pw_gecos.split(',')[0].strip()
return full_name
def do_install(self, distrib, version, curr_arch, install_list):
upgrade = ''
install = ''
tree = ElementTree.parse(r'app/apps.xml')
root = tree.getroot()
dist_list = root.getchildren()
for dist_node in dist_list:
if dist_node.attrib['name'] == distrib:
upgrade = dist_node.attrib['upgrade']
install = dist_node.attrib['install']
if upgrade != '':
#os.system('sudo ' + upgrade)
print
if install_list[0] == 'ALL' or 'eclipse' in install_list:
self.setup_eclipse(distrib, version, curr_arch)
release_list = dist_node.getchildren()
for release in release_list:
ver = release.attrib['version']
if ver == 'all' or ver == version:
self.distri_install(curr_arch, distrib, version, install, install_list, release.getchildren())
if ver == version:
return
def distri_install(self, arch, distrib, version, install, install_list, release_list):
for pkg in install_list:
for app_node in release_list:
if arch != app_node.get('arch', arch):
continue
members = re.split('\s+', app_node.text)
if install_list[0] == 'ALL' or pkg in members:
group = app_node.get('group')
#attr_post = app_node.get('post')
print 'Installing %s:\n %s' % (group.upper(), app_node.text)
os.system('sudo ' + install + ' ' + app_node.text)
#if attr_post != None:
# os.system('cd app/%s && ./%s' % (group, attr_post))
config = getattr(self, 'setup_'+group, None)
if config != None:
print 'Configuring %s ...' % group
config(distrib, version, members)
print
def config(self, key, val):
self.conf[key] = val
def setup(self, apps):
distrib = platform.dist()[0].lower()
version = platform.dist()[1].lower()
arch = platform.processor()
self.do_install(distrib, version, arch, apps);
if __name__ == '__main__':
user = os.getenv('USER')
if user == 'root':
print 'cannot run as root!'
exit()
power = powertool(user)
parser = OptionParser()
parser.add_option('-m', '--email', dest='email',
help='E-mail Account')
parser.add_option('-p', '--epass', dest='epass',
help='E-mail Password')
parser.add_option('-v', '--version', dest='version',
default=False, action='store_true',
help='show PowerTool version and exit')
(opts, args) = parser.parse_args()
if opts.version:
print ' PowerTool v%s (MaxWit Software, http://www.maxwit.com)' % version
exit()
if opts.email != None:
power.config('email', opts.email)
if opts.epass != None:
power.config('epass', opts.epass)
if len(args) != 0:
apps = args
else:
apps = ['ALL']
power.config('name', power.get_user_info())
power.setup(apps)
power.populate()