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.
25 import pykickstart
.parser
27 from imgcreate
.errors
import *
29 class TextProgress(object):
30 def start(self
, filename
, url
, *args
, **kwargs
):
31 sys
.stdout
.write("Retrieving %s " % (url
,))
33 def update(self
, *args
):
36 sys
.stdout
.write("...OK\n")
38 class LiveCDYum(yum
.YumBase
):
40 yum
.YumBase
.__init
__(self
)
42 def doFileLogSetup(self
, uid
, logfile
):
43 # don't do the file log for the livecd as it can lead to open fds
44 # being left and an inability to clean up after ourself
49 os
.unlink(self
.conf
.installroot
+ "/yum.conf")
52 yum
.YumBase
.close(self
)
57 def _writeConf(self
, confpath
, installroot
):
59 conf
+= "installroot=%s\n" % installroot
60 conf
+= "cachedir=/var/cache/yum\n"
63 conf
+= "failovermethod=priority\n"
65 f
= file(confpath
, "w+")
69 os
.chmod(confpath
, 0644)
71 def setup(self
, confpath
, installroot
):
72 self
._writeConf
(confpath
, installroot
)
73 self
.doConfigSetup(fn
= confpath
, root
= installroot
)
80 def selectPackage(self
, pkg
):
81 """Select a given package. Can be specified with name.arch or name*"""
82 return self
.install(pattern
= pkg
)
84 def deselectPackage(self
, pkg
):
85 """Deselect package. Can be specified as name.arch or name*"""
86 sp
= pkg
.rsplit(".", 2)
89 txmbrs
= self
.tsInfo
.matchNaevr(name
=sp
[0], arch
=sp
[1])
92 exact
, match
, unmatch
= yum
.packages
.parsePackages(self
.pkgSack
.returnPackages(), [pkg
], casematch
=1)
93 for p
in exact
+ match
:
98 self
.tsInfo
.remove(x
.pkgtup
)
99 # we also need to remove from the conditionals
100 # dict so that things don't get pulled back in as a result
101 # of them. yes, this is ugly. conditionals should die.
102 for req
, pkgs
in self
.tsInfo
.conditionals
.iteritems():
105 self
.tsInfo
.conditionals
[req
] = pkgs
107 logging
.warn("No such package %s to remove" %(pkg
,))
109 def selectGroup(self
, grp
, include
= pykickstart
.parser
.GROUP_DEFAULT
):
110 yum
.YumBase
.selectGroup(self
, grp
)
111 if include
== pykickstart
.parser
.GROUP_REQUIRED
:
112 map(lambda p
: self
.deselectPackage(p
), grp
.default_packages
.keys())
113 elif include
== pykickstart
.parser
.GROUP_ALL
:
114 map(lambda p
: self
.selectPackage(p
), grp
.optional_packages
.keys())
116 def addRepository(self
, name
, url
= None, mirrorlist
= None):
117 def _varSubstitute(option
):
118 # takes a variable and substitutes like yum configs do
119 option
= option
.replace("$basearch", rpmUtils
.arch
.getBaseArch())
120 option
= option
.replace("$arch", rpmUtils
.arch
.getCanonArch())
123 repo
= yum
.yumRepo
.YumRepository(name
)
125 repo
.baseurl
.append(_varSubstitute(url
))
127 repo
.mirrorlist
= _varSubstitute(mirrorlist
)
128 conf
= yum
.config
.RepoConf()
129 for k
, v
in conf
.iteritems():
130 if v
or not hasattr(repo
, k
):
131 repo
.setAttribute(k
, v
)
132 repo
.basecachedir
= self
.conf
.cachedir
133 repo
.failovermethod
= "priority"
134 repo
.metadata_expire
= 0
135 # disable gpg check???
139 repo
.setCallback(TextProgress())
143 def installHasFile(self
, file):
144 provides_pkg
= self
.whatProvides(file, None, None)
145 dlpkgs
= map(lambda x
: x
.po
, filter(lambda txmbr
: txmbr
.ts_state
in ("i", "u"), self
.tsInfo
.getMembers()))
147 for q
in provides_pkg
:
153 def runInstall(self
):
154 os
.environ
["HOME"] = "/"
156 (res
, resmsg
) = self
.buildTransaction()
157 except yum
.Errors
.RepoError
, e
:
158 raise CreatorError("Unable to download from repo : %s" %(e
,))
160 raise CreatorError("Failed to build transaction : %s" % str.join("\n", resmsg
))
162 dlpkgs
= map(lambda x
: x
.po
, filter(lambda txmbr
: txmbr
.ts_state
in ("i", "u"), self
.tsInfo
.getMembers()))
163 self
.downloadPkgs(dlpkgs
)
167 self
.populateTs(keepold
=0)
168 deps
= self
.ts
.check()
170 raise CreatorError("Dependency check failed!")
173 raise CreatorError("ordering packages for installation failed!")
175 # FIXME: callback should be refactored a little in yum
176 sys
.path
.append('/usr/share/yum-cli')
178 cb
= callback
.RPMInstallCallback()
179 cb
.tsInfo
= self
.tsInfo
181 ret
= self
.runTransaction(cb
)