forked from JamezQ/Palaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_plugin
executable file
·290 lines (288 loc) · 9.11 KB
/
install_plugin
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
#!/usr/bin/env python
# This program 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.
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import argparse, shutil, os, tarfile
parser = argparse.ArgumentParser(description='Install and remove Plugins.')
parser.add_argument('-i', metavar='file', type=str,help='Install a plugin - plugin .sp or .info file',default="")
parser.add_argument('-r', metavar='plugin', type=str,help='Remove the specified plugin',default="")
parser.add_argument('-info', metavar='plugin', type=str,help='Display info on a plugin',default="")
parser.add_argument('-l', dest='listPlugins', action='store_true',default=False,help='List the installed plugins')
args = parser.parse_args()
listPlugins = args.listPlugins
root = ''.join([e+'/' for e in os.path.realpath(__file__).split('/')[0:-1]])
if listPlugins == True:
try:
pluginList = open(root+'plugins')
except:
print "Plugins Library Not Found"
print "Plugins:"
for line in pluginList:
name,version,author,cmdfile,dictionaries,actions = line.split('|||')
print "\t"+name,"-",version
actions = eval(actions)
for each in actions:
print "\t\t"+each[0]+"\t\t"+each[1].replace('\n','')
elif args.info != '':
try:
pluginList = open(root+'plugins')
except:
print "Plugins Library Not Found"
for line in pluginList:
name,version,author,cmdfile,dictionaries,actions = line.split('|||')
if name == args.info:
print name
print "\tAuthor:",author
print "\tVersion:",version
print "\tCommand(s):",str(cmdfile)
print "\tDictionaries:",str(dictionaries)
print "\tActions:"
actions = eval(actions)
for each in actions:
print "\t\t"+each[0]+"\t\t"+each[1].replace('\n','')
print "\tCommands:"
dictionary = open(root+'Recognition/modes/main.dic')
adding = False
for line in dictionary:
if adding == False:
add = False
if line.startswith('#PLUGIN: '):
if line.replace('#PLUGIN: ','').replace('\n','') == args.info:
add = False
adding = True
else:
add = True
if line == "#END\n":
adding = False
add = False
if add == True:
print "\t\t"+line.replace('\n','')
dictionary.close()
elif args.i != '':
filename = args.i
try:
pluginList = open(root+'plugins')
except:
print "Plugins Library Not Found"
plugins = {}
for line in pluginList:
name,version,author,cmdfile,dictionaries,actions = line.split('|||')
plugins[name] = [float(version),eval(cmdfile)]
if filename.endswith('.sp'):
tar = tarfile.open(mode='r:gz',fileobj=file(filename))
data = tar.extractfile('plugin.info')
else:
try:
data = open(filename)
except:
print 'Invalid File - not found'
recActions = False
actions = []
depend = []
dictionaries = []
description = ''
for line in data:
if recActions == True:
if line != '':
actions.append(line.replace('\n','').split(',',1))
elif line.startswith("name"):
name = line.replace('name =','').replace(' ','').replace('\n','')
multiDesc = False
elif line.startswith("version"):
version = float(line.replace('version =','').replace(' ','').replace('\n',''))
multiDesc = False
elif line.startswith("description"):
multiDesc = True
elif line.startswith('dictionaries'):
dictionaries = line.replace('dictionaries =','').replace(' ','').replace('\n','')
try:
dictionaries = dictionaries.split(',')
except:
dictionaries = [dictionaries]
elif line.startswith("file"):
cmdfile = line.replace('file =','').replace(' ','').replace('\n','')
try:
cmdfile = cmdfile.split(",")
except:
cmdfile = [cmdfile]
multiDesc = False
elif line.startswith("author"):
author = line.replace("author =",'').replace('\n','')
multiDesc = False
elif line.startswith("dependencies"):
depend = line.replace("dependencies = ",'').replace('\n','')
multiDesc = False
try:
depend = depend.split(" ")
except:
depend = [depend]
elif line.startswith("actions"):
recActions = True
multiDesc = False
if multiDesc == True:
description = description + line.replace('description =','').replace('\n','')
data.close()
install = True
try:
currentVersion = plugins[name][0]
if currentVersion > version:
print "A newer version is already installed"
elif currentVersion == version:
print "This version is already installed"
else:
print "Updating Files Has Not Yet Been Added, please remove the old version with -r and then install again"
#Install Files
except:
for each in plugins:
for exe in plugins[each][1]:
if exe in cmdfile and exe != 'NONE' and len(plugins[each][1]) != 1 and install == True:
print "This plugin has a conflict with",each
print "Please rename the executable"
install = False
if install != False:
print name
print
print description
if len(depend) != 0:
print "WARNING: THIS PLUGIN REQUIRES:"
for each in depend:
print each,
print
print
if raw_input("Are you sure you want to continue? [Y/N]").lower() == 'y':
try:
dictionary = open(root+'Recognition/modes/main.dic')
text = dictionary.read()
dictionary.close()
if filename.endswith('.sp'):
command = tar.extractfile('actions.dic').read()
else:
data = open(''.join([e+'/' for e in args.i.split('/')[0:-1]]) + 'actions.dic')
command = data.read()
data.close()
try:
dictionary = open(root+'Recognition/modes/main.dic','w')
dictionary.write("#PLUGIN: "+name+"\n")
dictionary.write(command)
dictionary.write("#END\n")
dictionary.write(text)
except:
print "Error Installing Plugin In main.dic"
dictionary.close()
except:
print "Dictionary Not Found"
if filename.endswith('.sp'):
try:
for dic in dictionaries:
binfile = open(root+"Recognition/modes/"+dic,'w')
binfile.write(tar.extractfile(dic).read())
binfile.close()
print "File Installed to modes",dic
except:
print "Error Installing to modes",dic
else:
src = ''.join([e+'/' for e in filename.split('/')[0:-1]])
try:
for dic in dictionaries:
shutil.copy(src+exe,root+"Recognition/modes/")
print "File Installed to Bin",dic
except:
print "Error Moving File",dic
if cmdfile != "NONE":
if filename.endswith('.sp'):
try:
for exe in cmdfile:
binfile = open(root+"Recognition/bin/"+exe,'w')
binfile.write(tar.extractfile(exe).read())
binfile.close()
print "File Installed to Bin",exe
except:
print "Error Installing to Bin",exe
else:
src = ''.join([e+'/' for e in filename.split('/')[0:-1]])
try:
for exe in cmdfile:
shutil.copy(src+exe,root+"Recognition/bin/")
print "File Installed to Bin",exe
except:
print "Error Moving File"
for exe in cmdfile:
os.system("chmod +x "+root+"Recognition/bin/"+exe)
try:
pluginList = open(root+'plugins','a')
except:
print "Plugins Library Not Found"
pluginList.write(name+"|||"+str(version)+"|||"+author+"|||"+str(cmdfile)+"|||"+str(dictionaries)+"|||"+str(actions)+"\n")
print "Installed Plugin"
else:
print "Quiting Installation"
elif args.r != '':
pluginName = args.r
try:
pluginList = open(root+'plugins')
except:
print "Plugins Library Not Found"
good = False
try:
dictionary = open(root+'Recognition/modes/main.dic')
text = ""
deleting = False
for line in dictionary:
if deleting == False:
add = True
if line.startswith('#PLUGIN: '):
if line.replace('#PLUGIN: ','').replace('\n','') == pluginName:
add = False
deleting = True
else:
add = False
if line == "#END\n":
deleting = False
if add == True:
text = text + line
dictionary.close()
try:
dictionary = open(root+'Recognition/modes/main.dic','w')
dictionary.write(text)
except:
print "Error Installing Plugin In main.dic"
dictionary.close()
except:
print "Dictionary Not Found"
text = ""
found = False
for line in pluginList:
name,version,author,cmdfile,dictionaries,actions = line.split('|||')
cmdfile = eval(cmdfile)
dictionaries = eval(dictionaries)
if name == pluginName:
good = True
if cmdfile[0] != "NONE":
for exe in cmdfile:
os.remove(root+"Recognition/bin/"+exe)
print "Removed from Bin",exe
for dic in dictionaries:
os.remove(root+"Recognition/modes/"+dic)
print 'Removed from Modes',dic
print name,"Uninstalled"
found = True
else:
text = text + line
pluginList.close()
try:
pluginList = open(root+'plugins','w')
except:
print "Plugins Library Not Found"
pluginList.write(text)
pluginList.close()
if found == False:
print "Plugin Not Installed"
else:
print "No Action Supplied"