move pykickstart -> imgcreate.pykickstart
[pykickstart/EL-5.git] / pykickstart / commands / repo.py
blob1b2d2684a668b8c2e4ec330e6fa25513a3ccd166
2 # Chris Lumens <clumens@redhat.com>
4 # Copyright 2007 Red Hat, Inc.
6 # This copyrighted material is made available to anyone wishing to use, modify,
7 # copy, or redistribute it subject to the terms and conditions of the GNU
8 # General Public License v.2. This program is distributed in the hope that it
9 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
10 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
16 # trademarks that are incorporated in the source code or documentation are not
17 # subject to the GNU General Public License and may only be used or replicated
18 # with the express permission of Red Hat, Inc.
20 from imgcreate.pykickstart.base import *
21 from imgcreate.pykickstart.constants import *
22 from imgcreate.pykickstart.errors import *
23 from imgcreate.pykickstart.options import *
25 import string
27 import gettext
28 _ = lambda x: gettext.ldgettext("pykickstart", x)
30 class FC6_RepoData(BaseData):
31 def __init__(self, baseurl="", mirrorlist=None, name=""):
32 BaseData.__init__(self)
33 self.baseurl = baseurl
34 self.mirrorlist = mirrorlist
35 self.name = name
37 def _getArgsAsStr(self):
38 retval = ""
40 if self.baseurl:
41 retval += "--baseurl=%s" % self.baseurl
42 elif self.mirrorlist:
43 retval += "--mirrorlist=%s" % self.mirrorlist
45 return retval
47 def __str__(self):
48 return "repo --name=%s %s\n" % (self.name, self._getArgsAsStr())
50 class F8_RepoData(FC6_RepoData):
51 def __init__(self, baseurl="", mirrorlist=None, name="", cost=None,
52 includepkgs=[], excludepkgs=[]):
53 FC6_RepoData.__init__(self, baseurl=baseurl, mirrorlist=mirrorlist,
54 name=name)
55 self.cost = cost
56 self.includepkgs = includepkgs
57 self.excludepkgs = excludepkgs
59 def _getArgsAsStr(self):
60 retval = FC6_RepoData._getArgsAsStr(self)
62 if self.cost:
63 retval += " --cost=%s" % self.cost
64 if self.includepkgs:
65 retval += " --includepkgs=\"%s\"" % string.join(self.includepkgs, ",")
66 if self.excludepkgs:
67 retval += " --excludepkgs=\"%s\"" % string.join(self.excludepkgs, ",")
69 return retval
71 class FC6_Repo(KickstartCommand):
72 def __init__(self, writePriority=0, repoList=None):
73 KickstartCommand.__init__(self, writePriority)
74 self.op = self._getParser()
76 if repoList == None:
77 repoList = []
79 self.repoList = repoList
81 def __str__(self):
82 retval = ""
83 for repo in self.repoList:
84 retval += repo.__str__()
86 return retval
88 def _getParser(self):
89 op = KSOptionParser(lineno=self.lineno)
90 op.add_option("--name", dest="name", required=1)
91 op.add_option("--baseurl")
92 op.add_option("--mirrorlist")
93 return op
95 def parse(self, args):
96 (opts, extra) = self.op.parse_args(args=args)
98 # This is lame, but I can't think of a better way to make sure only
99 # one of these two is specified.
100 if opts.baseurl and opts.mirrorlist:
101 raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("Only one of --baseurl and --mirrorlist may be specified for repo command."))
103 if not opts.baseurl and not opts.mirrorlist:
104 raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("One of --baseurl or --mirrorlist must be specified for repo command."))
106 rd = self.handler.RepoData()
107 self._setToObj(self.op, opts, rd)
108 self.add(rd)
110 def add(self, newObj):
111 self.repoList.append(newObj)
113 class F8_Repo(FC6_Repo):
114 def __init__(self, writePriority=0, repoList=None):
115 FC6_Repo.__init__(self, writePriority, repoList)
117 def __str__(self):
118 retval = ""
119 for repo in self.repoList:
120 retval += repo.__str__()
122 return retval
124 def _getParser(self):
125 def list_cb (option, opt_str, value, parser):
126 for d in value.split(','):
127 parser.values.ensure_value(option.dest, []).append(d)
129 op = FC6_Repo._getParser(self)
130 op.add_option("--cost", action="store", type="int")
131 op.add_option("--excludepkgs", action="callback", callback=list_cb,
132 nargs=1, type="string")
133 op.add_option("--includepkgs", action="callback", callback=list_cb,
134 nargs=1, type="string")
135 return op
137 def methodToRepo(self):
138 if not self.handler.method.url:
139 raise KickstartError, formatErrorMsg(self.handler.method.lineno, msg=_("Method must be a url to be added to the repo list."))
140 reponame = "ks-method-url"
141 repourl = self.handler.method.url
142 rd = self.handler.RepoData(name=reponame, baseurl=repourl)
143 self.add(rd)