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=False |
---|
18 | self.zmd_folder='/usr/share/zero-center/zmds' |
---|
19 | self.disabled=None |
---|
20 | if hasattr(sys,'last_value') or not (os.path.exists(self.zmd_folder)): |
---|
21 | #If there's an error at this point it only could be an importError caused by xmlrpc |
---|
22 | print("ZMD support disabled") |
---|
23 | self.disabled=True |
---|
24 | self.plugin_actions={'install':'zmd','pkginfo':'zmd','remove':'zmd'} |
---|
25 | self.progress=0 |
---|
26 | self.n4dclient='' |
---|
27 | self.result={} |
---|
28 | self.result['data']=[] |
---|
29 | self.result['status']={} |
---|
30 | self.result['status']={'status':-1,'msg':''} |
---|
31 | #def __init__ |
---|
32 | |
---|
33 | def set_debug(self,dbg=True): |
---|
34 | self.dbg=dbg |
---|
35 | #self._debug ("Debug enabled") |
---|
36 | #def set__debug |
---|
37 | |
---|
38 | def _debug(self,msg=''): |
---|
39 | if self.dbg: |
---|
40 | print ('DEBUG Zmd: %s'%msg) |
---|
41 | #def _debug |
---|
42 | |
---|
43 | def register(self): |
---|
44 | return(self.plugin_actions) |
---|
45 | #def register |
---|
46 | |
---|
47 | def execute_action(self,action,applist): |
---|
48 | self.progress=0 |
---|
49 | self.result['status']={'status':-1,'msg':''} |
---|
50 | self.result['data']=list(applist) |
---|
51 | dataList=[] |
---|
52 | if self.disabled: |
---|
53 | self._set_status(9) |
---|
54 | else: |
---|
55 | count=len(applist) |
---|
56 | try: |
---|
57 | self.n4dclient=self._n4d_connect() |
---|
58 | for app_info in applist: |
---|
59 | if (action): |
---|
60 | if action=='install': |
---|
61 | dataList.append(self._install_Zmd(app_info)) |
---|
62 | if action=='remove': |
---|
63 | dataList.append(self._remove_Zmd(app_info)) |
---|
64 | if action=='pkginfo': |
---|
65 | dataList.append(self._get_Zmd_Info(app_info)) |
---|
66 | self.result['data']=list(dataList) |
---|
67 | except: |
---|
68 | self.disabled=True |
---|
69 | self._set_status(10) |
---|
70 | self.progress=100 |
---|
71 | return(self.result) |
---|
72 | #def execute_action |
---|
73 | |
---|
74 | def _set_status(self,status,msg=''): |
---|
75 | self.result['status']={'status':status,'msg':msg} |
---|
76 | #def _set_status |
---|
77 | |
---|
78 | def _callback(self,zmd_launcher): |
---|
79 | inc=1 |
---|
80 | limit=99 |
---|
81 | n4dvars=self.n4dclient.get_variable("","VariablesManager","ZEROCENTER") |
---|
82 | if zmd_launcher in n4dvars.keys(): |
---|
83 | if n4dvars[zmd_launcher]['pulsating']: |
---|
84 | margin=limit-self.progress |
---|
85 | inc=round(margin/limit,3) |
---|
86 | self.progress=self.progress+inc |
---|
87 | #def _callback |
---|
88 | |
---|
89 | def _install_Zmd(self,app_info): |
---|
90 | zmd=self.zmd_folder+'/'+app_info['package']+'.zmd' |
---|
91 | #self._debug("Installing "+str(zmd)) |
---|
92 | app_info=self._get_Zmd_Info(app_info) |
---|
93 | if app_info['state']=='installed': |
---|
94 | err=4 |
---|
95 | else: |
---|
96 | if os.path.exists(zmd): |
---|
97 | err=0 |
---|
98 | try: |
---|
99 | zmd_sudo=['pkexec',zmd] |
---|
100 | #self._debug("executing "+str(zmd_sudo)) |
---|
101 | launched_zmd=subprocess.Popen(zmd_sudo,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
102 | zmd_launcher=os.path.basename(zmd) |
---|
103 | zmd_launcher=os.path.splitext(zmd_launcher)[0] |
---|
104 | while launched_zmd.poll() is None: |
---|
105 | self._callback(zmd_launcher) |
---|
106 | time.sleep(0.4) |
---|
107 | zmd_status=launched_zmd.stdout.read() |
---|
108 | zmd_err=launched_zmd.stderr.read() |
---|
109 | #self._debug("Error: "+str(zmd_err)) |
---|
110 | #self._debug("Result: "+str(zmd_status)) |
---|
111 | except Exception as e: |
---|
112 | #self._debug(str(e)) |
---|
113 | pass |
---|
114 | finally: |
---|
115 | app_info=self._get_Zmd_Info(app_info) |
---|
116 | if app_info['state']!='installed': |
---|
117 | err=5 |
---|
118 | else: |
---|
119 | err=8 |
---|
120 | self._set_status(err) |
---|
121 | return(app_info) |
---|
122 | #def _install_Zmd |
---|
123 | |
---|
124 | def _remove_Zmd(self,app_info): |
---|
125 | zmd=app_info['package']+'.zmd' |
---|
126 | #self._debug("Removing "+str(zmd)) |
---|
127 | os.chdir(self.zmd_folder) |
---|
128 | sw_continue=False |
---|
129 | err=0 |
---|
130 | try: |
---|
131 | remove_packages=[] |
---|
132 | f=open(zmd,'r') |
---|
133 | for line in f: |
---|
134 | if 'PACKAGE_LIST=' in line: |
---|
135 | sw_continue=True |
---|
136 | packagelist=line.split('=')[-1] |
---|
137 | packagelist=packagelist.replace('"','') |
---|
138 | packagelist=packagelist[:-1] |
---|
139 | #We've the file with the packagelist, now it's time to read the list |
---|
140 | #self._debug("Obtaining packages in : "+packagelist) |
---|
141 | f2=open (packagelist,'r') |
---|
142 | for line2 in f2: |
---|
143 | pkg=line2.split(' ')[0] |
---|
144 | pkg=pkg.split("\t")[0] |
---|
145 | pkg=pkg.replace('"','') |
---|
146 | #self._debug("Append to remove list: "+pkg) |
---|
147 | remove_packages.append(pkg) |
---|
148 | f2.close() |
---|
149 | f.close() |
---|
150 | except Exception as e: |
---|
151 | err=7 |
---|
152 | print(str(e)) |
---|
153 | if sw_continue: |
---|
154 | zmd_script='/tmp/zmd_script' |
---|
155 | f3=open(zmd_script,'w') |
---|
156 | f3.write('#!/bin/bash'+"\n") |
---|
157 | for pkg in remove_packages: |
---|
158 | f3.write('/usr/bin/zero-installer remove '+pkg+"\n") |
---|
159 | f3.write ("zero-center set-non-configured "+app_info['package']+"\n") |
---|
160 | f3.close() |
---|
161 | os.chmod(zmd_script,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) |
---|
162 | # zmd_sudo=['gksudo',zmd_script] |
---|
163 | zmd_sudo=['pkexec',zmd_script] |
---|
164 | try: |
---|
165 | self._debug("Executing "+str(zmd_sudo)) |
---|
166 | zmd_launcher=os.path.basename(zmd) |
---|
167 | zmd_launcher=os.path.splitext(zmd_launcher)[0] |
---|
168 | launched_zmd=subprocess.Popen(zmd_sudo,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
169 | #self._debug("Launched") |
---|
170 | while launched_zmd.poll() is None: |
---|
171 | self._callback(zmd_launcher) |
---|
172 | time.sleep(0.2) |
---|
173 | zmd_status=launched_zmd.stdout.read() |
---|
174 | zmd_err=launched_zmd.stderr.read() |
---|
175 | except Exception as e: |
---|
176 | err=6 |
---|
177 | #self._debug(str(e)) |
---|
178 | #self._debug("Error: "+str(zmd_err)) |
---|
179 | #self._debug("Result: "+str(zmd_status)) |
---|
180 | app_info=self._get_Zmd_Info(app_info) |
---|
181 | if app_info['state']=='installed': |
---|
182 | err=6 |
---|
183 | os.remove(zmd_script) |
---|
184 | else: |
---|
185 | err=6 |
---|
186 | self._set_status(err) |
---|
187 | return(app_info) |
---|
188 | #def _remove_Zmd |
---|
189 | |
---|
190 | def _get_Zmd_Info(self,app_info): |
---|
191 | zmd=app_info['package'] |
---|
192 | app_info['state']='Available' |
---|
193 | try: |
---|
194 | n4dvars=self.n4dclient.get_variable("","VariablesManager","ZEROCENTER") |
---|
195 | if n4dvars: |
---|
196 | self._set_status(0) |
---|
197 | for key in n4dvars: |
---|
198 | if zmd.lower() in key.lower(): |
---|
199 | if 'state' in n4dvars[key]: |
---|
200 | if n4dvars[key]['state']==1: |
---|
201 | app_info['state']='installed' |
---|
202 | else: |
---|
203 | self._set_status(2) |
---|
204 | except Exception as e: |
---|
205 | self._set_status(10) |
---|
206 | return(app_info) |
---|
207 | #def _get_Zmd_Info |
---|
208 | |
---|
209 | def _n4d_connect(self): |
---|
210 | #Setup SSL |
---|
211 | context=ssl._create_unverified_context() |
---|
212 | n4dclient = n4d.ServerProxy("https://localhost:9779",context=context) |
---|
213 | return(n4dclient) |
---|
214 | #def _n4d_connect |
---|