3 # Copyright (c) 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.
22 from mic.utils import runner, fs_related
25 if not hasattr(zypp, 'PoolQuery') or not hasattr(zypp.RepoManager, 'loadSolvFile'):
26 raise ImportError("python-zypp in host system cannot support PoolQuery or loadSolvFile interface, "
27 "please update it to enhanced version which can be found in repo.meego.com/tools")
30 from mic.kickstart import ksparser
31 from mic.utils import rpmmisc
32 from mic.utils.proxy import get_proxy_for
33 from mic.utils.errors import CreatorError
34 from mic.imager.baseimager import BaseImageCreator
40 self.mirrorlist = None
42 self.proxy_username = None
43 self.proxy_password = None
46 self.autorefresh = True
47 self.keeppackages = True
50 class RepoError(CreatorError):
53 class RpmError(CreatorError):
56 from mic.pluginbase import BackendPlugin
57 class Zypp(BackendPlugin):
60 def __init__(self, creator = None):
61 if not isinstance(creator, BaseImageCreator):
62 raise CreatorError("Invalid argument: creator")
64 self.__pkgs_license = {}
65 self.__pkgs_content = {}
66 self.creator = creator
70 self.repo_manager = None
71 self.repo_manager_options = None
74 self.probFilterFlags = []
78 self.has_prov_query = True
80 def doFileLogSetup(self, uid, logfile):
81 # don't do the file log for the livecd as it can lead to open fds
82 # being left and an inability to clean up after ourself
90 if not os.path.exists("/etc/fedora-release") and not os.path.exists("/etc/meego-release"):
91 for i in range(3, os.sysconf("SC_OPEN_MAX")):
103 def _cleanupRpmdbLocks(self, installroot):
104 # cleans up temporary files left by bdb so that differing
105 # versions of rpm don't cause problems
107 for f in glob.glob(installroot + "/var/lib/rpm/__db*"):
110 def setup(self, confpath, installroot):
111 self._cleanupRpmdbLocks(installroot)
112 self.installroot = installroot
114 def whatObsolete(self, pkg):
115 query = zypp.PoolQuery()
116 query.addKind(zypp.ResKind.package)
117 query.addAttribute(zypp.SolvAttr.obsoletes, pkg)
118 query.setMatchExact()
119 for pi in query.queryResults(self.Z.pool()):
123 def _splitPkgString(self, pkg):
124 sp = pkg.rsplit(".",1)
129 if self.creator.target_arch == None:
130 # TODO, get the default_arch from conf or detected from global settings
131 sysarch = zypp.Arch('i686')
133 sysarch = zypp.Arch(self.creator.target_arch)
134 if not zypp.Arch(arch).compatible_with (sysarch):
139 def selectPackage(self, pkg):
140 """ Select a given package or package pattern, can be specified with name.arch or name* or *name """
142 self.__initialize_zypp()
144 def markPoolItem(obs, pi):
146 pi.status().setToBeInstalled (zypp.ResStatus.USER)
148 obs.status().setToBeInstalled (zypp.ResStatus.USER)
151 startx = pkg.startswith("*")
152 endx = pkg.endswith("*")
153 ispattern = startx or endx
154 name, arch = self._splitPkgString(pkg)
157 q.addKind(zypp.ResKind.package)
159 if startx and not endx:
160 pattern = '%s$' % (pkg[1:])
161 if endx and not startx:
162 pattern = '^%s' % (pkg[0:-1])
164 pattern = '%s' % (pkg[1:-1])
166 q.addAttribute(zypp.SolvAttr.name,pattern)
169 q.addAttribute(zypp.SolvAttr.name,name)
172 q.addAttribute(zypp.SolvAttr.name,pkg)
174 for item in sorted(q.queryResults(self.Z.pool()), key=lambda item: str(item.edition()), reverse=True):
175 if item.name() in self.excpkgs.keys() and self.excpkgs[item.name()] == item.repoInfo().name():
177 if item.name() in self.incpkgs.keys() and self.incpkgs[item.name()] != item.repoInfo().name():
180 obspkg = self.whatObsolete(item.name())
182 if arch == str(item.arch()):
183 item.status().setToBeInstalled (zypp.ResStatus.USER)
185 markPoolItem(obspkg, item)
188 # Can't match using package name, then search from packge provides infomation
189 if found == False and not ispattern:
190 q.addAttribute(zypp.SolvAttr.provides, pkg)
191 q.addAttribute(zypp.SolvAttr.name,'')
192 for item in sorted(q.queryResults(self.Z.pool()), key=lambda item: str(item.edition()), reverse=True):
193 if item.name() in self.excpkgs.keys() and self.excpkgs[item.name()] == item.repoInfo().name():
195 if item.name() in self.incpkgs.keys() and self.incpkgs[item.name()] != item.repoInfo().name():
198 obspkg = self.whatObsolete(item.name())
199 markPoolItem(obspkg, item)
204 raise CreatorError("Unable to find package: %s" % (pkg,))
206 def inDeselectPackages(self, item):
207 """check if specified pacakges are in the list of inDeselectPackages"""
209 for pkg in self.to_deselect:
210 startx = pkg.startswith("*")
211 endx = pkg.endswith("*")
212 ispattern = startx or endx
213 pkgname, pkgarch = self._splitPkgString(pkg)
216 if name == pkgname and str(item.arch()) == pkgarch:
222 if startx and name.endswith(pkg[1:]):
224 if endx and name.startswith(pkg[:-1]):
228 def deselectPackage(self, pkg):
229 """collect packages should not be installed"""
230 self.to_deselect.append(pkg)
232 def selectGroup(self, grp, include = ksparser.GROUP_DEFAULT):
234 self.__initialize_zypp()
237 q.addKind(zypp.ResKind.pattern)
238 for item in q.queryResults(self.Z.pool()):
239 summary = "%s" % item.summary()
240 name = "%s" % item.name()
241 if name == grp or summary == grp:
243 item.status().setToBeInstalled (zypp.ResStatus.USER)
247 if include == ksparser.GROUP_REQUIRED:
248 map(lambda p: self.deselectPackage(p), grp.default_packages.keys())
251 raise CreatorError("Unable to find pattern: %s" % (grp,))
253 def addRepository(self, name, url = None, mirrorlist = None, proxy = None,
254 proxy_username = None, proxy_password = None,
255 inc = None, exc = None, ssl_verify = True, cost=None,
257 # TODO: Handle cost attribute for repos
258 if not self.repo_manager:
259 self.__initialize_repo_manager()
261 repo = RepositoryStub()
265 repo.proxy_username = proxy_username
266 repo.proxy_password = proxy_password
267 repo.ssl_verify = ssl_verify
268 repo.baseurl.append(url)
271 self.incpkgs[pkg] = name
274 self.excpkgs[pkg] = name
276 # check LICENSE files
277 if not rpmmisc.checkRepositoryEULA(name, repo):
278 msger.warning('skip repo:%s for failed EULA confirmation' % name)
282 repo.mirrorlist = mirrorlist
284 # Enable gpg check for verifying corrupt packages
287 repo.priority = priority
288 self.repos.append(repo)
291 repo_info = zypp.RepoInfo()
292 repo_info.setAlias(repo.name)
293 repo_info.setName(repo.name)
294 repo_info.setEnabled(repo.enabled)
295 repo_info.setAutorefresh(repo.autorefresh)
296 repo_info.setKeepPackages(repo.keeppackages)
297 baseurl = zypp.Url(repo.baseurl[0])
299 baseurl.setQueryParam("ssl_verify", "no")
301 (scheme, host, path, parm, query, frag) = urlparse.urlparse(proxy)
302 proxyinfo = host.split(":")
303 baseurl.setQueryParam ("proxy", proxyinfo[0])
305 if len(proxyinfo) > 1:
307 baseurl.setQueryParam ("proxyport", port)
308 repo_info.addBaseUrl(baseurl)
310 repo_info.setPriority(repo.priority)
311 self.repo_manager.addRepository(repo_info)
312 self.__build_repo_cache(name)
313 except RuntimeError, e:
314 raise CreatorError(str(e))
316 msger.verbose('repo: %s was added' % name)
319 def installHasFile(self, file):
322 def runInstall(self, checksize = 0):
323 os.environ["HOME"] = "/"
324 self.buildTransaction()
326 todo = zypp.GetResolvablesToInsDel(self.Z.pool())
327 installed_pkgs = todo._toInstall
329 for item in installed_pkgs:
330 if not zypp.isKindPattern(item) and not self.inDeselectPackages(item):
333 # record the total size of installed pkgs
334 pkgs_total_size = sum(map(lambda x: int(x.installSize()), dlpkgs))
336 # check needed size before actually download and install
337 if checksize and pkgs_total_size > checksize:
338 raise CreatorError("Size of specified root partition in kickstart file is too small to install all selected packages.")
340 # record all pkg and the content
341 localpkgs = self.localpkgs.keys()
344 if pkg.name() in localpkgs:
345 hdr = rpmmisc.readRpmHeader(self.ts, self.localpkgs[pkg.name()])
346 pkg_long_name = "%s-%s-%s.%s.rpm" % (hdr['name'], hdr['version'], hdr['release'], hdr['arch'])
347 license = hdr['license']
349 pkg_long_name = "%s-%s.%s.rpm" % (pkg.name(), pkg.edition(), pkg.arch())
350 package = zypp.asKindPackage(pkg)
351 license = package.license()
352 self.__pkgs_content[pkg_long_name] = {} #TBD: to get file list
353 if license in self.__pkgs_license.keys():
354 self.__pkgs_license[license].append(pkg_long_name)
356 self.__pkgs_license[license] = [pkg_long_name]
358 total_count = len(dlpkgs)
360 localpkgs = self.localpkgs.keys()
361 msger.info("Checking packages cache and packages integrity ...")
363 """ Check if it is cached locally """
364 if po.name() in localpkgs:
367 local = self.getLocalPkgPath(po)
368 if os.path.exists(local):
369 if self.checkPkg(local) != 0:
373 download_count = total_count - cached_count
374 msger.info("%d packages to be installed, %d packages gotten from cache, %d packages to be downloaded" % (total_count, cached_count, download_count))
376 if download_count > 0:
377 msger.info("Downloading packages ...")
378 self.downloadPkgs(dlpkgs, download_count)
379 self.installPkgs(dlpkgs)
382 raise CreatorError("Unable to download from repo : %s" % (e,))
384 raise CreatorError("Unable to install: %s" % (e,))
386 def getAllContent(self):
387 return self.__pkgs_content
389 def getPkgsLicense(self):
390 return self.__pkgs_license
392 def __initialize_repo_manager(self):
393 if self.repo_manager:
396 """ Clean up repo metadata """
397 shutil.rmtree(self.creator.cachedir + "/etc", ignore_errors = True)
398 shutil.rmtree(self.creator.cachedir + "/solv", ignore_errors = True)
399 shutil.rmtree(self.creator.cachedir + "/raw", ignore_errors = True)
401 zypp.KeyRing.setDefaultAccept( zypp.KeyRing.ACCEPT_UNSIGNED_FILE
402 | zypp.KeyRing.ACCEPT_VERIFICATION_FAILED
403 | zypp.KeyRing.ACCEPT_UNKNOWNKEY
404 | zypp.KeyRing.TRUST_KEY_TEMPORARILY
406 self.repo_manager_options = zypp.RepoManagerOptions(zypp.Pathname(self.creator._instroot))
407 self.repo_manager_options.knownReposPath = zypp.Pathname(self.creator.cachedir + "/etc/zypp/repos.d")
408 self.repo_manager_options.repoCachePath = zypp.Pathname(self.creator.cachedir)
409 self.repo_manager_options.repoRawCachePath = zypp.Pathname(self.creator.cachedir + "/raw")
410 self.repo_manager_options.repoSolvCachePath = zypp.Pathname(self.creator.cachedir + "/solv")
411 self.repo_manager_options.repoPackagesCachePath = zypp.Pathname(self.creator.cachedir + "/packages")
413 self.repo_manager = zypp.RepoManager(self.repo_manager_options)
415 def __build_repo_cache(self, name):
416 repo = self.repo_manager.getRepositoryInfo(name)
417 if self.repo_manager.isCached(repo) or not repo.enabled():
419 self.repo_manager.buildCache(repo, zypp.RepoManager.BuildIfNeeded)
421 def __initialize_zypp(self):
425 zconfig = zypp.ZConfig_instance()
427 """ Set system architecture """
428 if self.creator.target_arch:
429 zconfig.setSystemArchitecture(zypp.Arch(self.creator.target_arch))
431 msger.info("zypp architecture is <%s>" % zconfig.systemArchitecture())
433 """ repoPackagesCachePath is corrected by this """
434 self.repo_manager = zypp.RepoManager(self.repo_manager_options)
435 repos = self.repo_manager.knownRepositories()
437 if not repo.enabled():
439 self.repo_manager.loadFromCache(repo)
441 self.Z = zypp.ZYppFactory_instance().getZYpp()
442 self.Z.initializeTarget(zypp.Pathname(self.creator._instroot))
443 self.Z.target().load()
446 def buildTransaction(self):
447 if not self.Z.resolver().resolvePool():
448 msger.warning("Problem count: %d" % len(self.Z.resolver().problems()))
449 for problem in self.Z.resolver().problems():
450 msger.warning("Problem: %s, %s" % (problem.description().decode("utf-8"), problem.details().decode("utf-8")))
452 def getLocalPkgPath(self, po):
453 repoinfo = po.repoInfo()
454 cacheroot = repoinfo.packagesPath()
455 location= zypp.asKindPackage(po).location()
456 rpmpath = str(location.filename())
457 pkgpath = "%s/%s" % (cacheroot, rpmpath)
460 def installLocal(self, pkg, po=None, updateonly=False):
462 self.__initialize_transaction()
463 solvfile = "%s/.solv" % (self.creator.cachedir)
464 rc, out = runner.runtool([fs_related.find_binary_path("rpms2solv"), pkg])
466 f = open(solvfile, "w+")
469 warnmsg = self.repo_manager.loadSolvFile(solvfile , os.path.basename(pkg))
471 msger.warning(warnmsg)
474 msger.warning('Can not get %s solv data.' % pkg)
475 hdr = rpmmisc.readRpmHeader(self.ts, pkg)
476 arch = zypp.Arch(hdr['arch'])
477 if self.creator.target_arch == None:
478 # TODO, get the default_arch from conf or detected from global settings
479 sysarch = zypp.Arch('i686')
481 sysarch = zypp.Arch(self.creator.target_arch)
482 if arch.compatible_with (sysarch):
483 pkgname = hdr['name']
484 self.localpkgs[pkgname] = pkg
485 self.selectPackage(pkgname)
486 msger.info("Marking %s to be installed" % (pkg))
488 msger.warning ("Cannot add package %s to transaction. Not a compatible architecture: %s" % (pkg, hdr['arch']))
490 def downloadPkgs(self, package_objects, count):
491 localpkgs = self.localpkgs.keys()
492 progress_obj = rpmmisc.TextProgress(count)
493 for po in package_objects:
494 if po.name() in localpkgs:
496 filename = self.getLocalPkgPath(po)
497 if os.path.exists(filename):
498 if self.checkPkg(filename) == 0:
500 dirn = os.path.dirname(filename)
501 if not os.path.exists(dirn):
503 baseurl = str(po.repoInfo().baseUrls()[0])
504 index = baseurl.find("?")
506 baseurl = baseurl[:index]
507 proxy = self.get_proxy(po.repoInfo())
510 proxies = {str(proxy.split(":")[0]):str(proxy)}
512 location = zypp.asKindPackage(po).location()
513 location = str(location.filename())
514 if location.startswith("./"):
515 location = location[2:]
516 url = baseurl + "/%s" % location
518 filename = rpmmisc.myurlgrab(url, filename, proxies, progress_obj)
523 def installPkgs(self, package_objects):
525 self.__initialize_transaction()
529 for flag in self.probFilterFlags:
531 self.ts.setProbFilter(probfilter)
533 localpkgs = self.localpkgs.keys()
534 for po in package_objects:
536 if pkgname in localpkgs:
537 rpmpath = self.localpkgs[pkgname]
539 rpmpath = self.getLocalPkgPath(po)
540 if not os.path.exists(rpmpath):
541 """ Maybe it is a local repo """
542 baseurl = str(po.repoInfo().baseUrls()[0])
543 baseurl = baseurl.strip()
544 location = zypp.asKindPackage(po).location()
545 location = str(location.filename())
546 if baseurl.startswith("file:/"):
547 rpmpath = baseurl[5:] + "/%s" % (location)
548 if not os.path.exists(rpmpath):
549 raise RpmError("Error: %s doesn't exist" % rpmpath)
550 h = rpmmisc.readRpmHeader(self.ts, rpmpath)
551 self.ts.addInstall(h, rpmpath, 'u')
553 unresolved_dependencies = self.ts.check()
554 if not unresolved_dependencies:
556 cb = rpmmisc.RPMInstallCallback(self.ts)
557 installlogfile = "%s/__catched_stderr.buf" % (self.creator._instroot)
558 msger.enable_logstderr(installlogfile)
559 errors = self.ts.run(cb.callback, '')
562 elif len(errors) == 0:
563 msger.warning('scriptlet or other non-fatal errors occurred during transaction.')
567 msger.error('Could not run transaction.')
568 msger.disable_logstderr()
573 for pkg, need, needflags, sense, key in unresolved_dependencies:
574 package = '-'.join(pkg)
575 if needflags == rpm.RPMSENSE_LESS:
576 deppkg = ' < '.join(need)
577 elif needflags == rpm.RPMSENSE_EQUAL:
578 deppkg = ' = '.join(need)
579 elif needflags == rpm.RPMSENSE_GREATER:
580 deppkg = ' > '.join(need)
582 deppkg = '-'.join(need)
584 if sense == rpm.RPMDEP_SENSE_REQUIRES:
585 msger.warning ("[%s] Requires [%s], which is not provided" % (package, deppkg))
586 elif sense == rpm.RPMDEP_SENSE_CONFLICTS:
587 msger.warning ("[%s] Conflicts with [%s]" % (package, deppkg))
589 raise RepoError("Unresolved dependencies, transaction failed.")
591 def __initialize_transaction(self):
593 self.ts = rpm.TransactionSet(self.creator._instroot)
594 # Set to not verify DSA signatures.
595 self.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS)
597 def checkPkg(self, pkg):
599 if not os.path.exists(pkg):
601 ret = rpmmisc.checkRpmIntegrity('rpm', pkg)
603 msger.warning("package %s is damaged: %s" % (os.path.basename(pkg), pkg))
607 def _add_prob_flags(self, *flags):
609 if flag not in self.probFilterFlags:
610 self.probFilterFlags.append(flag)
612 def get_proxy(self, repoinfo):
614 reponame = "%s" % repoinfo.name()
615 for repo in self.repos:
616 if repo.name == reponame:
623 repourl = str(repoinfo.baseUrls()[0])
624 return get_proxy_for(repourl)