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.
24 if not hasattr(zypp, 'PoolQuery') or \
25 not hasattr(zypp.RepoManager, 'loadSolvFile'):
26 raise ImportError("python-zypp in host system cannot support PoolQuery or "
27 "loadSolvFile interface, please update it to enhanced "
28 "version which can be found in download.tizen.org/tools")
31 from mic.kickstart import ksparser
32 from mic.utils import misc, rpmmisc, runner, fs_related
33 from mic.utils.grabber import myurlgrab, TextProgress
34 from mic.utils.proxy import get_proxy_for
35 from mic.utils.errors import CreatorError, RepoError, RpmError
36 from mic.imager.baseimager import BaseImageCreator
42 self.mirrorlist = None
44 self.proxy_username = None
45 self.proxy_password = None
48 self.autorefresh = True
49 self.keeppackages = True
52 from mic.pluginbase import BackendPlugin
53 class Zypp(BackendPlugin):
56 def __init__(self, target_arch, instroot, cachedir):
57 self.cachedir = cachedir
58 self.instroot = instroot
59 self.target_arch = target_arch
61 self.__pkgs_license = {}
62 self.__pkgs_content = {}
66 self.repo_manager = None
67 self.repo_manager_options = None
74 self.probFilterFlags = [ rpm.RPMPROB_FILTER_OLDPACKAGE,
75 rpm.RPMPROB_FILTER_REPLACEPKG ]
77 self.has_prov_query = True
79 def doFileLogSetup(self, uid, logfile):
80 # don't do the file log for the livecd as it can lead to open fds
81 # being left and an inability to clean up after ourself
98 if not os.path.exists("/etc/fedora-release") and \
99 not os.path.exists("/etc/meego-release"):
100 for i in range(3, os.sysconf("SC_OPEN_MAX")):
109 def _cleanupRpmdbLocks(self, installroot):
110 # cleans up temporary files left by bdb so that differing
111 # versions of rpm don't cause problems
113 for f in glob.glob(installroot + "/var/lib/rpm/__db*"):
117 self._cleanupRpmdbLocks(self.instroot)
119 def whatObsolete(self, pkg):
120 query = zypp.PoolQuery()
121 query.addKind(zypp.ResKind.package)
122 query.addAttribute(zypp.SolvAttr.obsoletes, pkg)
123 query.setMatchExact()
124 for pi in query.queryResults(self.Z.pool()):
128 def _splitPkgString(self, pkg):
129 sp = pkg.rsplit(".",1)
134 sysarch = zypp.Arch(self.target_arch)
135 if not zypp.Arch(arch).compatible_with (sysarch):
140 def selectPackage(self, pkg):
141 """Select a given package or package pattern, can be specified
142 with name.arch or name* or *name
146 self.__initialize_zypp()
148 def markPoolItem(obs, pi):
150 pi.status().setToBeInstalled (zypp.ResStatus.USER)
152 obs.status().setToBeInstalled (zypp.ResStatus.USER)
157 if str(a1) != str(a2):
158 if a1.compatible_with(a2):
164 (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()])
165 (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()])
166 return rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
169 startx = pkg.startswith("*")
170 endx = pkg.endswith("*")
171 ispattern = startx or endx
172 name, arch = self._splitPkgString(pkg)
175 q.addKind(zypp.ResKind.package)
178 if startx and not endx:
179 pattern = '%s$' % (pkg[1:])
180 if endx and not startx:
181 pattern = '^%s' % (pkg[0:-1])
183 pattern = '%s' % (pkg[1:-1])
185 q.addAttribute(zypp.SolvAttr.name,pattern)
189 q.addAttribute(zypp.SolvAttr.name,name)
193 q.addAttribute(zypp.SolvAttr.name,pkg)
196 q.queryResults(self.Z.pool()),
197 cmp=lambda x,y: cmpEVR(x, y),
200 if item.name() in self.excpkgs.keys() and \
201 self.excpkgs[item.name()] == item.repoInfo().name():
203 if item.name() in self.incpkgs.keys() and \
204 self.incpkgs[item.name()] != item.repoInfo().name():
208 obspkg = self.whatObsolete(item.name())
210 if arch == str(item.arch()):
211 item.status().setToBeInstalled (zypp.ResStatus.USER)
213 markPoolItem(obspkg, item)
217 # Can't match using package name, then search from packge
218 # provides infomation
219 if found == False and not ispattern:
220 q.addAttribute(zypp.SolvAttr.provides, pkg)
221 q.addAttribute(zypp.SolvAttr.name,'')
224 q.queryResults(self.Z.pool()),
225 cmp=lambda x,y: cmpEVR(x.edition(), y.edition()),
227 if item.name() in self.excpkgs.keys() and \
228 self.excpkgs[item.name()] == item.repoInfo().name():
230 if item.name() in self.incpkgs.keys() and \
231 self.incpkgs[item.name()] != item.repoInfo().name():
235 obspkg = self.whatObsolete(item.name())
236 markPoolItem(obspkg, item)
242 raise CreatorError("Unable to find package: %s" % (pkg,))
244 def inDeselectPackages(self, item):
245 """check if specified pacakges are in the list of inDeselectPackages
249 for pkg in self.to_deselect:
250 startx = pkg.startswith("*")
251 endx = pkg.endswith("*")
252 ispattern = startx or endx
253 pkgname, pkgarch = self._splitPkgString(pkg)
256 if name == pkgname and str(item.arch()) == pkgarch:
262 if startx and name.endswith(pkg[1:]):
264 if endx and name.startswith(pkg[:-1]):
269 def deselectPackage(self, pkg):
270 """collect packages should not be installed"""
271 self.to_deselect.append(pkg)
273 def selectGroup(self, grp, include = ksparser.GROUP_DEFAULT):
275 self.__initialize_zypp()
278 q.addKind(zypp.ResKind.pattern)
279 for item in q.queryResults(self.Z.pool()):
280 summary = "%s" % item.summary()
281 name = "%s" % item.name()
282 if name == grp or summary == grp:
284 item.status().setToBeInstalled (zypp.ResStatus.USER)
288 if include == ksparser.GROUP_REQUIRED:
290 lambda p: self.deselectPackage(p),
291 grp.default_packages.keys())
295 raise CreatorError("Unable to find pattern: %s" % (grp,))
297 def addRepository(self, name,
301 proxy_username = None,
302 proxy_password = None,
308 # TODO: Handle cost attribute for repos
310 if not self.repo_manager:
311 self.__initialize_repo_manager()
313 if not proxy and url:
314 proxy = get_proxy_for(url)
316 repo = RepositoryStub()
320 repo.proxy_username = proxy_username
321 repo.proxy_password = proxy_password
322 repo.ssl_verify = ssl_verify
323 repo.baseurl.append(url)
326 self.incpkgs[pkg] = name
329 self.excpkgs[pkg] = name
331 # check LICENSE files
332 if not rpmmisc.checkRepositoryEULA(name, repo):
333 msger.warning('skip repo:%s for failed EULA confirmation' % name)
337 repo.mirrorlist = mirrorlist
339 # Enable gpg check for verifying corrupt packages
342 repo.priority = priority
345 repo_info = zypp.RepoInfo()
346 repo_info.setAlias(repo.name)
347 repo_info.setName(repo.name)
348 repo_info.setEnabled(repo.enabled)
349 repo_info.setAutorefresh(repo.autorefresh)
350 repo_info.setKeepPackages(repo.keeppackages)
351 baseurl = zypp.Url(repo.baseurl[0])
353 baseurl.setQueryParam("ssl_verify", "no")
355 scheme, host, path, parm, query, frag = urlparse.urlparse(proxy)
357 proxyinfo = host.split(":")
361 if len(proxyinfo) > 1:
364 if proxy.startswith("socks") and len(proxy.rsplit(':', 1)) == 2:
365 host = proxy.rsplit(':', 1)[0]
366 port = proxy.rsplit(':', 1)[1]
368 baseurl.setQueryParam ("proxy", host)
369 baseurl.setQueryParam ("proxyport", port)
371 repo.baseurl[0] = baseurl.asCompleteString()
372 self.repos.append(repo)
374 repo_info.addBaseUrl(baseurl)
377 repo_info.setPriority(repo.priority)
379 self.repo_manager.addRepository(repo_info)
381 self.__build_repo_cache(name)
383 except RuntimeError, e:
384 raise CreatorError(str(e))
386 msger.verbose('repo: %s was added' % name)
389 def installHasFile(self, file):
392 def preInstall(self, pkg):
393 self.pre_pkgs.append(pkg)
395 def runInstall(self, checksize = 0):
396 os.environ["HOME"] = "/"
397 os.environ["LD_PRELOAD"] = ""
398 self.buildTransaction()
400 todo = zypp.GetResolvablesToInsDel(self.Z.pool())
401 installed_pkgs = todo._toInstall
403 for item in installed_pkgs:
404 if not zypp.isKindPattern(item) and \
405 not self.inDeselectPackages(item):
408 # record all pkg and the content
409 localpkgs = self.localpkgs.keys()
412 if pkg.name() in localpkgs:
413 hdr = rpmmisc.readRpmHeader(self.ts, self.localpkgs[pkg.name()])
414 pkg_long_name = misc.RPM_FMT % {
417 'ver_rel': '%s-%s' % (hdr['version'],
420 license = hdr['license']
423 pkg_long_name = misc.RPM_FMT % {
426 'ver_rel': pkg.edition(),
429 package = zypp.asKindPackage(pkg)
430 license = package.license()
432 self.__pkgs_content[pkg_long_name] = {} #TBD: to get file list
434 if license in self.__pkgs_license.keys():
435 self.__pkgs_license[license].append(pkg_long_name)
437 self.__pkgs_license[license] = [pkg_long_name]
439 total_count = len(dlpkgs)
441 download_total_size = sum(map(lambda x: int(x.downloadSize()), dlpkgs))
442 localpkgs = self.localpkgs.keys()
444 msger.info("Checking packages cache and packages integrity ...")
446 # Check if it is cached locally
447 if po.name() in localpkgs:
450 local = self.getLocalPkgPath(po)
451 if os.path.exists(local):
452 if self.checkPkg(local) != 0:
455 download_total_size -= int(po.downloadSize())
457 cache_avail_size = misc.get_filesystem_avail(self.cachedir)
458 if cache_avail_size < download_total_size:
459 raise CreatorError("No enough space used for downloading.")
461 # record the total size of installed pkgs
462 install_total_size = sum(map(lambda x: int(x.installSize()), dlpkgs))
463 # check needed size before actually download and install
465 # FIXME: for multiple partitions for loop type, check fails
466 # skip the check temporarily
467 #if checksize and install_total_size > checksize:
468 # raise CreatorError("No enough space used for installing, "
469 # "please resize partition size in ks file")
471 download_count = total_count - cached_count
472 msger.info("%d packages to be installed, "
473 "%d packages gotten from cache, "
474 "%d packages to be downloaded" \
475 % (total_count, cached_count, download_count))
478 if download_count > 0:
479 msger.info("Downloading packages ...")
480 self.downloadPkgs(dlpkgs, download_count)
482 self.installPkgs(dlpkgs)
484 except (RepoError, RpmError):
487 raise CreatorError("Package installation failed: %s" % (e,))
489 def getAllContent(self):
490 return self.__pkgs_content
492 def getPkgsLicense(self):
493 return self.__pkgs_license
495 def getFilelist(self, pkgname):
500 self.__initialize_transaction()
502 mi = self.ts.dbMatch('name', pkgname)
504 return header['FILENAMES']
506 def __initialize_repo_manager(self):
507 if self.repo_manager:
510 # Clean up repo metadata
511 shutil.rmtree(self.cachedir + "/etc", ignore_errors = True)
512 shutil.rmtree(self.cachedir + "/solv", ignore_errors = True)
513 shutil.rmtree(self.cachedir + "/raw", ignore_errors = True)
515 zypp.KeyRing.setDefaultAccept( zypp.KeyRing.ACCEPT_UNSIGNED_FILE
516 | zypp.KeyRing.ACCEPT_VERIFICATION_FAILED
517 | zypp.KeyRing.ACCEPT_UNKNOWNKEY
518 | zypp.KeyRing.TRUST_KEY_TEMPORARILY
521 self.repo_manager_options = \
522 zypp.RepoManagerOptions(zypp.Pathname(self.instroot))
524 self.repo_manager_options.knownReposPath = \
525 zypp.Pathname(self.cachedir + "/etc/zypp/repos.d")
527 self.repo_manager_options.repoCachePath = \
528 zypp.Pathname(self.cachedir)
530 self.repo_manager_options.repoRawCachePath = \
531 zypp.Pathname(self.cachedir + "/raw")
533 self.repo_manager_options.repoSolvCachePath = \
534 zypp.Pathname(self.cachedir + "/solv")
536 self.repo_manager_options.repoPackagesCachePath = \
537 zypp.Pathname(self.cachedir + "/packages")
539 self.repo_manager = zypp.RepoManager(self.repo_manager_options)
541 def __build_repo_cache(self, name):
542 repo = self.repo_manager.getRepositoryInfo(name)
543 if self.repo_manager.isCached(repo) or not repo.enabled():
546 msger.info('Refreshing repository: %s ...' % name)
547 self.repo_manager.buildCache(repo, zypp.RepoManager.BuildIfNeeded)
549 def __initialize_zypp(self):
553 zconfig = zypp.ZConfig_instance()
555 # Set system architecture
557 zconfig.setSystemArchitecture(zypp.Arch(self.target_arch))
559 msger.info("zypp architecture is <%s>" % zconfig.systemArchitecture())
561 # repoPackagesCachePath is corrected by this
562 self.repo_manager = zypp.RepoManager(self.repo_manager_options)
563 repos = self.repo_manager.knownRepositories()
565 if not repo.enabled():
567 self.repo_manager.loadFromCache(repo)
569 self.Z = zypp.ZYppFactory_instance().getZYpp()
570 self.Z.initializeTarget(zypp.Pathname(self.instroot))
571 self.Z.target().load()
573 def buildTransaction(self):
574 if not self.Z.resolver().resolvePool():
575 probs = self.Z.resolver().problems()
577 for problem in probs:
578 msger.warning("repo problem: %s, %s" \
579 % (problem.description().decode("utf-8"),
580 problem.details().decode("utf-8")))
582 raise RepoError("found %d resolver problem, abort!" \
585 def getLocalPkgPath(self, po):
586 repoinfo = po.repoInfo()
587 cacheroot = repoinfo.packagesPath()
588 location= zypp.asKindPackage(po).location()
589 rpmpath = str(location.filename())
590 pkgpath = "%s/%s" % (cacheroot, os.path.basename(rpmpath))
593 def installLocal(self, pkg, po=None, updateonly=False):
595 self.__initialize_transaction()
597 solvfile = "%s/.solv" % (self.cachedir)
599 rc, out = runner.runtool([fs_related.find_binary_path("rpms2solv"),
602 f = open(solvfile, "w+")
606 warnmsg = self.repo_manager.loadSolvFile(solvfile,
607 os.path.basename(pkg))
609 msger.warning(warnmsg)
613 msger.warning('Can not get %s solv data.' % pkg)
615 hdr = rpmmisc.readRpmHeader(self.ts, pkg)
616 arch = zypp.Arch(hdr['arch'])
617 sysarch = zypp.Arch(self.target_arch)
619 if arch.compatible_with (sysarch):
620 pkgname = hdr['name']
621 self.localpkgs[pkgname] = pkg
622 self.selectPackage(pkgname)
623 msger.info("Marking %s to be installed" % (pkg))
626 msger.warning("Cannot add package %s to transaction. "
627 "Not a compatible architecture: %s" \
628 % (pkg, hdr['arch']))
630 def downloadPkgs(self, package_objects, count):
631 localpkgs = self.localpkgs.keys()
632 progress_obj = TextProgress(count)
634 for po in package_objects:
635 if po.name() in localpkgs:
638 filename = self.getLocalPkgPath(po)
639 if os.path.exists(filename):
640 if self.checkPkg(filename) == 0:
643 dirn = os.path.dirname(filename)
644 if not os.path.exists(dirn):
647 url = self.get_url(po)
648 proxies = self.get_proxies(po)
651 filename = myurlgrab(url, filename, proxies, progress_obj)
656 def preinstallPkgs(self):
658 self.__initialize_transaction()
661 cb = rpmmisc.RPMInstallCallback(self.ts_pre)
662 cb.headmsg = "Preinstall"
663 installlogfile = "%s/__catched_stderr.buf" % (self.instroot)
665 # start to catch stderr output from librpm
666 msger.enable_logstderr(installlogfile)
668 errors = self.ts_pre.run(cb.callback, '')
670 msger.disable_logstderr()
671 self.ts_pre.closeDB()
674 if errors is not None:
676 msger.warning('scriptlet or other non-fatal errors occurred '
677 'during transaction.')
682 raise RepoError('Could not run transaction.')
684 def installPkgs(self, package_objects):
686 self.__initialize_transaction()
689 self._cleanupRpmdbLocks(self.instroot)
692 for flag in self.probFilterFlags:
694 self.ts.setProbFilter(probfilter)
695 self.ts_pre.setProbFilter(probfilter)
697 localpkgs = self.localpkgs.keys()
699 for po in package_objects:
701 if pkgname in localpkgs:
702 rpmpath = self.localpkgs[pkgname]
704 rpmpath = self.getLocalPkgPath(po)
706 if not os.path.exists(rpmpath):
707 # Maybe it is a local repo
708 baseurl = str(po.repoInfo().baseUrls()[0])
709 baseurl = baseurl.strip()
711 location = zypp.asKindPackage(po).location()
712 location = str(location.filename())
714 if baseurl.startswith("file:/"):
715 rpmpath = baseurl[5:] + "/%s" % (location)
717 if not os.path.exists(rpmpath):
718 raise RpmError("Error: %s doesn't exist" % rpmpath)
720 h = rpmmisc.readRpmHeader(self.ts, rpmpath)
722 if pkgname in self.pre_pkgs:
723 msger.verbose("pre-install package added: %s" % pkgname)
724 self.ts_pre.addInstall(h, rpmpath, 'u')
726 self.ts.addInstall(h, rpmpath, 'u')
728 unresolved_dependencies = self.ts.check()
729 if not unresolved_dependencies:
731 self.preinstallPkgs()
734 cb = rpmmisc.RPMInstallCallback(self.ts)
735 installlogfile = "%s/__catched_stderr.buf" % (self.instroot)
737 # start to catch stderr output from librpm
738 msger.enable_logstderr(installlogfile)
740 errors = self.ts.run(cb.callback, '')
742 msger.disable_logstderr()
746 if errors is not None:
748 msger.warning('scriptlet or other non-fatal errors occurred '
749 'during transaction.')
754 raise RepoError('Could not run transaction.')
757 for pkg, need, needflags, sense, key in unresolved_dependencies:
758 package = '-'.join(pkg)
760 if needflags == rpm.RPMSENSE_LESS:
761 deppkg = ' < '.join(need)
762 elif needflags == rpm.RPMSENSE_EQUAL:
763 deppkg = ' = '.join(need)
764 elif needflags == rpm.RPMSENSE_GREATER:
765 deppkg = ' > '.join(need)
767 deppkg = '-'.join(need)
769 if sense == rpm.RPMDEP_SENSE_REQUIRES:
770 msger.warning("[%s] Requires [%s], which is not provided" \
773 elif sense == rpm.RPMDEP_SENSE_CONFLICTS:
774 msger.warning("[%s] Conflicts with [%s]" %(package,deppkg))
776 raise RepoError("Unresolved dependencies, transaction failed.")
778 def __initialize_transaction(self):
780 self.ts = rpm.TransactionSet(self.instroot)
781 # Set to not verify DSA signatures.
782 self.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS)
785 self.ts_pre = rpm.TransactionSet(self.instroot)
786 # Just unpack the files, don't run scripts
787 self.ts_pre.setFlags(rpm.RPMTRANS_FLAG_ALLFILES | rpm.RPMTRANS_FLAG_NOSCRIPTS)
788 # Set to not verify DSA signatures.
789 self.ts_pre.setVSFlags(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS)
791 def checkPkg(self, pkg):
793 if not os.path.exists(pkg):
795 ret = rpmmisc.checkRpmIntegrity('rpm', pkg)
797 msger.warning("package %s is damaged: %s" \
798 % (os.path.basename(pkg), pkg))
802 def _add_prob_flags(self, *flags):
804 if flag not in self.probFilterFlags:
805 self.probFilterFlags.append(flag)
807 def get_proxies(self, pobj):
813 repoinfo = pobj.repoInfo()
814 reponame = "%s" % repoinfo.name()
815 repos = filter(lambda r: r.name == reponame, self.repos)
816 repourl = str(repoinfo.baseUrls()[0])
819 proxy = repos[0].proxy
821 proxy = get_proxy_for(repourl)
823 proxies = {str(repourl.split(':')[0]): str(proxy)}
827 def get_url(self, pobj):
831 name = str(pobj.repoInfo().name())
833 repo = filter(lambda r: r.name == name, self.repos)[0]
837 baseurl = repo.baseurl[0]
839 index = baseurl.find("?")
841 baseurl = baseurl[:index]
843 location = zypp.asKindPackage(pobj).location()
844 location = str(location.filename())
845 if location.startswith("./"):
846 location = location[2:]
848 return os.path.join(baseurl, location)
850 def package_url(self, pkgname):
852 def cmpEVR(ed1, ed2):
853 (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()])
854 (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()])
855 return rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
858 self.__initialize_zypp()
861 q.addKind(zypp.ResKind.package)
863 q.addAttribute(zypp.SolvAttr.name, pkgname)
864 items = sorted(q.queryResults(self.Z.pool()),
865 cmp=lambda x,y: cmpEVR(x.edition(), y.edition()),
869 url = self.get_url(items[0])
870 proxies = self.get_proxies(items[0])
871 return (url, proxies)