3 # Copyright (c) 2008, 2009, 2010, 2011 Intel, Inc.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 from mic.utils.errors import CreatorError
25 from mic.utils.proxy import get_proxy_for
26 from mic.utils import runner
29 class RPMInstallCallback:
30 """ Command line callback class for callbacks from the RPM library.
33 def __init__(self, ts, output=1):
35 self.callbackfilehandles = {}
36 self.total_actions = 0
37 self.total_installed = 0
38 self.total_installing = 0
39 self.installed_pkg_names = []
40 self.total_removed = 0
44 self.tsInfo = None # this needs to be set for anything else to work
48 self.headmsg = "Installing"
50 def _dopkgtup(self, hdr):
51 tmpepoch = hdr['epoch']
52 if tmpepoch is None: epoch = '0'
53 else: epoch = str(tmpepoch)
55 return (hdr['name'], hdr['arch'], epoch, hdr['version'], hdr['release'])
57 def _makeHandle(self, hdr):
58 handle = '%s:%s.%s-%s-%s' % (hdr['epoch'], hdr['name'], hdr['version'],
59 hdr['release'], hdr['arch'])
63 def _localprint(self, msg):
67 def _makefmt(self, percent, progress = True):
68 l = len(str(self.total_actions))
69 size = "%s.%s" % (l, l)
70 fmt_done = "[%" + size + "s/%" + size + "s]"
71 done = fmt_done % (self.total_installing,
73 marks = self.marks - (2 * l)
74 width = "%s.%s" % (marks, marks)
75 fmt_bar = "%-" + width + "s"
77 bar = fmt_bar % (self.mark * int(marks * (percent / 100.0)), )
78 fmt = "%-10.10s: %-50.50s " + bar + " " + done
80 bar = fmt_bar % (self.mark * marks, )
81 fmt = "%-10.10s: %-50.50s " + bar + " " + done
84 def _logPkgString(self, hdr):
85 """return nice representation of the package for the log"""
86 (n,a,e,v,r) = self._dopkgtup(hdr)
88 pkg = '%s.%s %s-%s' % (n, a, v, r)
90 pkg = '%s.%s %s:%s-%s' % (n, a, e, v, r)
94 def callback(self, what, bytes, total, h, user):
95 if what == rpm.RPMCALLBACK_TRANS_START:
97 self.total_actions = total
99 elif what == rpm.RPMCALLBACK_TRANS_PROGRESS:
102 elif what == rpm.RPMCALLBACK_TRANS_STOP:
105 elif what == rpm.RPMCALLBACK_INST_OPEN_FILE:
113 hdr = readRpmHeader(self.ts, h)
115 m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc))
119 pkgname = os.path.basename(rpmloc)
120 msger.info("Next install: %s " % pkgname)
122 handle = self._makeHandle(hdr)
123 fd = os.open(rpmloc, os.O_RDONLY)
124 self.callbackfilehandles[handle]=fd
125 if hdr['name'] not in self.installed_pkg_names:
126 self.installed_pkg_names.append(hdr['name'])
127 self.total_installed += 1
130 self._localprint("No header - huh?")
132 elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE:
139 hdr = readRpmHeader(self.ts, h)
141 handle = self._makeHandle(hdr)
142 os.close(self.callbackfilehandles[handle])
146 #pkgtup = self._dopkgtup(hdr)
147 self.logString.append(self._logPkgString(hdr))
149 elif what == rpm.RPMCALLBACK_INST_START:
150 self.total_installing += 1
152 elif what == rpm.RPMCALLBACK_UNINST_STOP:
155 elif what == rpm.RPMCALLBACK_INST_PROGRESS:
157 percent = (self.total_installed*100)/self.total_actions
164 m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc))
168 pkgname = os.path.basename(rpmloc)
170 fmt = self._makefmt(percent)
171 msg = fmt % (self.headmsg, pkgname)
172 if msg != self.lastmsg:
177 if self.total_installed == self.total_actions:
179 msger.verbose('\n'.join(self.logString))
181 elif what == rpm.RPMCALLBACK_UNINST_START:
184 elif what == rpm.RPMCALLBACK_UNINST_PROGRESS:
187 elif what == rpm.RPMCALLBACK_UNINST_STOP:
188 self.total_removed += 1
190 elif what == rpm.RPMCALLBACK_REPACKAGE_START:
193 elif what == rpm.RPMCALLBACK_REPACKAGE_STOP:
196 elif what == rpm.RPMCALLBACK_REPACKAGE_PROGRESS:
198 elif what == rpm.RPMCALLBACK_SCRIPT_ERROR:
205 m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc))
209 pkgname = os.path.basename(rpmloc)
211 msger.warning('(%s) Post script failed' % pkgname)
213 def readRpmHeader(ts, filename):
214 """ Read an rpm header. """
216 fd = os.open(filename, os.O_RDONLY)
217 h = ts.hdrFromFdno(fd)
221 def splitFilename(filename):
222 """ Pass in a standard style rpm fullname
224 Return a name, version, release, epoch, arch, e.g.::
225 foo-1.0-1.i386.rpm returns foo, 1.0, 1, i386
226 1:bar-9-123a.ia64.rpm returns bar, 9, 123a, 1, ia64
229 if filename[-4:] == '.rpm':
230 filename = filename[:-4]
232 archIndex = filename.rfind('.')
233 arch = filename[archIndex+1:]
235 relIndex = filename[:archIndex].rfind('-')
236 rel = filename[relIndex+1:archIndex]
238 verIndex = filename[:relIndex].rfind('-')
239 ver = filename[verIndex+1:relIndex]
241 epochIndex = filename.find(':')
245 epoch = filename[:epochIndex]
247 name = filename[epochIndex + 1:verIndex]
248 return name, ver, rel, epoch, arch
250 def getCanonX86Arch(arch):
253 f = open("/proc/cpuinfo", "r")
254 lines = f.readlines()
257 if line.startswith("model name") and line.find("Geode(TM)") != -1:
260 # only athlon vs i686 isn't handled with uname currently
264 # if we're i686 and AuthenticAMD, then we should be an athlon
265 f = open("/proc/cpuinfo", "r")
266 lines = f.readlines()
269 if line.startswith("vendor") and line.find("AuthenticAMD") != -1:
271 # i686 doesn't guarantee cmov, but we depend on it
272 elif line.startswith("flags") and line.find("cmov") == -1:
277 def getCanonX86_64Arch(arch):
282 f = open("/proc/cpuinfo", "r")
283 lines = f.readlines()
286 if line.startswith("vendor_id"):
287 vendor = line.split(':')[1]
292 if vendor.find("Authentic AMD") != -1 or vendor.find("AuthenticAMD") != -1:
294 if vendor.find("GenuineIntel") != -1:
301 if (len(arch) == 4 and arch[0] == "i" and arch[2:4] == "86"):
302 return getCanonX86Arch(arch)
305 return getCanonX86_64Arch(arch)
309 # Copy from libsatsolver:poolarch.c, with cleanup
311 "x86_64": "x86_64:i686:i586:i486:i386",
312 "i686": "i686:i586:i486:i386",
313 "i586": "i586:i486:i386",
314 "ia64": "ia64:i686:i586:i486:i386",
315 "aarch64": "aarch64",
316 "armv7tnhl": "armv7tnhl:armv7thl:armv7nhl:armv7hl",
317 "armv7thl": "armv7thl:armv7hl",
318 "armv7nhl": "armv7nhl:armv7hl",
319 "armv7hl": "armv7hl",
320 "armv7l": "armv7l:armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
321 "armv6l": "armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
322 "armv5tejl": "armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
323 "armv5tel": "armv5tel:armv5l:armv4tl:armv4l:armv3l",
324 "armv5l": "armv5l:armv4tl:armv4l:armv3l",
328 # dict mapping arch -> ( multicompat, best personality, biarch personality )
330 "x86_64": ( "athlon", "x86_64", "athlon" ),
349 "armv7tnhl": "armv7nhl",
350 "armv7nhl": "armv7hl",
353 "armv6l": "armv5tejl",
354 "armv5tejl": "armv5tel",
355 "armv5tel": "noarch",
363 def isMultiLibArch(arch=None):
364 """returns true if arch is a multilib arch, false if not"""
366 arch = getCanonArch()
368 if not arches.has_key(arch): # or we could check if it is noarch
371 if multilibArches.has_key(arch):
374 if multilibArches.has_key(arches[arch]):
380 myarch = getCanonArch()
381 if not arches.has_key(myarch):
384 if isMultiLibArch(arch=myarch):
385 if multilibArches.has_key(myarch):
388 return arches[myarch]
390 if arches.has_key(myarch):
392 value = arches[basearch]
393 while value != 'noarch':
395 value = arches[basearch]
399 def checkRpmIntegrity(bin_rpm, package):
400 return runner.quiet([bin_rpm, "-K", "--nosignature", package])
402 def checkSig(ts, package):
403 """ Takes a transaction set and a package, check it's sigs,
404 return 0 if they are all fine
405 return 1 if the gpg key can't be found
406 return 2 if the header is in someway damaged
407 return 3 if the key is not trusted
408 return 4 if the pkg is not gpg or pgp signed
412 currentflags = ts.setVSFlags(0)
413 fdno = os.open(package, os.O_RDONLY)
415 hdr = ts.hdrFromFdno(fdno)
417 except rpm.error as e:
418 if str(e) == "public key not availaiable":
420 if str(e) == "public key not available":
422 if str(e) == "public key not trusted":
424 if str(e) == "error reading package header":
427 error, siginfo = getSigInfo(hdr)
440 ts.setVSFlags(currentflags) # put things back like they were before
444 """ checks signature from an hdr hand back signature information and/or
449 locale.setlocale(locale.LC_ALL, 'C')
451 string = '%|DSAHEADER?{%{DSAHEADER:pgpsig}}:{%|RSAHEADER?{%{RSAHEADER:pgpsig}}:{%|SIGGPG?{%{SIGGPG:pgpsig}}:{%|SIGPGP?{%{SIGPGP:pgpsig}}:{(none)}|}|}|}|'
452 siginfo = hdr.sprintf(string)
453 if siginfo != '(none)':
455 sigtype, sigdate, sigid = siginfo.split(',')
462 infotuple = (sigtype, sigdate, sigid)
463 return error, infotuple