1 | import sys |
---|
2 | import os |
---|
3 | import subprocess |
---|
4 | import stat |
---|
5 | import locale |
---|
6 | import tempfile |
---|
7 | try: |
---|
8 | import xmlrpc.client as n4d |
---|
9 | except ImportError: |
---|
10 | raise ImportError('xmlrpc not available. Disabling ZmdManager') |
---|
11 | import ssl |
---|
12 | import time |
---|
13 | |
---|
14 | class zmdmanager: |
---|
15 | def __init__(self): |
---|
16 | self.locale=locale.getlocale()[0] |
---|
17 | self.dbg=0 |
---|
18 | self.zmdPath='/usr/share/zero-center/zmds' |
---|
19 | self.disabled=False |
---|
20 | if hasattr(sys,'last_value') or not (os.path.exists(self.zmdPath)): |
---|
21 | #If there's an error at this point it only could be an importError caused by xmlrpc |
---|
22 | self.disabled=True |
---|
23 | self.pluginInfo={'install':'zmd','pkginfo':'zmd','remove':'zmd'} |
---|
24 | self.progress=0 |
---|
25 | self.n4dclient='' |
---|
26 | self.result={} |
---|
27 | self.result['data']=[] |
---|
28 | self.result['status']={} |
---|
29 | self.result['status']={'status':-1,'msg':''} |
---|
30 | #def __init__ |
---|
31 | |
---|
32 | def set_debug(self,dbg='1'): |
---|
33 | self.dbg=int(dbg) |
---|
34 | self._debug ("Debug enabled") |
---|
35 | #def set__debug |
---|
36 | |
---|
37 | def _debug(self,msg=''): |
---|
38 | if self.dbg==1: |
---|
39 | print ('DEBUG Zmd: '+str(msg)) |
---|
40 | #def _debug |
---|
41 | |
---|
42 | def register(self): |
---|
43 | return(self.pluginInfo) |
---|
44 | #def register |
---|
45 | |
---|
46 | def execute_action(self,action,applist): |
---|
47 | self.progress=0 |
---|
48 | self.result['status']={'status':-1,'msg':''} |
---|
49 | self.result['data']=list(applist) |
---|
50 | dataList=[] |
---|
51 | if self.disabled: |
---|
52 | self._set_status(9) |
---|
53 | else: |
---|
54 | count=len(applist) |
---|
55 | try: |
---|
56 | self.n4dclient=self._n4d_connect() |
---|
57 | for appInfo in applist: |
---|
58 | if (action): |
---|
59 | if action=='install': |
---|
60 | dataList.append(self._install_Zmd(appInfo)) |
---|
61 | if action=='remove': |
---|
62 | dataList.append(self._remove_Zmd(appInfo)) |
---|
63 | if action=='pkginfo': |
---|
64 | dataList.append(self._get_Zmd_Info(appInfo)) |
---|
65 | self.result['data']=list(dataList) |
---|
66 | except: |
---|
67 | self.disabled=True |
---|
68 | self._set_status(10) |
---|
69 | self.progress=100 |
---|
70 | return(self.result) |
---|
71 | #def execute_action |
---|
72 | |
---|
73 | def _set_status(self,status,msg=''): |
---|
74 | self.result['status']={'status':status,'msg':msg} |
---|
75 | # dataList['status']={'status':status,'msg':msg} |
---|
76 | |
---|
77 | def _callback(self,zmdLauncher): |
---|
78 | inc=1 |
---|
79 | limit=99 |
---|
80 | n4dvars=self.n4dclient.get_variable("","VariablesManager","ZEROCENTER") |
---|
81 | if zmdLauncher in n4dvars.keys(): |
---|
82 | if n4dvars[zmdLauncher]['pulsating']: |
---|
83 | margin=limit-self.progress |
---|
84 | inc=round(margin/limit,3) |
---|
85 | self.progress=self.progress+inc |
---|
86 | #def _callback |
---|
87 | |
---|
88 | def _install_Zmd(self,appInfo): |
---|
89 | zmd=self.zmdPath+'/'+appInfo['package']+'.zmd' |
---|
90 | self._debug("Installing "+str(zmd)) |
---|
91 | appInfo=self._get_Zmd_Info(appInfo) |
---|
92 | if appInfo['state']=='installed': |
---|
93 | err=4 |
---|
94 | else: |
---|
95 | if os.path.exists(zmd): |
---|
96 | err=0 |
---|
97 | try: |
---|
98 | # zmdsudo=['gksudo',zmd] |
---|
99 | zmdsudo=['pe',zmd] |
---|
100 | self._debug("executing "+str(zmdsudo)) |
---|
101 | zmdCmd=subprocess.Popen(zmdsudo,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
102 | zmdLauncher=os.path.basename(zmd) |
---|
103 | zmdLauncher=os.path.splitext(zmdLauncher)[0] |
---|
104 | while zmdCmd.poll() is None: |
---|
105 | self._callback(zmdLauncher) |
---|
106 | time.sleep(0.4) |
---|
107 | zmdResult=zmdCmd.stdout.read() |
---|
108 | zmdError=zmdCmd.stderr.read() |
---|
109 | self._debug("Error: "+str(zmdError)) |
---|
110 | self._debug("Result: "+str(zmdResult)) |
---|
111 | except Exception as e: |
---|
112 | self._debug(str(e)) |
---|
113 | finally: |
---|
114 | appInfo=self._get_Zmd_Info(appInfo) |
---|
115 | if appInfo['state']!='installed': |
---|
116 | err=5 |
---|
117 | else: |
---|
118 | err=8 |
---|
119 | self._set_status(err) |
---|
120 | return(appInfo) |
---|
121 | #def _install_Zmd |
---|
122 | |
---|
123 | def _remove_Zmd(self,appInfo): |
---|
124 | zmd=appInfo['package']+'.zmd' |
---|
125 | self._debug("Removing "+str(zmd)) |
---|
126 | os.chdir(self.zmdPath) |
---|
127 | sw_continue=False |
---|
128 | err=0 |
---|
129 | try: |
---|
130 | removePackages=[] |
---|
131 | f=open(zmd,'r') |
---|
132 | for line in f: |
---|
133 | if 'PACKAGE_LIST=' in line: |
---|
134 | sw_continue=True |
---|
135 | packagelist=line.split('=')[-1] |
---|
136 | packagelist=packagelist.replace('"','') |
---|
137 | packagelist=packagelist[:-1] |
---|
138 | #We've the file with the packagelist, now it's time to read the list |
---|
139 | self._debug("Obtaining packages in : "+packagelist) |
---|
140 | f2=open (packagelist,'r') |
---|
141 | for line2 in f2: |
---|
142 | pkg=line2.split(' ')[0] |
---|
143 | pkg=pkg.split("\t")[0] |
---|
144 | pkg=pkg.replace('"','') |
---|
145 | self._debug("Append to remove list: "+pkg) |
---|
146 | removePackages.append(pkg) |
---|
147 | f2.close() |
---|
148 | f.close() |
---|
149 | except Exception as e: |
---|
150 | err=7 |
---|
151 | print(str(e)) |
---|
152 | if sw_continue: |
---|
153 | zeroScript='/tmp/zeroScript' |
---|
154 | f3=open(zeroScript,'w') |
---|
155 | f3.write('#!/bin/bash'+"\n") |
---|
156 | for pkg in removePackages: |
---|
157 | f3.write('/usr/bin/zero-installer remove '+pkg+"\n") |
---|
158 | f3.write ("zero-center set-non-configured "+appInfo['package']+"\n") |
---|
159 | f3.close() |
---|
160 | os.chmod(zeroScript,stat.S_IEXEC|stat.S_IREAD|stat.S_IWRITE|stat.S_IROTH|stat.S_IWOTH|stat.S_IXOTH|stat.S_IRGRP|stat.S_IWGRP|stat.S_IXGRP) |
---|
161 | zmdsudo=['gksudo',zeroScript] |
---|
162 | try: |
---|
163 | self._debug("Executing "+str(zmdsudo)) |
---|
164 | zmdLauncher=os.path.basename(zmd) |
---|
165 | zmdLauncher=os.path.splitext(zmdLauncher)[0] |
---|
166 | zmdCmd=subprocess.Popen(zmdsudo,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
167 | self._debug("Launched") |
---|
168 | while zmdCmd.poll() is None: |
---|
169 | self._callback(zmdLauncher) |
---|
170 | time.sleep(0.2) |
---|
171 | zmdResult=zmdCmd.stdout.read() |
---|
172 | zmdError=zmdCmd.stderr.read() |
---|
173 | except Exception as e: |
---|
174 | err=6 |
---|
175 | self._debug(str(e)) |
---|
176 | self._debug("Error: "+str(zmdError)) |
---|
177 | self._debug("Result: "+str(zmdResult)) |
---|
178 | appInfo=self._get_Zmd_Info(appInfo) |
---|
179 | if appInfo['state']=='installed': |
---|
180 | err=6 |
---|
181 | os.remove(zeroScript) |
---|
182 | else: |
---|
183 | err=6 |
---|
184 | self._set_status(err) |
---|
185 | return(appInfo) |
---|
186 | #def _remove_Zmd |
---|
187 | |
---|
188 | def _get_Zmd_Info(self,appInfo): |
---|
189 | zmd=appInfo['package'] |
---|
190 | appInfo['state']='Available' |
---|
191 | try: |
---|
192 | n4dvars=self.n4dclient.get_variable("","VariablesManager","ZEROCENTER") |
---|
193 | if n4dvars: |
---|
194 | self._set_status(0) |
---|
195 | for key in n4dvars: |
---|
196 | if zmd.lower() in key.lower(): |
---|
197 | if 'state' in n4dvars[key]: |
---|
198 | if n4dvars[key]['state']==1: |
---|
199 | appInfo['state']='installed' |
---|
200 | else: |
---|
201 | self._set_status(2) |
---|
202 | except Exception as e: |
---|
203 | self._set_status(10) |
---|
204 | # self.result['data'].append(appInfo) |
---|
205 | # self.result.append(appInfo) |
---|
206 | if appInfo['state']!='installed': |
---|
207 | #There're some zomandos that may be unregistered, so we check again |
---|
208 | zmdApp='/usr/share/zero-center/applications/'+zmd+".app" |
---|
209 | print("*******************") |
---|
210 | print(zmdApp) |
---|
211 | print("*******************") |
---|
212 | if os.path.isfile(zmdApp): |
---|
213 | f=open(zmdApp,'r') |
---|
214 | fileContent=f.readlines() |
---|
215 | f.close |
---|
216 | sw_service=False |
---|
217 | for line in fileContent: |
---|
218 | if line.startswith('Service'): |
---|
219 | sw_service=True |
---|
220 | if 'True' not in line: |
---|
221 | print(line) |
---|
222 | appInfo['state']='installed' |
---|
223 | if sw_service==False: |
---|
224 | appInfo['state']='installed' |
---|
225 | |
---|
226 | return(appInfo) |
---|
227 | #def _get_Zmd_Info |
---|
228 | |
---|
229 | def _n4d_connect(self): |
---|
230 | #Setup SSL |
---|
231 | context=ssl._create_unverified_context() |
---|
232 | n4dclient = n4d.ServerProxy("https://localhost:9779",context=context) |
---|
233 | return(n4dclient) |
---|
234 | #def _n4d_connect |
---|