Add support for the --priority, --includepkgs, and --excludepkgs options.
[pykickstart.git] / pykickstart / commands / repo.py
bloba2d723a1b95c0e0a73fdb4d27b040163037b1202
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 pykickstart.base import *
21 from pykickstart.constants import *
22 from pykickstart.errors import *
23 from pykickstart.options import *
25 import string
26 from rhpl.translate import _
27 import rhpl.translate as translate
29 translate.textdomain("pykickstart")
31 class FC6_RepoData(BaseData):
32 def __init__(self, baseurl="", mirrorlist="", name=""):
33 BaseData.__init__(self)
34 self.baseurl = baseurl
35 self.mirrorlist = mirrorlist
36 self.name = name
38 def __str__(self):
39 if self.baseurl:
40 urlopt = "--baseurl=%s" % self.baseurl
41 elif self.mirrorlist:
42 urlopt = "--mirrorlist=%s" % self.mirrorlist
44 return "repo --name=%s %s\n" % (self.name, urlopt)
46 class F8_RepoData(FC6_RepoData):
47 def __init__(self, baseurl="", mirrorlist="", name="", priority=None,
48 includepkgs=[], excludepkgs=[]):
49 FC6_RepoData.__init__(self, baseurl=baseurl, mirrorlist=mirrorlist,
50 name=name)
51 self.priority = priority
52 self.includepkgs = includepkgs
53 self.excludepkgs = excludepkgs
55 def __str__(self):
56 str = FC6_RepoData.__str__(self).rstrip()
58 if self.priority:
59 str += " --priority=%s" % self.priority
60 if self.includepkgs:
61 str += " --includepkgs=\"%s\"" % string.join(self.includepkgs, ",")
62 if self.excludepkgs:
63 str += " --excludepkgs=\"%s\"" % string.join(self.excludepkgs, ",")
65 return str + "\n"
67 class FC6_Repo(KickstartCommand):
68 def __init__(self, writePriority=0, repoList=None):
69 KickstartCommand.__init__(self, writePriority)
71 if repoList == None:
72 repoList = []
74 self.repoList = repoList
76 def __str__(self):
77 retval = ""
78 for repo in self.repoList:
79 retval += repo.__str__()
81 return retval
83 def parse(self, args):
84 op = KSOptionParser(lineno=self.lineno)
85 op.add_option("--name", dest="name", required=1)
86 op.add_option("--baseurl")
87 op.add_option("--mirrorlist")
89 (opts, extra) = op.parse_args(args=args)
91 # This is lame, but I can't think of a better way to make sure only
92 # one of these two is specified.
93 if opts.baseurl and opts.mirrorlist:
94 raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("Only one of --baseurl and --mirrorlist may be specified for repo command."))
96 if not opts.baseurl and not opts.mirrorlist:
97 raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("One of --baseurl or --mirrorlist must be specified for repo command."))
99 rd = FC6_RepoData()
100 self._setToObj(op, opts, rd)
101 self.add(rd)
103 def add(self, newObj):
104 self.repoList.append(newObj)
106 class F8_Repo(FC6_Repo):
107 def __init__(self, writePriority=0, repoList=None):
108 FC6_Repo.__init__(self, writePriority, repoList)
110 def __str__(self):
111 retval = ""
112 for repo in self.repoList:
113 retval += repo.__str__()
115 return retval
117 def parse(self, args):
118 def list_cb (option, opt_str, value, parser):
119 for d in value.split(','):
120 parser.values.ensure_value(option.dest, []).append(d)
122 op = KSOptionParser(lineno=self.lineno)
123 op.add_option("--name", dest="name", required=1)
124 op.add_option("--baseurl")
125 op.add_option("--mirrorlist")
126 op.add_option("--priority", action="store", type="int")
127 op.add_option("--excludepkgs", action="callback", callback=list_cb,
128 nargs=1, type="string")
129 op.add_option("--includepkgs", action="callback", callback=list_cb,
130 nargs=1, type="string")
132 (opts, extra) = op.parse_args(args=args)
134 # This is lame, but I can't think of a better way to make sure only
135 # one of these two is specified.
136 if opts.baseurl and opts.mirrorlist:
137 raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("Only one of --baseurl and --mirrorlist may be specified for repo command."))
139 if not opts.baseurl and not opts.mirrorlist:
140 raise KickstartValueError, formatErrorMsg(self.lineno, msg=_("One of --baseurl or --mirrorlist must be specified for repo command."))
142 rd = F8_RepoData()
143 self._setToObj(op, opts, rd)
144 self.add(rd)