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 handle = self._makeHandle(hdr)
116 fd = os.open(rpmloc, os.O_RDONLY)
117 self.callbackfilehandles[handle]=fd
118 if hdr['name'] not in self.installed_pkg_names:
119 self.installed_pkg_names.append(hdr['name'])
120 self.total_installed += 1
123 self._localprint("No header - huh?")
125 elif what == rpm.RPMCALLBACK_INST_CLOSE_FILE:
132 hdr = readRpmHeader(self.ts, h)
134 handle = self._makeHandle(hdr)
135 os.close(self.callbackfilehandles[handle])
139 #pkgtup = self._dopkgtup(hdr)
140 self.logString.append(self._logPkgString(hdr))
142 elif what == rpm.RPMCALLBACK_INST_START:
143 self.total_installing += 1
145 elif what == rpm.RPMCALLBACK_UNINST_STOP:
148 elif what == rpm.RPMCALLBACK_INST_PROGRESS:
150 percent = (self.total_installed*100L)/self.total_actions
157 m = re.match("(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc))
161 pkgname = os.path.basename(rpmloc)
163 fmt = self._makefmt(percent)
164 msg = fmt % (self.headmsg, pkgname)
165 if msg != self.lastmsg:
170 if self.total_installed == self.total_actions:
172 msger.verbose('\n'.join(self.logString))
174 elif what == rpm.RPMCALLBACK_UNINST_START:
177 elif what == rpm.RPMCALLBACK_UNINST_PROGRESS:
180 elif what == rpm.RPMCALLBACK_UNINST_STOP:
181 self.total_removed += 1
183 elif what == rpm.RPMCALLBACK_REPACKAGE_START:
186 elif what == rpm.RPMCALLBACK_REPACKAGE_STOP:
189 elif what == rpm.RPMCALLBACK_REPACKAGE_PROGRESS:
192 def readRpmHeader(ts, filename):
193 """ Read an rpm header. """
195 fd = os.open(filename, os.O_RDONLY)
196 h = ts.hdrFromFdno(fd)
200 def splitFilename(filename):
201 """ Pass in a standard style rpm fullname
203 Return a name, version, release, epoch, arch, e.g.::
204 foo-1.0-1.i386.rpm returns foo, 1.0, 1, i386
205 1:bar-9-123a.ia64.rpm returns bar, 9, 123a, 1, ia64
208 if filename[-4:] == '.rpm':
209 filename = filename[:-4]
211 archIndex = filename.rfind('.')
212 arch = filename[archIndex+1:]
214 relIndex = filename[:archIndex].rfind('-')
215 rel = filename[relIndex+1:archIndex]
217 verIndex = filename[:relIndex].rfind('-')
218 ver = filename[verIndex+1:relIndex]
220 epochIndex = filename.find(':')
224 epoch = filename[:epochIndex]
226 name = filename[epochIndex + 1:verIndex]
227 return name, ver, rel, epoch, arch
229 def getCanonX86Arch(arch):
232 f = open("/proc/cpuinfo", "r")
233 lines = f.readlines()
236 if line.startswith("model name") and line.find("Geode(TM)") != -1:
239 # only athlon vs i686 isn't handled with uname currently
243 # if we're i686 and AuthenticAMD, then we should be an athlon
244 f = open("/proc/cpuinfo", "r")
245 lines = f.readlines()
248 if line.startswith("vendor") and line.find("AuthenticAMD") != -1:
250 # i686 doesn't guarantee cmov, but we depend on it
251 elif line.startswith("flags") and line.find("cmov") == -1:
256 def getCanonX86_64Arch(arch):
261 f = open("/proc/cpuinfo", "r")
262 lines = f.readlines()
265 if line.startswith("vendor_id"):
266 vendor = line.split(':')[1]
271 if vendor.find("Authentic AMD") != -1 or vendor.find("AuthenticAMD") != -1:
273 if vendor.find("GenuineIntel") != -1:
280 if (len(arch) == 4 and arch[0] == "i" and arch[2:4] == "86"):
281 return getCanonX86Arch(arch)
284 return getCanonX86_64Arch(arch)
288 # Copy from libsatsolver:poolarch.c, with cleanup
290 "x86_64": "x86_64:i686:i586:i486:i386",
291 "i686": "i686:i586:i486:i386",
292 "i586": "i586:i486:i386",
293 "ia64": "ia64:i686:i586:i486:i386",
294 "aarch64": "aarch64",
295 "armv7tnhl": "armv7tnhl:armv7thl:armv7nhl:armv7hl",
296 "armv7thl": "armv7thl:armv7hl",
297 "armv7nhl": "armv7nhl:armv7hl",
298 "armv7hl": "armv7hl",
299 "armv7l": "armv7l:armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
300 "armv6l": "armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
301 "armv5tejl": "armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
302 "armv5tel": "armv5tel:armv5l:armv4tl:armv4l:armv3l",
303 "armv5l": "armv5l:armv4tl:armv4l:armv3l",
307 # dict mapping arch -> ( multicompat, best personality, biarch personality )
309 "x86_64": ( "athlon", "x86_64", "athlon" ),
328 "armv7tnhl": "armv7nhl",
329 "armv7nhl": "armv7hl",
332 "armv6l": "armv5tejl",
333 "armv5tejl": "armv5tel",
334 "armv5tel": "noarch",
342 def isMultiLibArch(arch=None):
343 """returns true if arch is a multilib arch, false if not"""
345 arch = getCanonArch()
347 if not arches.has_key(arch): # or we could check if it is noarch
350 if multilibArches.has_key(arch):
353 if multilibArches.has_key(arches[arch]):
359 myarch = getCanonArch()
360 if not arches.has_key(myarch):
363 if isMultiLibArch(arch=myarch):
364 if multilibArches.has_key(myarch):
367 return arches[myarch]
369 if arches.has_key(myarch):
371 value = arches[basearch]
372 while value != 'noarch':
374 value = arches[basearch]
378 def checkRpmIntegrity(bin_rpm, package):
379 return runner.quiet([bin_rpm, "-K", "--nosignature", package])
381 def checkSig(ts, package):
382 """ Takes a transaction set and a package, check it's sigs,
383 return 0 if they are all fine
384 return 1 if the gpg key can't be found
385 return 2 if the header is in someway damaged
386 return 3 if the key is not trusted
387 return 4 if the pkg is not gpg or pgp signed
391 currentflags = ts.setVSFlags(0)
392 fdno = os.open(package, os.O_RDONLY)
394 hdr = ts.hdrFromFdno(fdno)
397 if str(e) == "public key not availaiable":
399 if str(e) == "public key not available":
401 if str(e) == "public key not trusted":
403 if str(e) == "error reading package header":
406 error, siginfo = getSigInfo(hdr)
419 ts.setVSFlags(currentflags) # put things back like they were before
423 """ checks signature from an hdr hand back signature information and/or
428 locale.setlocale(locale.LC_ALL, 'C')
430 string = '%|DSAHEADER?{%{DSAHEADER:pgpsig}}:{%|RSAHEADER?{%{RSAHEADER:pgpsig}}:{%|SIGGPG?{%{SIGGPG:pgpsig}}:{%|SIGPGP?{%{SIGPGP:pgpsig}}:{(none)}|}|}|}|'
431 siginfo = hdr.sprintf(string)
432 if siginfo != '(none)':
434 sigtype, sigdate, sigid = siginfo.split(',')
441 infotuple = (sigtype, sigdate, sigid)
442 return error, infotuple