diff options
| author | Tom Zanussi <tom.zanussi@linux.intel.com> | 2014-08-03 12:28:32 -0500 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2014-08-11 10:53:09 +0100 |
| commit | a9351096dbeaec3e33b907460d3bcaf32db875c2 (patch) | |
| tree | eb4f342c9cf260e20a4110d2c3a6cca73a793781 /scripts/lib/mic/plugins/backend/zypppkgmgr.py | |
| parent | f1dbd46e7e723da6e8aa79b6ff79079092f8462c (diff) | |
| download | poky-a9351096dbeaec3e33b907460d3bcaf32db875c2.tar.gz | |
wic: Remove mic package managers
wic doesn't use the yum or zypp package managers, remove them.
(From OE-Core rev: ef70b961b6b8f3a004036f84b608665db2085215)
Signed-off-by: Tom Zanussi <tom.zanussi@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/mic/plugins/backend/zypppkgmgr.py')
| -rwxr-xr-x | scripts/lib/mic/plugins/backend/zypppkgmgr.py | 973 |
1 files changed, 0 insertions, 973 deletions
diff --git a/scripts/lib/mic/plugins/backend/zypppkgmgr.py b/scripts/lib/mic/plugins/backend/zypppkgmgr.py deleted file mode 100755 index c760859832..0000000000 --- a/scripts/lib/mic/plugins/backend/zypppkgmgr.py +++ /dev/null | |||
| @@ -1,973 +0,0 @@ | |||
| 1 | #!/usr/bin/python -tt | ||
| 2 | # | ||
| 3 | # Copyright (c) 2010, 2011 Intel, Inc. | ||
| 4 | # | ||
| 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 | ||
| 8 | # | ||
| 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 | ||
| 12 | # for more details. | ||
| 13 | # | ||
| 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. | ||
| 17 | |||
| 18 | import os | ||
| 19 | import shutil | ||
| 20 | import urlparse | ||
| 21 | import rpm | ||
| 22 | |||
| 23 | import zypp | ||
| 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") | ||
| 29 | |||
| 30 | from mic import msger | ||
| 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 | ||
| 37 | |||
| 38 | class RepositoryStub: | ||
| 39 | def __init__(self): | ||
| 40 | self.name = None | ||
| 41 | self.baseurl = [] | ||
| 42 | self.mirrorlist = None | ||
| 43 | self.proxy = None | ||
| 44 | self.proxy_username = None | ||
| 45 | self.proxy_password = None | ||
| 46 | self.nocache = False | ||
| 47 | |||
| 48 | self.enabled = True | ||
| 49 | self.autorefresh = True | ||
| 50 | self.keeppackages = True | ||
| 51 | self.priority = None | ||
| 52 | |||
| 53 | from mic.pluginbase import BackendPlugin | ||
| 54 | class Zypp(BackendPlugin): | ||
| 55 | name = 'zypp' | ||
| 56 | |||
| 57 | def __init__(self, target_arch, instroot, cachedir): | ||
| 58 | self.cachedir = cachedir | ||
| 59 | self.instroot = instroot | ||
| 60 | self.target_arch = target_arch | ||
| 61 | |||
| 62 | self.__pkgs_license = {} | ||
| 63 | self.__pkgs_content = {} | ||
| 64 | self.__pkgs_vcsinfo = {} | ||
| 65 | self.repos = [] | ||
| 66 | self.to_deselect = [] | ||
| 67 | self.localpkgs = {} | ||
| 68 | self.repo_manager = None | ||
| 69 | self.repo_manager_options = None | ||
| 70 | self.Z = None | ||
| 71 | self.ts = None | ||
| 72 | self.ts_pre = None | ||
| 73 | self.incpkgs = {} | ||
| 74 | self.excpkgs = {} | ||
| 75 | self.pre_pkgs = [] | ||
| 76 | self.probFilterFlags = [ rpm.RPMPROB_FILTER_OLDPACKAGE, | ||
| 77 | rpm.RPMPROB_FILTER_REPLACEPKG ] | ||
| 78 | |||
| 79 | self.has_prov_query = True | ||
| 80 | self.install_debuginfo = False | ||
| 81 | |||
| 82 | def doFileLogSetup(self, uid, logfile): | ||
| 83 | # don't do the file log for the livecd as it can lead to open fds | ||
| 84 | # being left and an inability to clean up after ourself | ||
| 85 | pass | ||
| 86 | |||
| 87 | def closeRpmDB(self): | ||
| 88 | pass | ||
| 89 | |||
| 90 | def close(self): | ||
| 91 | if self.ts: | ||
| 92 | self.ts.closeDB() | ||
| 93 | self.ts = None | ||
| 94 | |||
| 95 | if self.ts_pre: | ||
| 96 | self.ts_pre.closeDB() | ||
| 97 | self.ts = None | ||
| 98 | |||
| 99 | self.closeRpmDB() | ||
| 100 | |||
| 101 | if not os.path.exists("/etc/fedora-release") and \ | ||
| 102 | not os.path.exists("/etc/meego-release"): | ||
| 103 | for i in range(3, os.sysconf("SC_OPEN_MAX")): | ||
| 104 | try: | ||
| 105 | os.close(i) | ||
| 106 | except: | ||
| 107 | pass | ||
| 108 | |||
| 109 | def __del__(self): | ||
| 110 | self.close() | ||
| 111 | |||
| 112 | def _cleanupRpmdbLocks(self, installroot): | ||
| 113 | # cleans up temporary files left by bdb so that differing | ||
| 114 | # versions of rpm don't cause problems | ||
| 115 | import glob | ||
| 116 | for f in glob.glob(installroot + "/var/lib/rpm/__db*"): | ||
| 117 | os.unlink(f) | ||
| 118 | |||
| 119 | def _cleanupZyppJunk(self, installroot): | ||
| 120 | try: | ||
| 121 | shutil.rmtree(os.path.join(installroot, '.zypp')) | ||
| 122 | except: | ||
| 123 | pass | ||
| 124 | |||
| 125 | def setup(self): | ||
| 126 | self._cleanupRpmdbLocks(self.instroot) | ||
| 127 | |||
| 128 | def whatObsolete(self, pkg): | ||
| 129 | query = zypp.PoolQuery() | ||
| 130 | query.addKind(zypp.ResKind.package) | ||
| 131 | query.addAttribute(zypp.SolvAttr.obsoletes, pkg) | ||
| 132 | query.setMatchExact() | ||
| 133 | for pi in query.queryResults(self.Z.pool()): | ||
| 134 | return pi | ||
| 135 | return None | ||
| 136 | |||
| 137 | def _zyppQueryPackage(self, pkg): | ||
| 138 | query = zypp.PoolQuery() | ||
| 139 | query.addKind(zypp.ResKind.package) | ||
| 140 | query.addAttribute(zypp.SolvAttr.name,pkg) | ||
| 141 | query.setMatchExact() | ||
| 142 | for pi in query.queryResults(self.Z.pool()): | ||
| 143 | return pi | ||
| 144 | return None | ||
| 145 | |||
| 146 | def _splitPkgString(self, pkg): | ||
| 147 | sp = pkg.rsplit(".",1) | ||
| 148 | name = sp[0] | ||
| 149 | arch = None | ||
| 150 | if len(sp) == 2: | ||
| 151 | arch = sp[1] | ||
| 152 | sysarch = zypp.Arch(self.target_arch) | ||
| 153 | if not zypp.Arch(arch).compatible_with (sysarch): | ||
| 154 | arch = None | ||
| 155 | name = ".".join(sp) | ||
| 156 | return name, arch | ||
| 157 | |||
| 158 | def selectPackage(self, pkg): | ||
| 159 | """Select a given package or package pattern, can be specified | ||
| 160 | with name.arch or name* or *name | ||
| 161 | """ | ||
| 162 | |||
| 163 | if not self.Z: | ||
| 164 | self.__initialize_zypp() | ||
| 165 | |||
| 166 | def markPoolItem(obs, pi): | ||
| 167 | if obs == None: | ||
| 168 | pi.status().setToBeInstalled (zypp.ResStatus.USER) | ||
| 169 | else: | ||
| 170 | obs.status().setToBeInstalled (zypp.ResStatus.USER) | ||
| 171 | |||
| 172 | def cmpEVR(p1, p2): | ||
| 173 | # compare criterion: arch compatibility first, then repo | ||
| 174 | # priority, and version last | ||
| 175 | a1 = p1.arch() | ||
| 176 | a2 = p2.arch() | ||
| 177 | if str(a1) != str(a2): | ||
| 178 | if a1.compatible_with(a2): | ||
| 179 | return -1 | ||
| 180 | else: | ||
| 181 | return 1 | ||
| 182 | # Priority of a repository is an integer value between 0 (the | ||
| 183 | # highest priority) and 99 (the lowest priority) | ||
| 184 | pr1 = int(p1.repoInfo().priority()) | ||
| 185 | pr2 = int(p2.repoInfo().priority()) | ||
| 186 | if pr1 > pr2: | ||
| 187 | return -1 | ||
| 188 | elif pr1 < pr2: | ||
| 189 | return 1 | ||
| 190 | |||
| 191 | ed1 = p1.edition() | ||
| 192 | ed2 = p2.edition() | ||
| 193 | (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()]) | ||
| 194 | (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()]) | ||
| 195 | return rpm.labelCompare((e1, v1, r1), (e2, v2, r2)) | ||
| 196 | |||
| 197 | found = False | ||
| 198 | startx = pkg.startswith("*") | ||
| 199 | endx = pkg.endswith("*") | ||
| 200 | ispattern = startx or endx | ||
| 201 | name, arch = self._splitPkgString(pkg) | ||
| 202 | |||
| 203 | q = zypp.PoolQuery() | ||
| 204 | q.addKind(zypp.ResKind.package) | ||
| 205 | |||
| 206 | if ispattern: | ||
| 207 | if startx and not endx: | ||
| 208 | pattern = '%s$' % (pkg[1:]) | ||
| 209 | if endx and not startx: | ||
| 210 | pattern = '^%s' % (pkg[0:-1]) | ||
| 211 | if endx and startx: | ||
| 212 | pattern = '%s' % (pkg[1:-1]) | ||
| 213 | q.setMatchRegex() | ||
| 214 | q.addAttribute(zypp.SolvAttr.name,pattern) | ||
| 215 | |||
| 216 | elif arch: | ||
| 217 | q.setMatchExact() | ||
| 218 | q.addAttribute(zypp.SolvAttr.name,name) | ||
| 219 | |||
| 220 | else: | ||
| 221 | q.setMatchExact() | ||
| 222 | q.addAttribute(zypp.SolvAttr.name,pkg) | ||
| 223 | |||
| 224 | for pitem in sorted( | ||
| 225 | q.queryResults(self.Z.pool()), | ||
| 226 | cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)), | ||
| 227 | reverse=True): | ||
| 228 | item = zypp.asKindPackage(pitem) | ||
| 229 | if item.name() in self.excpkgs.keys() and \ | ||
| 230 | self.excpkgs[item.name()] == item.repoInfo().name(): | ||
| 231 | continue | ||
| 232 | if item.name() in self.incpkgs.keys() and \ | ||
| 233 | self.incpkgs[item.name()] != item.repoInfo().name(): | ||
| 234 | continue | ||
| 235 | |||
| 236 | found = True | ||
| 237 | obspkg = self.whatObsolete(item.name()) | ||
| 238 | if arch: | ||
| 239 | if arch == str(item.arch()): | ||
| 240 | item.status().setToBeInstalled (zypp.ResStatus.USER) | ||
| 241 | else: | ||
| 242 | markPoolItem(obspkg, pitem) | ||
| 243 | if not ispattern: | ||
| 244 | break | ||
| 245 | |||
| 246 | # Can't match using package name, then search from packge | ||
| 247 | # provides infomation | ||
| 248 | if found == False and not ispattern: | ||
| 249 | q.addAttribute(zypp.SolvAttr.provides, pkg) | ||
| 250 | q.addAttribute(zypp.SolvAttr.name,'') | ||
| 251 | |||
| 252 | for pitem in sorted( | ||
| 253 | q.queryResults(self.Z.pool()), | ||
| 254 | cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)), | ||
| 255 | reverse=True): | ||
| 256 | item = zypp.asKindPackage(pitem) | ||
| 257 | if item.name() in self.excpkgs.keys() and \ | ||
| 258 | self.excpkgs[item.name()] == item.repoInfo().name(): | ||
| 259 | continue | ||
| 260 | if item.name() in self.incpkgs.keys() and \ | ||
| 261 | self.incpkgs[item.name()] != item.repoInfo().name(): | ||
| 262 | continue | ||
| 263 | |||
| 264 | found = True | ||
| 265 | obspkg = self.whatObsolete(item.name()) | ||
| 266 | markPoolItem(obspkg, pitem) | ||
| 267 | break | ||
| 268 | |||
| 269 | if found: | ||
| 270 | return None | ||
| 271 | else: | ||
| 272 | raise CreatorError("Unable to find package: %s" % (pkg,)) | ||
| 273 | |||
| 274 | def inDeselectPackages(self, pitem): | ||
| 275 | """check if specified pacakges are in the list of inDeselectPackages | ||
| 276 | """ | ||
| 277 | item = zypp.asKindPackage(pitem) | ||
| 278 | name = item.name() | ||
| 279 | for pkg in self.to_deselect: | ||
| 280 | startx = pkg.startswith("*") | ||
| 281 | endx = pkg.endswith("*") | ||
| 282 | ispattern = startx or endx | ||
| 283 | pkgname, pkgarch = self._splitPkgString(pkg) | ||
| 284 | if not ispattern: | ||
| 285 | if pkgarch: | ||
| 286 | if name == pkgname and str(item.arch()) == pkgarch: | ||
| 287 | return True; | ||
| 288 | else: | ||
| 289 | if name == pkgname: | ||
| 290 | return True; | ||
| 291 | else: | ||
| 292 | if startx and name.endswith(pkg[1:]): | ||
| 293 | return True; | ||
| 294 | if endx and name.startswith(pkg[:-1]): | ||
| 295 | return True; | ||
| 296 | |||
| 297 | return False; | ||
| 298 | |||
| 299 | def deselectPackage(self, pkg): | ||
| 300 | """collect packages should not be installed""" | ||
| 301 | self.to_deselect.append(pkg) | ||
| 302 | |||
| 303 | def selectGroup(self, grp, include = ksparser.GROUP_DEFAULT): | ||
| 304 | if not self.Z: | ||
| 305 | self.__initialize_zypp() | ||
| 306 | found = False | ||
| 307 | q=zypp.PoolQuery() | ||
| 308 | q.addKind(zypp.ResKind.pattern) | ||
| 309 | for pitem in q.queryResults(self.Z.pool()): | ||
| 310 | item = zypp.asKindPattern(pitem) | ||
| 311 | summary = "%s" % item.summary() | ||
| 312 | name = "%s" % item.name() | ||
| 313 | if name == grp or summary == grp: | ||
| 314 | found = True | ||
| 315 | pitem.status().setToBeInstalled (zypp.ResStatus.USER) | ||
| 316 | break | ||
| 317 | |||
| 318 | if found: | ||
| 319 | if include == ksparser.GROUP_REQUIRED: | ||
| 320 | map( | ||
| 321 | lambda p: self.deselectPackage(p), | ||
| 322 | grp.default_packages.keys()) | ||
| 323 | |||
| 324 | return None | ||
| 325 | else: | ||
| 326 | raise CreatorError("Unable to find pattern: %s" % (grp,)) | ||
| 327 | |||
| 328 | def addRepository(self, name, | ||
| 329 | url = None, | ||
| 330 | mirrorlist = None, | ||
| 331 | proxy = None, | ||
| 332 | proxy_username = None, | ||
| 333 | proxy_password = None, | ||
| 334 | inc = None, | ||
| 335 | exc = None, | ||
| 336 | ssl_verify = True, | ||
| 337 | nocache = False, | ||
| 338 | cost=None, | ||
| 339 | priority=None): | ||
| 340 | # TODO: Handle cost attribute for repos | ||
| 341 | |||
| 342 | if not self.repo_manager: | ||
| 343 | self.__initialize_repo_manager() | ||
| 344 | |||
| 345 | if not proxy and url: | ||
| 346 | proxy = get_proxy_for(url) | ||
| 347 | |||
| 348 | repo = RepositoryStub() | ||
| 349 | repo.name = name | ||
| 350 | repo.id = name | ||
| 351 | repo.proxy = proxy | ||
| 352 | repo.proxy_username = proxy_username | ||
| 353 | repo.proxy_password = proxy_password | ||
| 354 | repo.ssl_verify = ssl_verify | ||
| 355 | repo.nocache = nocache | ||
| 356 | repo.baseurl.append(url) | ||
| 357 | if inc: | ||
| 358 | for pkg in inc: | ||
| 359 | self.incpkgs[pkg] = name | ||
| 360 | if exc: | ||
| 361 | for pkg in exc: | ||
| 362 | self.excpkgs[pkg] = name | ||
| 363 | |||
| 364 | # check LICENSE files | ||
| 365 | if not rpmmisc.checkRepositoryEULA(name, repo): | ||
| 366 | msger.warning('skip repo:%s for failed EULA confirmation' % name) | ||
| 367 | return None | ||
| 368 | |||
| 369 | if mirrorlist: | ||
| 370 | repo.mirrorlist = mirrorlist | ||
| 371 | |||
| 372 | # Enable gpg check for verifying corrupt packages | ||
| 373 | repo.gpgcheck = 1 | ||
| 374 | if priority is not None: | ||
| 375 | # priority 0 has issue in RepoInfo.setPriority | ||
| 376 | repo.priority = priority + 1 | ||
| 377 | |||
| 378 | try: | ||
| 379 | repo_info = zypp.RepoInfo() | ||
| 380 | repo_info.setAlias(repo.name) | ||
| 381 | repo_info.setName(repo.name) | ||
| 382 | repo_info.setEnabled(repo.enabled) | ||
| 383 | repo_info.setAutorefresh(repo.autorefresh) | ||
| 384 | repo_info.setKeepPackages(repo.keeppackages) | ||
| 385 | baseurl = zypp.Url(repo.baseurl[0]) | ||
| 386 | if not ssl_verify: | ||
| 387 | baseurl.setQueryParam("ssl_verify", "no") | ||
| 388 | if proxy: | ||
| 389 | scheme, host, path, parm, query, frag = urlparse.urlparse(proxy) | ||
| 390 | |||
| 391 | proxyinfo = host.split(":") | ||
| 392 | host = proxyinfo[0] | ||
| 393 | |||
| 394 | port = "80" | ||
| 395 | if len(proxyinfo) > 1: | ||
| 396 | port = proxyinfo[1] | ||
| 397 | |||
| 398 | if proxy.startswith("socks") and len(proxy.rsplit(':', 1)) == 2: | ||
| 399 | host = proxy.rsplit(':', 1)[0] | ||
| 400 | port = proxy.rsplit(':', 1)[1] | ||
| 401 | |||
| 402 | baseurl.setQueryParam ("proxy", host) | ||
| 403 | baseurl.setQueryParam ("proxyport", port) | ||
| 404 | |||
| 405 | repo.baseurl[0] = baseurl.asCompleteString() | ||
| 406 | self.repos.append(repo) | ||
| 407 | |||
| 408 | repo_info.addBaseUrl(baseurl) | ||
| 409 | |||
| 410 | if repo.priority is not None: | ||
| 411 | repo_info.setPriority(repo.priority) | ||
| 412 | |||
| 413 | # this hack is used to change zypp credential file location | ||
| 414 | # the default one is $HOME/.zypp, which cause conflicts when | ||
| 415 | # installing some basic packages, and the location doesn't | ||
| 416 | # have any interface actually, so use a tricky way anyway | ||
| 417 | homedir = None | ||
| 418 | if 'HOME' in os.environ: | ||
| 419 | homedir = os.environ['HOME'] | ||
| 420 | os.environ['HOME'] = '/' | ||
| 421 | else: | ||
| 422 | os.environ['HOME'] = '/' | ||
| 423 | |||
| 424 | self.repo_manager.addRepository(repo_info) | ||
| 425 | |||
| 426 | # save back the $HOME env | ||
| 427 | if homedir: | ||
| 428 | os.environ['HOME'] = homedir | ||
| 429 | else: | ||
| 430 | del os.environ['HOME'] | ||
| 431 | |||
| 432 | self.__build_repo_cache(name) | ||
| 433 | |||
| 434 | except RuntimeError, e: | ||
| 435 | raise CreatorError(str(e)) | ||
| 436 | |||
| 437 | msger.verbose('repo: %s was added' % name) | ||
| 438 | return repo | ||
| 439 | |||
| 440 | def installHasFile(self, file): | ||
| 441 | return False | ||
| 442 | |||
| 443 | def preInstall(self, pkg): | ||
| 444 | self.pre_pkgs.append(pkg) | ||
| 445 | |||
| 446 | def runInstall(self, checksize = 0): | ||
| 447 | os.environ["HOME"] = "/" | ||
| 448 | os.environ["LD_PRELOAD"] = "" | ||
| 449 | self.buildTransaction() | ||
| 450 | |||
| 451 | todo = zypp.GetResolvablesToInsDel(self.Z.pool()) | ||
| 452 | installed_pkgs = todo._toInstall | ||
| 453 | dlpkgs = [] | ||
| 454 | for pitem in installed_pkgs: | ||
| 455 | if not zypp.isKindPattern(pitem) and \ | ||
| 456 | not self.inDeselectPackages(pitem): | ||
| 457 | item = zypp.asKindPackage(pitem) | ||
| 458 | dlpkgs.append(item) | ||
| 459 | |||
| 460 | if not self.install_debuginfo or str(item.arch()) == "noarch": | ||
| 461 | continue | ||
| 462 | |||
| 463 | dipkg = self._zyppQueryPackage("%s-debuginfo" % item.name()) | ||
| 464 | if dipkg: | ||
| 465 | ditem = zypp.asKindPackage(dipkg) | ||
| 466 | dlpkgs.append(ditem) | ||
| 467 | else: | ||
| 468 | msger.warning("No debuginfo rpm found for: %s" \ | ||
| 469 | % item.name()) | ||
| 470 | |||
| 471 | # record all pkg and the content | ||
| 472 | localpkgs = self.localpkgs.keys() | ||
| 473 | for pkg in dlpkgs: | ||
| 474 | license = '' | ||
| 475 | if pkg.name() in localpkgs: | ||
| 476 | hdr = rpmmisc.readRpmHeader(self.ts, self.localpkgs[pkg.name()]) | ||
| 477 | pkg_long_name = misc.RPM_FMT % { | ||
| 478 | 'name': hdr['name'], | ||
| 479 | 'arch': hdr['arch'], | ||
| 480 | 'version': hdr['version'], | ||
| 481 | 'release': hdr['release'] | ||
| 482 | } | ||
| 483 | license = hdr['license'] | ||
| 484 | |||
| 485 | else: | ||
| 486 | pkg_long_name = misc.RPM_FMT % { | ||
| 487 | 'name': pkg.name(), | ||
| 488 | 'arch': pkg.arch(), | ||
| 489 | 'version': pkg.edition().version(), | ||
| 490 | 'release': pkg.edition().release() | ||
| 491 | } | ||
| 492 | |||
| 493 | license = pkg.license() | ||
| 494 | |||
| 495 | if license in self.__pkgs_license.keys(): | ||
| 496 | self.__pkgs_license[license].append(pkg_long_name) | ||
| 497 | else: | ||
| 498 | self.__pkgs_license[license] = [pkg_long_name] | ||
| 499 | |||
| 500 | total_count = len(dlpkgs) | ||
| 501 | cached_count = 0 | ||
| 502 | download_total_size = sum(map(lambda x: int(x.downloadSize()), dlpkgs)) | ||
| 503 | localpkgs = self.localpkgs.keys() | ||
| 504 | |||
| 505 | msger.info("Checking packages cached ...") | ||
| 506 | for po in dlpkgs: | ||
| 507 | # Check if it is cached locally | ||
| 508 | if po.name() in localpkgs: | ||
| 509 | cached_count += 1 | ||
| 510 | else: | ||
| 511 | local = self.getLocalPkgPath(po) | ||
| 512 | name = str(po.repoInfo().name()) | ||
| 513 | try: | ||
| 514 | repo = filter(lambda r: r.name == name, self.repos)[0] | ||
| 515 | except IndexError: | ||
| 516 | repo = None | ||
| 517 | nocache = repo.nocache if repo else False | ||
| 518 | |||
| 519 | if os.path.exists(local): | ||
| 520 | if nocache or self.checkPkg(local) !=0: | ||
| 521 | os.unlink(local) | ||
| 522 | else: | ||
| 523 | download_total_size -= int(po.downloadSize()) | ||
| 524 | cached_count += 1 | ||
| 525 | cache_avail_size = misc.get_filesystem_avail(self.cachedir) | ||
| 526 | if cache_avail_size < download_total_size: | ||
| 527 | raise CreatorError("No enough space used for downloading.") | ||
| 528 | |||
| 529 | # record the total size of installed pkgs | ||
| 530 | install_total_size = sum(map(lambda x: int(x.installSize()), dlpkgs)) | ||
| 531 | # check needed size before actually download and install | ||
| 532 | |||
| 533 | # FIXME: for multiple partitions for loop type, check fails | ||
| 534 | # skip the check temporarily | ||
| 535 | #if checksize and install_total_size > checksize: | ||
| 536 | # raise CreatorError("No enough space used for installing, " | ||
| 537 | # "please resize partition size in ks file") | ||
| 538 | |||
| 539 | download_count = total_count - cached_count | ||
| 540 | msger.info("Packages: %d Total, %d Cached, %d Missed" \ | ||
| 541 | % (total_count, cached_count, download_count)) | ||
| 542 | |||
| 543 | try: | ||
| 544 | if download_count > 0: | ||
| 545 | msger.info("Downloading packages ...") | ||
| 546 | self.downloadPkgs(dlpkgs, download_count) | ||
| 547 | |||
| 548 | self.installPkgs(dlpkgs) | ||
| 549 | |||
| 550 | except (RepoError, RpmError): | ||
| 551 | raise | ||
| 552 | except Exception, e: | ||
| 553 | raise CreatorError("Package installation failed: %s" % (e,)) | ||
| 554 | |||
| 555 | def getVcsInfo(self): | ||
| 556 | if self.__pkgs_vcsinfo: | ||
| 557 | return | ||
| 558 | |||
| 559 | if not self.ts: | ||
| 560 | self.__initialize_transaction() | ||
| 561 | |||
| 562 | mi = self.ts.dbMatch() | ||
| 563 | for hdr in mi: | ||
| 564 | lname = misc.RPM_FMT % { | ||
| 565 | 'name': hdr['name'], | ||
| 566 | 'arch': hdr['arch'], | ||
| 567 | 'version': hdr['version'], | ||
| 568 | 'release': hdr['release'] | ||
| 569 | } | ||
| 570 | self.__pkgs_vcsinfo[lname] = hdr['VCS'] | ||
| 571 | |||
| 572 | return self.__pkgs_vcsinfo | ||
| 573 | |||
| 574 | def getAllContent(self): | ||
| 575 | if self.__pkgs_content: | ||
| 576 | return self.__pkgs_content | ||
| 577 | |||
| 578 | if not self.ts: | ||
| 579 | self.__initialize_transaction() | ||
| 580 | |||
| 581 | mi = self.ts.dbMatch() | ||
| 582 | for hdr in mi: | ||
| 583 | lname = misc.RPM_FMT % { | ||
| 584 | 'name': hdr['name'], | ||
| 585 | 'arch': hdr['arch'], | ||
| 586 | 'version': hdr['version'], | ||
| 587 | 'release': hdr['release'] | ||
| 588 | } | ||
| 589 | self.__pkgs_content[lname] = hdr['FILENAMES'] | ||
| 590 | |||
| 591 | return self.__pkgs_content | ||
| 592 | |||
| 593 | def getPkgsLicense(self): | ||
| 594 | return self.__pkgs_license | ||
| 595 | |||
| 596 | def getFilelist(self, pkgname): | ||
| 597 | if not pkgname: | ||
| 598 | return None | ||
| 599 | |||
| 600 | if not self.ts: | ||
| 601 | self.__initialize_transaction() | ||
| 602 | |||
| 603 | mi = self.ts.dbMatch('name', pkgname) | ||
| 604 | for header in mi: | ||
| 605 | return header['FILENAMES'] | ||
| 606 | |||
| 607 | def __initialize_repo_manager(self): | ||
| 608 | if self.repo_manager: | ||
| 609 | return | ||
| 610 | |||
| 611 | # Clean up repo metadata | ||
| 612 | shutil.rmtree(self.cachedir + "/etc", ignore_errors = True) | ||
| 613 | shutil.rmtree(self.cachedir + "/solv", ignore_errors = True) | ||
| 614 | shutil.rmtree(self.cachedir + "/raw", ignore_errors = True) | ||
| 615 | |||
| 616 | zypp.KeyRing.setDefaultAccept( zypp.KeyRing.ACCEPT_UNSIGNED_FILE | ||
| 617 | | zypp.KeyRing.ACCEPT_VERIFICATION_FAILED | ||
| 618 | | zypp.KeyRing.ACCEPT_UNKNOWNKEY | ||
| 619 | | zypp.KeyRing.TRUST_KEY_TEMPORARILY | ||
| 620 | ) | ||
| 621 | |||
| 622 | self.repo_manager_options = \ | ||
| 623 | zypp.RepoManagerOptions(zypp.Pathname(self.instroot)) | ||
| 624 | |||
| 625 | self.repo_manager_options.knownReposPath = \ | ||
| 626 | zypp.Pathname(self.cachedir + "/etc/zypp/repos.d") | ||
| 627 | |||
| 628 | self.repo_manager_options.repoCachePath = \ | ||
| 629 | zypp.Pathname(self.cachedir) | ||
| 630 | |||
| 631 | self.repo_manager_options.repoRawCachePath = \ | ||
| 632 | zypp.Pathname(self.cachedir + "/raw") | ||
| 633 | |||
| 634 | self.repo_manager_options.repoSolvCachePath = \ | ||
| 635 | zypp.Pathname(self.cachedir + "/solv") | ||
| 636 | |||
| 637 | self.repo_manager_options.repoPackagesCachePath = \ | ||
| 638 | zypp.Pathname(self.cachedir + "/packages") | ||
| 639 | |||
| 640 | self.repo_manager = zypp.RepoManager(self.repo_manager_options) | ||
| 641 | |||
| 642 | def __build_repo_cache(self, name): | ||
| 643 | repo = self.repo_manager.getRepositoryInfo(name) | ||
| 644 | if self.repo_manager.isCached(repo) or not repo.enabled(): | ||
| 645 | return | ||
| 646 | |||
| 647 | msger.info('Refreshing repository: %s ...' % name) | ||
| 648 | self.repo_manager.buildCache(repo, zypp.RepoManager.BuildIfNeeded) | ||
| 649 | |||
| 650 | def __initialize_zypp(self): | ||
| 651 | if self.Z: | ||
| 652 | return | ||
| 653 | |||
| 654 | zconfig = zypp.ZConfig_instance() | ||
| 655 | |||
| 656 | # Set system architecture | ||
| 657 | if self.target_arch: | ||
| 658 | zconfig.setSystemArchitecture(zypp.Arch(self.target_arch)) | ||
| 659 | |||
| 660 | msger.info("zypp architecture is <%s>" % zconfig.systemArchitecture()) | ||
| 661 | |||
| 662 | # repoPackagesCachePath is corrected by this | ||
| 663 | self.repo_manager = zypp.RepoManager(self.repo_manager_options) | ||
| 664 | repos = self.repo_manager.knownRepositories() | ||
| 665 | for repo in repos: | ||
| 666 | if not repo.enabled(): | ||
| 667 | continue | ||
| 668 | self.repo_manager.loadFromCache(repo) | ||
| 669 | |||
| 670 | self.Z = zypp.ZYppFactory_instance().getZYpp() | ||
| 671 | self.Z.initializeTarget(zypp.Pathname(self.instroot)) | ||
| 672 | self.Z.target().load() | ||
| 673 | |||
| 674 | def buildTransaction(self): | ||
| 675 | if not self.Z.resolver().resolvePool(): | ||
| 676 | probs = self.Z.resolver().problems() | ||
| 677 | |||
| 678 | for problem in probs: | ||
| 679 | msger.warning("repo problem: %s, %s" \ | ||
| 680 | % (problem.description().decode("utf-8"), | ||
| 681 | problem.details().decode("utf-8"))) | ||
| 682 | |||
| 683 | raise RepoError("found %d resolver problem, abort!" \ | ||
| 684 | % len(probs)) | ||
| 685 | |||
| 686 | def getLocalPkgPath(self, po): | ||
| 687 | repoinfo = po.repoInfo() | ||
| 688 | cacheroot = repoinfo.packagesPath() | ||
| 689 | location= po.location() | ||
| 690 | rpmpath = str(location.filename()) | ||
| 691 | pkgpath = "%s/%s" % (cacheroot, os.path.basename(rpmpath)) | ||
| 692 | return pkgpath | ||
| 693 | |||
| 694 | def installLocal(self, pkg, po=None, updateonly=False): | ||
| 695 | if not self.ts: | ||
| 696 | self.__initialize_transaction() | ||
| 697 | |||
| 698 | solvfile = "%s/.solv" % (self.cachedir) | ||
| 699 | |||
| 700 | rc, out = runner.runtool([fs_related.find_binary_path("rpms2solv"), | ||
| 701 | pkg]) | ||
| 702 | if rc == 0: | ||
| 703 | f = open(solvfile, "w+") | ||
| 704 | f.write(out) | ||
| 705 | f.close() | ||
| 706 | |||
| 707 | warnmsg = self.repo_manager.loadSolvFile(solvfile, | ||
| 708 | os.path.basename(pkg)) | ||
| 709 | if warnmsg: | ||
| 710 | msger.warning(warnmsg) | ||
| 711 | |||
| 712 | os.unlink(solvfile) | ||
| 713 | else: | ||
| 714 | msger.warning('Can not get %s solv data.' % pkg) | ||
| 715 | |||
| 716 | hdr = rpmmisc.readRpmHeader(self.ts, pkg) | ||
| 717 | arch = zypp.Arch(hdr['arch']) | ||
| 718 | sysarch = zypp.Arch(self.target_arch) | ||
| 719 | |||
| 720 | if arch.compatible_with (sysarch): | ||
| 721 | pkgname = hdr['name'] | ||
| 722 | self.localpkgs[pkgname] = pkg | ||
| 723 | self.selectPackage(pkgname) | ||
| 724 | msger.info("Marking %s to be installed" % (pkg)) | ||
| 725 | |||
| 726 | else: | ||
| 727 | msger.warning("Cannot add package %s to transaction. " | ||
| 728 | "Not a compatible architecture: %s" \ | ||
| 729 | % (pkg, hdr['arch'])) | ||
| 730 | |||
| 731 | def downloadPkgs(self, package_objects, count): | ||
| 732 | localpkgs = self.localpkgs.keys() | ||
| 733 | progress_obj = TextProgress(count) | ||
| 734 | |||
| 735 | for po in package_objects: | ||
| 736 | if po.name() in localpkgs: | ||
| 737 | continue | ||
| 738 | |||
| 739 | filename = self.getLocalPkgPath(po) | ||
| 740 | if os.path.exists(filename): | ||
| 741 | if self.checkPkg(filename) == 0: | ||
| 742 | continue | ||
| 743 | |||
| 744 | dirn = os.path.dirname(filename) | ||
| 745 | if not os.path.exists(dirn): | ||
| 746 | os.makedirs(dirn) | ||
| 747 | |||
| 748 | url = self.get_url(po) | ||
| 749 | proxies = self.get_proxies(po) | ||
| 750 | |||
| 751 | try: | ||
| 752 | filename = myurlgrab(url, filename, proxies, progress_obj) | ||
| 753 | except CreatorError: | ||
| 754 | self.close() | ||
| 755 | raise | ||
| 756 | |||
| 757 | def preinstallPkgs(self): | ||
| 758 | if not self.ts_pre: | ||
| 759 | self.__initialize_transaction() | ||
| 760 | |||
| 761 | self.ts_pre.order() | ||
| 762 | cb = rpmmisc.RPMInstallCallback(self.ts_pre) | ||
| 763 | cb.headmsg = "Preinstall" | ||
| 764 | installlogfile = "%s/__catched_stderr.buf" % (self.instroot) | ||
| 765 | |||
| 766 | # start to catch stderr output from librpm | ||
| 767 | msger.enable_logstderr(installlogfile) | ||
| 768 | |||
| 769 | errors = self.ts_pre.run(cb.callback, '') | ||
| 770 | # stop catch | ||
| 771 | msger.disable_logstderr() | ||
| 772 | self.ts_pre.closeDB() | ||
| 773 | self.ts_pre = None | ||
| 774 | |||
| 775 | if errors is not None: | ||
| 776 | if len(errors) == 0: | ||
| 777 | msger.warning('scriptlet or other non-fatal errors occurred ' | ||
| 778 | 'during transaction.') | ||
| 779 | |||
| 780 | else: | ||
| 781 | for e in errors: | ||
| 782 | msger.warning(e[0]) | ||
| 783 | raise RepoError('Could not run transaction.') | ||
| 784 | |||
| 785 | def installPkgs(self, package_objects): | ||
| 786 | if not self.ts: | ||
| 787 | self.__initialize_transaction() | ||
| 788 | |||
| 789 | # clean rpm lock | ||
| 790 | self._cleanupRpmdbLocks(self.instroot) | ||
| 791 | self._cleanupZyppJunk(self.instroot) | ||
| 792 | # Set filters | ||
| 793 | probfilter = 0 | ||
| 794 | for flag in self.probFilterFlags: | ||
| 795 | probfilter |= flag | ||
| 796 | self.ts.setProbFilter(probfilter) | ||
| 797 | self.ts_pre.setProbFilter(probfilter) | ||
| 798 | |||
| 799 | localpkgs = self.localpkgs.keys() | ||
| 800 | |||
| 801 | for po in package_objects: | ||
| 802 | pkgname = po.name() | ||
| 803 | if pkgname in localpkgs: | ||
| 804 | rpmpath = self.localpkgs[pkgname] | ||
| 805 | else: | ||
| 806 | rpmpath = self.getLocalPkgPath(po) | ||
| 807 | |||
| 808 | if not os.path.exists(rpmpath): | ||
| 809 | # Maybe it is a local repo | ||
| 810 | rpmuri = self.get_url(po) | ||
| 811 | if rpmuri.startswith("file:/"): | ||
| 812 | rpmpath = rpmuri[5:] | ||
| 813 | |||
| 814 | if not os.path.exists(rpmpath): | ||
| 815 | raise RpmError("Error: %s doesn't exist" % rpmpath) | ||
| 816 | |||
| 817 | h = rpmmisc.readRpmHeader(self.ts, rpmpath) | ||
| 818 | |||
| 819 | if pkgname in self.pre_pkgs: | ||
| 820 | msger.verbose("pre-install package added: %s" % pkgname) | ||
| 821 | self.ts_pre.addInstall(h, rpmpath, 'u') | ||
| 822 | |||
| 823 | self.ts.addInstall(h, rpmpath, 'u') | ||
| 824 | |||
| 825 | unresolved_dependencies = self.ts.check() | ||
| 826 | if not unresolved_dependencies: | ||
| 827 | if self.pre_pkgs: | ||
| 828 | self.preinstallPkgs() | ||
| 829 | |||
| 830 | self.ts.order() | ||
| 831 | cb = rpmmisc.RPMInstallCallback(self.ts) | ||
| 832 | installlogfile = "%s/__catched_stderr.buf" % (self.instroot) | ||
| 833 | |||
| 834 | # start to catch stderr output from librpm | ||
| 835 | msger.enable_logstderr(installlogfile) | ||
| 836 | |||
| 837 | errors = self.ts.run(cb.callback, '') | ||
| 838 | # stop catch | ||
| 839 | msger.disable_logstderr() | ||
| 840 | self.ts.closeDB() | ||
| 841 | self.ts = None | ||
| 842 | |||
| 843 | if errors is not None: | ||
| 844 | if len(errors) == 0: | ||
| 845 | msger.warning('scriptlet or other non-fatal errors occurred ' | ||
| 846 | 'during transaction.') | ||
| 847 | |||
| 848 | else: | ||
| 849 | for e in errors: | ||
| 850 | msger.warning(e[0]) | ||
| 851 | raise RepoError('Could not run transaction.') | ||
| 852 | |||
| 853 | else: | ||
| 854 | for pkg, need, needflags, sense, key in unresolved_dependencies: | ||
| 855 | package = '-'.join(pkg) | ||
| 856 | |||
| 857 | if needflags == rpm.RPMSENSE_LESS: | ||
| 858 | deppkg = ' < '.join(need) | ||
| 859 | elif needflags == rpm.RPMSENSE_EQUAL: | ||
| 860 | deppkg = ' = '.join(need) | ||
| 861 | elif needflags == rpm.RPMSENSE_GREATER: | ||
| 862 | deppkg = ' > '.join(need) | ||
| 863 | else: | ||
| 864 | deppkg = '-'.join(need) | ||
| 865 | |||
| 866 | if sense == rpm.RPMDEP_SENSE_REQUIRES: | ||
| 867 | msger.warning("[%s] Requires [%s], which is not provided" \ | ||
| 868 | % (package, deppkg)) | ||
| 869 | |||
| 870 | elif sense == rpm.RPMDEP_SENSE_CONFLICTS: | ||
| 871 | msger.warning("[%s] Conflicts with [%s]" %(package,deppkg)) | ||
| 872 | |||
| 873 | raise RepoError("Unresolved dependencies, transaction failed.") | ||
| 874 | |||
| 875 | def __initialize_transaction(self): | ||
| 876 | if not self.ts: | ||
| 877 | self.ts = rpm.TransactionSet(self.instroot) | ||
| 878 | # Set to not verify DSA signatures. | ||
| 879 | self.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS) | ||
| 880 | |||
| 881 | if not self.ts_pre: | ||
| 882 | self.ts_pre = rpm.TransactionSet(self.instroot) | ||
| 883 | # Just unpack the files, don't run scripts | ||
| 884 | self.ts_pre.setFlags(rpm.RPMTRANS_FLAG_ALLFILES | rpm.RPMTRANS_FLAG_NOSCRIPTS) | ||
| 885 | # Set to not verify DSA signatures. | ||
| 886 | self.ts_pre.setVSFlags(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS) | ||
| 887 | |||
| 888 | def checkPkg(self, pkg): | ||
| 889 | ret = 1 | ||
| 890 | if not os.path.exists(pkg): | ||
| 891 | return ret | ||
| 892 | ret = rpmmisc.checkRpmIntegrity('rpm', pkg) | ||
| 893 | if ret != 0: | ||
| 894 | msger.warning("package %s is damaged: %s" \ | ||
| 895 | % (os.path.basename(pkg), pkg)) | ||
| 896 | |||
| 897 | return ret | ||
| 898 | |||
| 899 | def _add_prob_flags(self, *flags): | ||
| 900 | for flag in flags: | ||
| 901 | if flag not in self.probFilterFlags: | ||
| 902 | self.probFilterFlags.append(flag) | ||
| 903 | |||
| 904 | def get_proxies(self, pobj): | ||
| 905 | if not pobj: | ||
| 906 | return None | ||
| 907 | |||
| 908 | proxy = None | ||
| 909 | proxies = None | ||
| 910 | repoinfo = pobj.repoInfo() | ||
| 911 | reponame = "%s" % repoinfo.name() | ||
| 912 | repos = filter(lambda r: r.name == reponame, self.repos) | ||
| 913 | repourl = str(repoinfo.baseUrls()[0]) | ||
| 914 | |||
| 915 | if repos: | ||
| 916 | proxy = repos[0].proxy | ||
| 917 | if not proxy: | ||
| 918 | proxy = get_proxy_for(repourl) | ||
| 919 | if proxy: | ||
| 920 | proxies = {str(repourl.split(':')[0]): str(proxy)} | ||
| 921 | |||
| 922 | return proxies | ||
| 923 | |||
| 924 | def get_url(self, pobj): | ||
| 925 | if not pobj: | ||
| 926 | return None | ||
| 927 | |||
| 928 | name = str(pobj.repoInfo().name()) | ||
| 929 | try: | ||
| 930 | repo = filter(lambda r: r.name == name, self.repos)[0] | ||
| 931 | except IndexError: | ||
| 932 | return None | ||
| 933 | |||
| 934 | baseurl = repo.baseurl[0] | ||
| 935 | |||
| 936 | index = baseurl.find("?") | ||
| 937 | if index > -1: | ||
| 938 | baseurl = baseurl[:index] | ||
| 939 | |||
| 940 | location = pobj.location() | ||
| 941 | location = str(location.filename()) | ||
| 942 | if location.startswith("./"): | ||
| 943 | location = location[2:] | ||
| 944 | |||
| 945 | return os.path.join(baseurl, location) | ||
| 946 | |||
| 947 | def package_url(self, pkgname): | ||
| 948 | |||
| 949 | def cmpEVR(p1, p2): | ||
| 950 | ed1 = p1.edition() | ||
| 951 | ed2 = p2.edition() | ||
| 952 | (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()]) | ||
| 953 | (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()]) | ||
| 954 | return rpm.labelCompare((e1, v1, r1), (e2, v2, r2)) | ||
| 955 | |||
| 956 | if not self.Z: | ||
| 957 | self.__initialize_zypp() | ||
| 958 | |||
| 959 | q = zypp.PoolQuery() | ||
| 960 | q.addKind(zypp.ResKind.package) | ||
| 961 | q.setMatchExact() | ||
| 962 | q.addAttribute(zypp.SolvAttr.name, pkgname) | ||
| 963 | items = sorted(q.queryResults(self.Z.pool()), | ||
| 964 | cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)), | ||
| 965 | reverse=True) | ||
| 966 | |||
| 967 | if items: | ||
| 968 | item = zypp.asKindPackage(items[0]) | ||
| 969 | url = self.get_url(item) | ||
| 970 | proxies = self.get_proxies(item) | ||
| 971 | return (url, proxies) | ||
| 972 | |||
| 973 | return (None, None) | ||
