1 | import xmlrpclib |
---|
2 | import os |
---|
3 | import shutil |
---|
4 | import subprocess |
---|
5 | import socket |
---|
6 | |
---|
7 | class LliurexUpCore(object): |
---|
8 | """docstring for LliurexUpCore""" |
---|
9 | def __init__(self): |
---|
10 | super(LliurexUpCore, self).__init__() |
---|
11 | self.defaultMirror = 'llx16' |
---|
12 | self.defaultVersion = 'xenial' |
---|
13 | self.processPath = '/var/run/lliurex-up' |
---|
14 | self.processSourceslist = '/var/run/lliurex-up/sourceslist' |
---|
15 | self.n4d = xmlrpclib.ServerProxy('https://localhost:9779') |
---|
16 | self.haveLliurexMirror = False |
---|
17 | self.flavours = [ x.strip() for x in self.n4d.lliurex_version('','LliurexVersion','-v')[1].split(',') ] |
---|
18 | if len(self.n4d.get_methods('MirrorManager')) > 0: |
---|
19 | self.haveLliurexMirror = True |
---|
20 | self.prepareEnvironment() |
---|
21 | |
---|
22 | def prepareEnvironment(self): |
---|
23 | ''' |
---|
24 | This funcion delete all environment and rebuild environment |
---|
25 | ''' |
---|
26 | if os.path.exists(self.processPath): |
---|
27 | shutil.rmtree(os.path.join(self.processPath)) |
---|
28 | os.mkdir(self.processPath) |
---|
29 | os.mkdir(self.processSourceslist) |
---|
30 | self.writeDefaultSourceslist() |
---|
31 | |
---|
32 | def writeDefaultSourceslist(self): |
---|
33 | f = open(os.path.join(self.processSourceslist,'default'),'w') |
---|
34 | f.write('deb http://lliurex.net/{version} {version} main'.format(version=self.defaultVersion)) |
---|
35 | f.write('deb http://lliurex.net/{version} {version}-updates main'.format(version=self.defaultVersion)) |
---|
36 | f.write('deb http://lliurex.net/{version} {version}-security main'.format(version=self.defaultVersion)) |
---|
37 | f.close() |
---|
38 | |
---|
39 | def getPackageVersionAvailable(self,package,options=""): |
---|
40 | ''' |
---|
41 | Args : |
---|
42 | package String |
---|
43 | options String |
---|
44 | |
---|
45 | return dictionary => result |
---|
46 | result : {'installed':String,'candidate':String} |
---|
47 | |
---|
48 | Options are Apt options |
---|
49 | ''' |
---|
50 | command = "LANG=C apt-cache policy {package} {options}".format(package=package,options=options) |
---|
51 | p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
52 | installed = None |
---|
53 | candidate = None |
---|
54 | for line in iter(p.stdout.readline,""): |
---|
55 | stripedline = line.strip() |
---|
56 | if stripedline.startswith("Installed"): |
---|
57 | installed = stripedline.replace("Installed: ","") |
---|
58 | if stripedline.startswith("Candidate"): |
---|
59 | candidate = stripedline.replace("Candidate: ","") |
---|
60 | return {"installed":installed,"candidate":candidate} |
---|
61 | |
---|
62 | def isLliurexUpIsUpdated(self): |
---|
63 | ''' |
---|
64 | return Boolean |
---|
65 | ''' |
---|
66 | sourceslistDefaultPath = os.path.join(self.processSourceslist,'default') |
---|
67 | options = "" |
---|
68 | if self.canConnectToLliurexNet(): |
---|
69 | options = "-o Dir::Etc::sourcelist={sourceslistOnlyLliurex} -o Dir::Etc::sourceparts=/dev/null".format(sourceslistOnlyLliurex=sourceslistDefaultPath) |
---|
70 | command = "LANG=C apt-get update {options}".format(options=options) |
---|
71 | subprocess.Popen(command,shell=True).communicate() |
---|
72 | result = self.getPackageVersionAvailable('lliurex-up',options) |
---|
73 | |
---|
74 | if result['installed'] != result['candidate']: |
---|
75 | return False |
---|
76 | return True |
---|
77 | |
---|
78 | def installLliurexUp(self,options=""): |
---|
79 | ''' |
---|
80 | Args : |
---|
81 | options String |
---|
82 | return dictionary => result |
---|
83 | result : {'returncode':Int,'stdout':String,'stderr':String} |
---|
84 | |
---|
85 | options are Apt options |
---|
86 | |
---|
87 | |
---|
88 | This function install lliurex-up |
---|
89 | ''' |
---|
90 | command = "LANG=C apt-get install lliurex-up {options}".format(options=options) |
---|
91 | p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) |
---|
92 | poutput,perror = p.communicate() |
---|
93 | return {'returncode':p.returncode,'stdout':poutput,'stderrs':perror} |
---|
94 | |
---|
95 | def lliurexMirrorIsUpdated(self): |
---|
96 | ''' |
---|
97 | return None | dictionary => result |
---|
98 | result : {'status':Boolean,'msg':String,'action':String} |
---|
99 | result.msg : message of status |
---|
100 | result.action : Action to launch |
---|
101 | ''' |
---|
102 | if self.haveLliurexMirror and 'server' in self.flavours : |
---|
103 | result = self.n4d.is_update_available('','MirrorManager',self.defaultMirror) |
---|
104 | return result |
---|
105 | return None |
---|
106 | |
---|
107 | def lliurexMirrorIsRunning(self): |
---|
108 | ''' |
---|
109 | return Boolean |
---|
110 | ''' |
---|
111 | if self.haveLliurexMirror and 'server' in self.flavours : |
---|
112 | result = self.n4d.is_alive('','MirrorManager') |
---|
113 | return result['status'] |
---|
114 | return False |
---|
115 | |
---|
116 | def getPercentageLliurexMirror(self): |
---|
117 | ''' |
---|
118 | return int | None |
---|
119 | ''' |
---|
120 | if self.haveLliurexMirror and 'server' in self.flavours: |
---|
121 | result = self.n4d.get_percentage('','MirrorManager',self.defaultMirror) |
---|
122 | if result['status']: |
---|
123 | return result['msg'] |
---|
124 | return None |
---|
125 | |
---|
126 | def checkFlavour(self): |
---|
127 | ''' |
---|
128 | return None|String |
---|
129 | If metapackages has been uninstalled, this function return |
---|
130 | package to must install. If return None, you are ok and don't need |
---|
131 | install anything. |
---|
132 | ''' |
---|
133 | targetMetapackage = None |
---|
134 | if 'None' in self.flavours: |
---|
135 | # get last flavour |
---|
136 | result = self.n4d.lliurex_version('','LliurexVersion','--history') |
---|
137 | if result[0]: |
---|
138 | history = [ x.strip().split('\t')[0].strip() for x in result[1].split('\n') ] |
---|
139 | history = [ x for x in history if not 'lliurex-meta-live' in x ] |
---|
140 | for x in reversed(history): |
---|
141 | if x.startswith('-'): |
---|
142 | targetMetapackage = x[2:] |
---|
143 | break |
---|
144 | return targetMetapackage |
---|
145 | |
---|
146 | def canConnectToLliurexNet(self): |
---|
147 | ''' |
---|
148 | return Boolean |
---|
149 | ''' |
---|
150 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
151 | host = socket.gethostbyname('lliurex.net') |
---|
152 | result = s.connect_ex((host, 80)) |
---|
153 | s.close() |
---|
154 | if result: |
---|
155 | return False |
---|
156 | return True |
---|
157 | |
---|
158 | def getLliurexVersion(self): |
---|
159 | ''' |
---|
160 | return dictionary => result |
---|
161 | result : {'installed':String,'candidate':String} |
---|
162 | ''' |
---|
163 | sourceslistDefaultPath = os.path.join(self.processSourceslist,'default') |
---|
164 | options = "" |
---|
165 | if self.canConnectToLliurexNet(): |
---|
166 | options = "-o Dir::Etc::sourcelist={sourceslistOnlyLliurex} -o Dir::Etc::sourceparts=/dev/null".format(sourceslistOnlyLliurex=sourceslistDefaultPath) |
---|
167 | command = "LANG=C apt-get update {options}".format(options=options) |
---|
168 | subprocess.Popen(command,shell=True).communicate() |
---|
169 | return self.getPackageVersionAvailable('lliurex-version-stamp',options) |
---|
170 | |
---|
171 | def getPackagesToUpdate(self): |
---|
172 | ''' |
---|
173 | packageInfo definition |
---|
174 | { |
---|
175 | 'PACKAGENAME' : { |
---|
176 | 'install' : 'INSTALLEDVERSION', |
---|
177 | 'candidate' : 'CANDIDATEVERSION', |
---|
178 | 'icon' : 'ICONNAME', |
---|
179 | 'changelog' : 'CHANGELOGTEXT' |
---|
180 | } |
---|
181 | } |
---|
182 | ''' |
---|
183 | packageInfo = {} |
---|
184 | subprocess.Popen("LANG=C apt-get update",shell=True).communicate() |
---|
185 | psimulate = subprocess.Popen('LANG=C apt-get dist-upgrade -sV',shell=True,stdout=subprocess.PIPE) |
---|
186 | rawoutputpsimulate = psimulate.stdout.readlines() |
---|
187 | rawpackagestoinstall = [ aux.strip() for aux in rawoutputpsimulate if aux.startswith('Inst') ] |
---|
188 | r = [ aux.replace('Inst ','') for aux in rawpackagestoinstall ] |
---|
189 | for allinfo in r : |
---|
190 | packageInfo[allinfo.split(' ')[0]] = {} |
---|
191 | packageInfo[allinfo.split(' ')[0]]['raw'] = ' '.join(allinfo.split(' ')[1:]) |
---|
192 | |
---|
193 | for package in packageInfo: |
---|
194 | raw = packageInfo[package]['raw'].split(' ') |
---|
195 | if raw[0].startswith('['): |
---|
196 | packageInfo[package]['install'] = raw[0][1:-1] |
---|
197 | packageInfo[package]['candidate'] = raw[1][1:] |
---|
198 | elif raw[0].startswith('('): |
---|
199 | packageInfo[package]['install'] = None |
---|
200 | packageInfo[package]['candidate'] = raw[0][1:] |
---|
201 | packageInfo[package].pop('raw') |
---|
202 | return packageInfo |
---|
203 | |
---|
204 | if __name__ == '__main__': |
---|
205 | x = LliurexUpCore() |
---|