-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.py
executable file
·282 lines (202 loc) · 7.69 KB
/
uninstall.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
#!/usr/bin/env python
# Copyright 2008 David Selby dave6502@googlemail.com
# This file is part of kmotion.
# kmotion is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# kmotion is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with kmotion. If not, see <http://www.gnu.org/licenses/>.
"""
The automated uninstaller
"""
import os, pwd
from subprocess import * # breaking habit of a lifetime !
class exit_(Exception): pass
UNINSTALL_TEXT = """
****************************************************************************
Welcome to the kmotion v2 automatic ununstaller. If this uninstaller fails
please file a bug at http://code.google.com/p/kmotion-v2-code/issues/list
with full details so kmotion can be improved.
****************************************************************************
This universal uninstaller will modify your system in the following ways :
(1) The 'kmotion' and 'kmotion_ptz' exes will be removed from 'bin'
(2) The kmotion vhosts include will be removed from 'apache2.conf'
(3) The @reboot kmotion start will be removed from 'crontab'
Type \'uninstall\' to start uninstall :"""
LINE_TEXT = """
****************************************************************************
"""
COMPLETION_TEXT = """
****************************************************************************
KMOTION HAS NOW BEEN UNINSTALLED
****************************************************************************
"""
def uninstall():
"""
The automated uninstall script
args :
excepts :
return : none
"""
# ##########################################################################
print UNINSTALL_TEXT,
if raw_input() != 'uninstall':
raise exit_('Uninstall aborted')
print LINE_TEXT,
# ##########################################################################
# check we are running as root
checking('Checking install is running as root')
uid = os.getuid()
if uid != 0:
fail()
raise exit_('The uninstaller needs to be run as root')
ok()
# ##########################################################################
# check we can read ./kmotion_rc - if we can't, assume we are
# not in the kmotion root directory
checking('Checking uninstaller is running in correct directory')
if not os.path.isfile('./kmotion_rc'):
fail()
raise exit_('Please \'cd\' to the kmotion root directory before running the uninstaller')
ok()
# ##########################################################################
checking('Removing \'kmotion\' and \'kmotion_ptz\' exes from \'bin\'')
rm_exes()
ok()
# ##########################################################################
checking('Removing kmotion vhost reference from \'apache2.conf\'')
rm_apache2_include()
ok()
# ##########################################################################
checking('Apache2 restart')
ok()
restart_apache2()
# ##########################################################################
checking('Removing @reboot from \'crontab\'')
rm_crontab_boot()
ok()
print COMPLETION_TEXT
def rm_exes():
"""
Remove kmotion and kmotion_reload bin files from /usr/local/bin, /usr/bin or /bin
args :
excepts :
return : none
"""
paths = ['/usr/local/bin', '/usr/bin', '/bin']
for exe_path in paths:
if os.path.isfile('%s/kmotion' % exe_path): os.remove('%s/kmotion' % exe_path)
if os.path.isfile('%s/kmotion_ptz' % exe_path): os.remove('%s/kmotion_ptz' % exe_path)
def rm_apache2_include():
"""
Locate apache2.conf and remove any kmotion include references
args :
excepts :
return : none
"""
confs = ['/etc/apache2/apache2.conf', '/etc/apache2/httpd.conf',
'/etc/httpd/conf/httpd.conf', '/etc/httpd/httpd.conf',
'/etc/apache2/default-server.conf']
for conf in confs: # coded this way to allow for different .conf files
if os.path.isfile(conf):
f_obj = open(conf)
lines = f_obj.readlines()
f_obj.close()
for i in range(len(lines) - 1):
if i >= len(lines): break # 'del' will shorten the list length
if (lines[i].rstrip() == '# Include kmotion vhosts directory' and
lines[i + 1][:7] == 'Include'):
del lines[i:i + 2]
if lines[-1] == '\n': del lines[-1] # strip off new line
f_obj = open(conf, 'w')
f_obj.writelines(lines)
f_obj.close()
break
def restart_apache2():
"""
Restart the apache2 server
args :
excepts :
return :
"""
if call(['which', 'apachectl']) == 1:
call(['apache2ctl', 'restart'])
else:
call(['apachectl', 'restart'])
#apache2s = ['/etc/init.d/apache2', '/etc/init.d/httpd']
#for apache2 in apache2s:
#if os.path.isfile(apache2):
#print
#os.system('%s restart' % apache2) # os.system used deliberately as
## it dumps output to term real time
#break
#else:
#raise exit_('Unable to locate : %s' % _list_format(apache2s))
def rm_crontab_boot():
"""
Checks all users crontabs and removes any references to @reboot kmotion
args :
excepts :
return : none
"""
# How about: users = [ i[0] for i in pwd.getpwall() if i[2] > 500 ] --
# seems to work on all unix variants I have access to capable of running
# Motion :) -- I'd make the groupid check >1000 but the pesky OSX uses
# 501 for it's first user. Patch from Roman Gaufman
for user in [i[0] for i in pwd.getpwall() if i[2] > 500 or i[2] == 0]:
f_obj = os.popen('crontab -u %s -l' % user)
ctab = f_obj.readlines()
f_obj.close()
# 'ctab == []' when no crontab for user
if ctab == [] or ctab[0] == 'no crontab for %s' % user: continue
tmp = []
for line in ctab:
if not (line[:7] == '@reboot' and line[-17:] == '/kmotion start &\n'):
tmp.append(line)
f_obj = os.popen('crontab -u %s -' % user, 'w')
f_obj.writelines(tmp)
f_obj.close()
def _list_format(list_):
"""
Changes a list of strings into a comma seperated string containing all the
list items with an 'or' instead of the last comma
args : list_ ... a list of strings
excepts :
return : string ... a single formatted string
"""
tmp = '\'%s\'' % list_[0] # for correct ',' logic
for i in list_[1:-1]: tmp += ', \'%s\'' % i
return '%s or \'%s\'' % (tmp, list_[-1])
def checking(text_):
"""
Print the text and calculate the number of '.'s
args : text ... the string to print
excepts :
return : none
"""
print text_, '.' * (68 - len(text_)) ,
def ok():
"""
print [ OK ]
args :
excepts :
return : none
"""
print '[ OK ]'
def fail():
"""
print [FAIL]
args :
excepts :
return : none
"""
print '[FAIL]'
try:
uninstall()
except exit_, text:
print '\n%s\n' % text