removed unnecessary selinux abort condition
[livecd.git] / imgcreate / yuminst.py
blob721a2d01bac44a96883662debc0efc56884a3c97
2 # yum.py : yum utilities
4 # Copyright 2007, Red Hat Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Library General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 import glob
20 import os
21 import sys
22 import logging
24 import yum
25 import rpmUtils
26 import pykickstart.parser
28 from imgcreate.errors import *
30 class TextProgress(object):
31 def start(self, filename, url, *args, **kwargs):
32 sys.stdout.write("Retrieving %s " % (url,))
33 self.url = url
34 def update(self, *args):
35 pass
36 def end(self, *args):
37 sys.stdout.write("...OK\n")
39 class LiveCDYum(yum.YumBase):
40 def __init__(self, releasever=None):
41 """
42 releasever = optional value to use in replacing $releasever in repos
43 """
44 yum.YumBase.__init__(self)
45 self.releasever = releasever
47 def doFileLogSetup(self, uid, logfile):
48 # don't do the file log for the livecd as it can lead to open fds
49 # being left and an inability to clean up after ourself
50 pass
52 def close(self):
53 try:
54 os.unlink(self.conf.installroot + "/yum.conf")
55 except:
56 pass
57 yum.YumBase.close(self)
59 def __del__(self):
60 pass
62 def _writeConf(self, confpath, installroot):
63 conf = "[main]\n"
64 conf += "installroot=%s\n" % installroot
65 conf += "cachedir=/var/cache/yum\n"
66 conf += "plugins=0\n"
67 conf += "reposdir=\n"
68 conf += "failovermethod=priority\n"
69 conf += "keepcache=1\n"
71 f = file(confpath, "w+")
72 f.write(conf)
73 f.close()
75 os.chmod(confpath, 0644)
77 def _cleanupRpmdbLocks(self, installroot):
78 # cleans up temporary files left by bdb so that differing
79 # versions of rpm don't cause problems
80 for f in glob.glob(installroot + "/var/lib/rpm/__db*"):
81 os.unlink(f)
83 def setup(self, confpath, installroot):
84 self._writeConf(confpath, installroot)
85 self._cleanupRpmdbLocks(installroot)
86 self.doConfigSetup(fn = confpath, root = installroot)
87 self.conf.cache = 0
88 self.doTsSetup()
89 self.doRpmDBSetup()
90 self.doRepoSetup()
91 self.doSackSetup()
93 def selectPackage(self, pkg):
94 """Select a given package. Can be specified with name.arch or name*"""
95 return self.install(pattern = pkg)
97 def deselectPackage(self, pkg):
98 """Deselect package. Can be specified as name.arch or name*"""
99 sp = pkg.rsplit(".", 2)
100 txmbrs = []
101 if len(sp) == 2:
102 txmbrs = self.tsInfo.matchNaevr(name=sp[0], arch=sp[1])
104 if len(txmbrs) == 0:
105 exact, match, unmatch = yum.packages.parsePackages(self.pkgSack.returnPackages(), [pkg], casematch=1)
106 for p in exact + match:
107 txmbrs.append(p)
109 if len(txmbrs) > 0:
110 for x in txmbrs:
111 self.tsInfo.remove(x.pkgtup)
112 # we also need to remove from the conditionals
113 # dict so that things don't get pulled back in as a result
114 # of them. yes, this is ugly. conditionals should die.
115 for req, pkgs in self.tsInfo.conditionals.iteritems():
116 if x in pkgs:
117 pkgs.remove(x)
118 self.tsInfo.conditionals[req] = pkgs
119 else:
120 logging.warn("No such package %s to remove" %(pkg,))
122 def selectGroup(self, grp, include = pykickstart.parser.GROUP_DEFAULT):
123 # default to getting mandatory and default packages from a group
124 # unless we have specific options from kickstart
125 package_types = ['mandatory', 'default']
126 if include == pykickstart.parser.GROUP_REQUIRED:
127 package_types.remove('default')
128 elif include == pykickstart.parser.GROUP_ALL:
129 package_types.append('optional')
130 yum.YumBase.selectGroup(self, grp, group_package_types=package_types)
132 def addRepository(self, name, url = None, mirrorlist = None):
133 def _varSubstitute(option):
134 # takes a variable and substitutes like yum configs do
135 option = option.replace("$basearch", rpmUtils.arch.getBaseArch())
136 option = option.replace("$arch", rpmUtils.arch.getCanonArch())
137 # If the url includes $releasever substitute user's value or
138 # current system's version.
139 if option.find("$releasever") > -1:
140 if self.releasever:
141 option = option.replace("$releasever", self.releasever)
142 else:
143 try:
144 option = option.replace("$releasever", yum.config._getsysver("/", "redhat-release"))
145 except yum.Errors.YumBaseError:
146 raise CreatorError("$releasever in repo url, but no releasever set")
147 return option
149 repo = yum.yumRepo.YumRepository(name)
150 if url:
151 repo.baseurl.append(_varSubstitute(url))
152 if mirrorlist:
153 repo.mirrorlist = _varSubstitute(mirrorlist)
154 conf = yum.config.RepoConf()
155 for k, v in conf.iteritems():
156 if v or not hasattr(repo, k):
157 repo.setAttribute(k, v)
158 repo.basecachedir = self.conf.cachedir
159 repo.failovermethod = "priority"
160 repo.metadata_expire = 0
161 repo.mirrorlist_expire = 0
162 repo.timestamp_check = 0
163 # disable gpg check???
164 repo.gpgcheck = 0
165 repo.enable()
166 repo.setup(0)
167 repo.setCallback(TextProgress())
168 self.repos.add(repo)
169 return repo
171 def installHasFile(self, file):
172 provides_pkg = self.whatProvides(file, None, None)
173 dlpkgs = map(lambda x: x.po, filter(lambda txmbr: txmbr.ts_state in ("i", "u"), self.tsInfo.getMembers()))
174 for p in dlpkgs:
175 for q in provides_pkg:
176 if (p == q):
177 return True
178 return False
181 def runInstall(self):
182 os.environ["HOME"] = "/"
183 try:
184 (res, resmsg) = self.buildTransaction()
185 except yum.Errors.RepoError, e:
186 raise CreatorError("Unable to download from repo : %s" %(e,))
187 # Empty transactions are generally fine, we might be rebuilding an
188 # existing image with no packages added
189 if resmsg and resmsg[0].endswith(" - empty transaction"):
190 return res
191 if res != 2:
192 raise CreatorError("Failed to build transaction : %s" % str.join("\n", resmsg))
194 dlpkgs = map(lambda x: x.po, filter(lambda txmbr: txmbr.ts_state in ("i", "u"), self.tsInfo.getMembers()))
195 self.downloadPkgs(dlpkgs)
196 # FIXME: sigcheck?
198 self.initActionTs()
199 self.populateTs(keepold=0)
200 deps = self.ts.check()
201 if len(deps) != 0:
202 raise CreatorError("Dependency check failed!")
203 rc = self.ts.order()
204 if rc != 0:
205 raise CreatorError("ordering packages for installation failed!")
207 # FIXME: callback should be refactored a little in yum
208 sys.path.append('/usr/share/yum-cli')
209 import yum.misc
210 yum.misc.setup_locale()
211 import callback
212 cb = callback.RPMInstallCallback()
213 cb.tsInfo = self.tsInfo
214 cb.filelog = False
215 ret = self.runTransaction(cb)
216 print ""
217 self._cleanupRpmdbLocks(self.conf.installroot)
218 return ret