1 | import sys |
---|
2 | import os |
---|
3 | import subprocess |
---|
4 | import stat |
---|
5 | import locale |
---|
6 | import tempfile |
---|
7 | import xmlrpc.client as n4d |
---|
8 | import ssl |
---|
9 | import time |
---|
10 | |
---|
11 | class zmdmanager: |
---|
12 | def __init__(self): |
---|
13 | self.locale=locale.getlocale()[0] |
---|
14 | self.dbg=1 |
---|
15 | self.zmdPath='/usr/share/zero-center/zmds' |
---|
16 | self.pluginInfo={'install':'zmd','pkginfo':'zmd','remove':'zmd'} |
---|
17 | self.progress=0 |
---|
18 | self.n4dclient='' |
---|
19 | self.result=[] |
---|
20 | #def __init__ |
---|
21 | |
---|
22 | def set_debug(self,dbg='1'): |
---|
23 | self.dbg=int(dbg) |
---|
24 | self._debug ("Debug enabled") |
---|
25 | #def set__debug |
---|
26 | |
---|
27 | def _debug(self,msg=''): |
---|
28 | if self.dbg==1: |
---|
29 | print ('DEBUG Zmd: '+str(msg)) |
---|
30 | #def _debug |
---|
31 | |
---|
32 | def register(self): |
---|
33 | return(self.pluginInfo) |
---|
34 | #def register |
---|
35 | |
---|
36 | def execute_action(self,action,applist): |
---|
37 | count=len(applist) |
---|
38 | self.n4dclient=self._n4d_connect() |
---|
39 | for appInfo in applist: |
---|
40 | if not (os.path.exists(self.zmdPath)): |
---|
41 | return applist; |
---|
42 | if (action): |
---|
43 | if action=='install': |
---|
44 | self._install_Zmd(appInfo) |
---|
45 | if action=='remove': |
---|
46 | self._remove_Zmd(appInfo) |
---|
47 | if action=='pkginfo': |
---|
48 | self._get_Zmd_Info(appInfo) |
---|
49 | self.progress=100 |
---|
50 | return(self.result) |
---|
51 | #def execute_action |
---|
52 | |
---|
53 | def _callback(self,zmdLauncher): |
---|
54 | inc=1 |
---|
55 | limit=99 |
---|
56 | n4dvars=self.n4dclient.get_variable("","VariablesManager","ZEROCENTER") |
---|
57 | if zmdLauncher in n4dvars.keys(): |
---|
58 | if n4dvars[zmdLauncher]['pulsating']: |
---|
59 | margin=limit-self.progress |
---|
60 | inc=round(margin/limit,3) |
---|
61 | self.progress=self.progress+inc |
---|
62 | #def _callback |
---|
63 | |
---|
64 | def _install_Zmd(self,appInfo): |
---|
65 | zmd=self.zmdPath+'/'+appInfo['package']+'.zmd' |
---|
66 | self._debug("Installing "+str(zmd)) |
---|
67 | try: |
---|
68 | zmdsudo=['gksudo',zmd] |
---|
69 | print("executing "+str(zmdsudo)) |
---|
70 | zmdCmd=subprocess.Popen(zmdsudo,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
71 | zmdLauncher=os.path.basename(zmd) |
---|
72 | zmdLauncher=os.path.splitext(zmdLauncher)[0] |
---|
73 | while zmdCmd.poll() is None: |
---|
74 | self._callback(zmdLauncher) |
---|
75 | time.sleep(0.4) |
---|
76 | zmdResult=zmdCmd.stdout.read() |
---|
77 | zmdError=zmdCmd.stderr.read() |
---|
78 | self._debug("Result: "+str(zmdError)) |
---|
79 | self._debug("Result: "+str(zmdResult)) |
---|
80 | appInfo=self._get_Zmd_Info(appInfo) |
---|
81 | if appInfo['status']=='Installed': |
---|
82 | self.result=[{appInfo['name']:" installed succesfully"}] |
---|
83 | else: |
---|
84 | self.result=[{appInfo['name']:" failed to install"}] |
---|
85 | except Exception as e: |
---|
86 | appInfo=self._get_Zmd_Info(appInfo) |
---|
87 | self._debug(str(e)) |
---|
88 | if appInfo['status']=='Installed': |
---|
89 | self.result=[{appInfo['name']:" installed succesfully"}] |
---|
90 | else: |
---|
91 | self.result=[{appInfo['name']:" failed to install"}] |
---|
92 | #def _install_Zmd |
---|
93 | |
---|
94 | def _remove_Zmd(self,appInfo): |
---|
95 | zmd=appInfo['package']+'.zmd' |
---|
96 | self._debug("Removing "+str(zmd)) |
---|
97 | os.chdir(self.zmdPath) |
---|
98 | sw_continue=False |
---|
99 | try: |
---|
100 | removePackages=[] |
---|
101 | f=open(zmd,'r') |
---|
102 | for line in f: |
---|
103 | if 'PACKAGE_LIST=' in line: |
---|
104 | sw_continue=True |
---|
105 | packagelist=line.split('=')[-1] |
---|
106 | packagelist=packagelist.replace('"','') |
---|
107 | packagelist=packagelist[:-1] |
---|
108 | #We've the file with the packagelist, now it's time to read the list |
---|
109 | print("Obtaining packages in : "+packagelist) |
---|
110 | f2=open (packagelist,'r') |
---|
111 | for line2 in f2: |
---|
112 | pkg=line2.split(' ')[0] |
---|
113 | pkg=pkg.split("\t")[0] |
---|
114 | pkg=pkg.replace('"','') |
---|
115 | self._debug("Append to remove list: "+pkg) |
---|
116 | removePackages.append(pkg) |
---|
117 | f2.close() |
---|
118 | f.close() |
---|
119 | except Exception as e: |
---|
120 | print(str(e)) |
---|
121 | if sw_continue: |
---|
122 | zeroScript='/tmp/zeroScript' |
---|
123 | f3=open(zeroScript,'w') |
---|
124 | f3.write('#!/bin/bash'+"\n") |
---|
125 | for pkg in removePackages: |
---|
126 | f3.write('/usr/bin/zero-installer remove '+pkg+"\n") |
---|
127 | f3.write ("zero-center set-non-configured "+appInfo['package']+"\n") |
---|
128 | f3.close() |
---|
129 | 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) |
---|
130 | zmdsudo=['gksudo',zeroScript] |
---|
131 | try: |
---|
132 | self._debug("Executing "+str(zmdsudo)) |
---|
133 | zmdLauncher=os.path.basename(zmd) |
---|
134 | zmdLauncher=os.path.splitext(zmdLauncher)[0] |
---|
135 | zmdCmd=subprocess.Popen(zmdsudo,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) |
---|
136 | self._debug("Launched") |
---|
137 | while zmdCmd.poll() is None: |
---|
138 | self._callback(zmdLauncher) |
---|
139 | time.sleep(0.2) |
---|
140 | zmdResult=zmdCmd.stdout.read() |
---|
141 | zmdError=zmdCmd.stderr.read() |
---|
142 | except Exception as e: |
---|
143 | print(str(e)) |
---|
144 | self._debug("Result: "+str(zmdResult)) |
---|
145 | self._debug("Error: "+str(zmdError)) |
---|
146 | appInfo=self._get_Zmd_Info(appInfo) |
---|
147 | if appInfo['status']=='Installed': |
---|
148 | self.result=[{appInfo['name']:" failed to remove"}] |
---|
149 | else: |
---|
150 | self.result=[{appInfo['name']:" removed succesfully"}] |
---|
151 | os.remove(zeroScript) |
---|
152 | #def _remove_Zmd |
---|
153 | |
---|
154 | def _get_Zmd_Info(self,appInfo): |
---|
155 | n4dvars=self.n4dclient.get_variable("","VariablesManager","ZEROCENTER") |
---|
156 | zmd=appInfo['package'] |
---|
157 | appInfo['status']='Available' |
---|
158 | for key in n4dvars: |
---|
159 | if zmd.lower() in key.lower(): |
---|
160 | if n4dvars[key]['state']==1: |
---|
161 | appInfo['status']='Installed' |
---|
162 | self.result.append(appInfo) |
---|
163 | return(appInfo) |
---|
164 | #def _get_Zmd_Info |
---|
165 | |
---|
166 | def _n4d_connect(self): |
---|
167 | #Setup SSL |
---|
168 | context=ssl._create_unverified_context() |
---|
169 | n4dclient = n4d.ServerProxy("https://localhost:9779",context=context) |
---|
170 | return(n4dclient) |
---|
171 | #def _n4d_connect |
---|