Merge release-0.28.17 from 'tools/mic'
[platform/upstream/mic.git] / mic / 3rdparty / pykickstart / base.py
1 #
2 # Chris Lumens <clumens@redhat.com>
3 #
4 # Copyright 2006, 2007, 2008 Red Hat, Inc.
5 #
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.
12 #
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. 
19 #
20 """
21 Base classes for creating commands and syntax version object.
22
23 This module exports several important base classes:
24
25     BaseData - The base abstract class for all data objects.  Data objects
26                are contained within a BaseHandler object.
27
28     BaseHandler - The base abstract class from which versioned kickstart
29                   handler are derived.  Subclasses of BaseHandler hold
30                   BaseData and KickstartCommand objects.
31
32     DeprecatedCommand - An abstract subclass of KickstartCommand that should
33                         be further subclassed by users of this module.  When
34                         a subclass is used, a warning message will be
35                         printed.
36
37     KickstartCommand - The base abstract class for all kickstart commands.
38                        Command objects are contained within a BaseHandler
39                        object.
40 """
41 import gettext
42 gettext.textdomain("pykickstart")
43 _ = lambda x: gettext.ldgettext("pykickstart", x)
44
45 import types
46 import warnings
47 from pykickstart.errors import *
48 from pykickstart.ko import *
49 from pykickstart.parser import Packages,TpkPackages
50 from pykickstart.version import versionToString
51
52 ###
53 ### COMMANDS
54 ###
55 class KickstartCommand(KickstartObject):
56     """The base class for all kickstart commands.  This is an abstract class."""
57     removedKeywords = []
58     removedAttrs = []
59
60     def __init__(self, writePriority=0, *args, **kwargs):
61         """Create a new KickstartCommand instance.  This method must be
62            provided by all subclasses, but subclasses must call
63            KickstartCommand.__init__ first.  Instance attributes:
64
65            currentCmd    -- The name of the command in the input file that
66                             caused this handler to be run.
67            currentLine   -- The current unprocessed line from the input file
68                             that caused this handler to be run.
69            handler       -- A reference to the BaseHandler subclass this
70                             command is contained withing.  This is needed to
71                             allow referencing of Data objects.
72            lineno        -- The current line number in the input file.
73            writePriority -- An integer specifying when this command should be
74                             printed when iterating over all commands' __str__
75                             methods.  The higher the number, the later this
76                             command will be written.  All commands with the
77                             same priority will be written alphabetically.
78         """
79
80         # We don't want people using this class by itself.
81         if self.__class__ is KickstartCommand:
82             raise TypeError ("KickstartCommand is an abstract class.")
83
84         KickstartObject.__init__(self, *args, **kwargs)
85
86         self.writePriority = writePriority
87
88         # These will be set by the dispatcher.
89         self.currentCmd = ""
90         self.currentLine = ""
91         self.handler = None
92         self.lineno = 0
93
94         # If a subclass provides a removedKeywords list, remove all the
95         # members from the kwargs list before we start processing it.  This
96         # ensures that subclasses don't continue to recognize arguments that
97         # were removed.
98         for arg in filter(kwargs.has_key, self.removedKeywords):
99             kwargs.pop(arg)
100
101     def __call__(self, *args, **kwargs):
102         """Set multiple attributes on a subclass of KickstartCommand at once
103            via keyword arguments.  Valid attributes are anything specified in
104            a subclass, but unknown attributes will be ignored.
105         """
106         for (key, val) in kwargs.items():
107             # Ignore setting attributes that were removed in a subclass, as
108             # if they were unknown attributes.
109             if key in self.removedAttrs:
110                 continue
111
112             if hasattr(self, key):
113                 setattr(self, key, val)
114
115     def __str__(self):
116         """Return a string formatted for output to a kickstart file.  This
117            method must be provided by all subclasses.
118         """
119         return KickstartObject.__str__(self)
120
121     def parse(self, args):
122         """Parse the list of args and set data on the KickstartCommand object.
123            This method must be provided by all subclasses.
124         """
125         raise TypeError ("parse() not implemented for KickstartCommand")
126
127     def apply(self, instroot="/"):
128         """Write out the configuration related to the KickstartCommand object.
129            Subclasses which do not provide this method will not have their
130            configuration written out.
131         """
132         return
133
134     def dataList(self):
135         """For commands that can occur multiple times in a single kickstart
136            file (like network, part, etc.), return the list that we should
137            append more data objects to.
138         """
139         return None
140
141     def deleteRemovedAttrs(self):
142         """Remove all attributes from self that are given in the removedAttrs
143            list.  This method should be called from __init__ in a subclass,
144            but only after the superclass's __init__ method has been called.
145         """
146         for attr in filter(lambda k: hasattr(self, k), self.removedAttrs):
147             delattr(self, attr)
148
149     # Set the contents of the opts object (an instance of optparse.Values
150     # returned by parse_args) as attributes on the KickstartCommand object.
151     # It's useful to call this from KickstartCommand subclasses after parsing
152     # the arguments.
153     def _setToSelf(self, optParser, opts):
154         self._setToObj(optParser, opts, self)
155
156     # Sets the contents of the opts object (an instance of optparse.Values
157     # returned by parse_args) as attributes on the provided object obj.  It's
158     # useful to call this from KickstartCommand subclasses that handle lists
159     # of objects (like partitions, network devices, etc.) and need to populate
160     # a Data object.
161     def _setToObj(self, optParser, opts, obj):
162         for key in filter (lambda k: getattr(opts, k) != None, optParser.keys()):
163             setattr(obj, key, getattr(opts, key))
164
165 class DeprecatedCommand(KickstartCommand):
166     """Specify that a command is deprecated and no longer has any function.
167        Any command that is deprecated should be subclassed from this class,
168        only specifying an __init__ method that calls the superclass's __init__.
169        This is an abstract class.
170     """
171     def __init__(self, writePriority=None, *args, **kwargs):
172         # We don't want people using this class by itself.
173         if self.__class__ is KickstartCommand:
174             raise TypeError ("DeprecatedCommand is an abstract class.")
175
176         # Create a new DeprecatedCommand instance.
177         KickstartCommand.__init__(self, writePriority, *args, **kwargs)
178
179     def __str__(self):
180         """Placeholder since DeprecatedCommands don't work anymore."""
181         return ""
182
183     def parse(self, args):
184         """Print a warning message if the command is seen in the input file."""
185         mapping = {"lineno": self.lineno, "cmd": self.currentCmd}
186         warnings.warn(_("Ignoring deprecated command on line %(lineno)s:  The %(cmd)s command has been deprecated and no longer has any effect.  It may be removed from future releases, which will result in a fatal error from kickstart.  Please modify your kickstart file to remove this command.") % mapping, DeprecationWarning)
187
188
189 ###
190 ### HANDLERS
191 ###
192 class BaseHandler(KickstartObject):
193     """Each version of kickstart syntax is provided by a subclass of this
194        class.  These subclasses are what users will interact with for parsing,
195        extracting data, and writing out kickstart files.  This is an abstract
196        class.
197
198        version -- The version this syntax handler supports.  This is set by
199                   a class attribute of a BaseHandler subclass and is used to
200                   set up the command dict.  It is for read-only use.
201     """
202     version = None
203
204     def __init__(self, mapping=None, dataMapping=None, commandUpdates=None,
205                  dataUpdates=None, *args, **kwargs):
206         """Create a new BaseHandler instance.  This method must be provided by
207            all subclasses, but subclasses must call BaseHandler.__init__ first.
208
209            mapping          -- A custom map from command strings to classes,
210                                useful when creating your own handler with
211                                special command objects.  It is otherwise unused
212                                and rarely needed.  If you give this argument,
213                                the mapping takes the place of the default one
214                                and so must include all commands you want
215                                recognized.
216            dataMapping      -- This is the same as mapping, but for data
217                                objects.  All the same comments apply.
218            commandUpdates   -- This is similar to mapping, but does not take
219                                the place of the defaults entirely.  Instead,
220                                this mapping is applied after the defaults and
221                                updates it with just the commands you want to
222                                modify.
223            dataUpdates      -- This is the same as commandUpdates, but for
224                                data objects.
225
226
227            Instance attributes:
228
229            commands -- A mapping from a string command to a KickstartCommand
230                        subclass object that handles it.  Multiple strings can
231                        map to the same object, but only one instance of the
232                        command object should ever exist.  Most users should
233                        never have to deal with this directly, as it is
234                        manipulated internally and called through dispatcher.
235            currentLine -- The current unprocessed line from the input file
236                           that caused this handler to be run.
237            packages -- An instance of pykickstart.parser.Packages which
238                        describes the packages section of the input file.
239            platform -- A string describing the hardware platform, which is
240                        needed only by system-config-kickstart.
241            scripts  -- A list of pykickstart.parser.Script instances, which is
242                        populated by KickstartParser.addScript and describes the
243                        %pre/%post/%traceback script section of the input file.
244         """
245
246         # We don't want people using this class by itself.
247         if self.__class__ is BaseHandler:
248             raise TypeError ("BaseHandler is an abstract class.")
249
250         KickstartObject.__init__(self, *args, **kwargs)
251
252         # This isn't really a good place for these, but it's better than
253         # everything else I can think of.
254         self.scripts = []
255         self.packages = Packages()
256         self.tpk_packages = TpkPackages()
257         self.platform = ""
258
259         # These will be set by the dispatcher.
260         self.commands = {}
261         self.currentLine = 0
262
263         # A dict keyed by an integer priority number, with each value being a
264         # list of KickstartCommand subclasses.  This dict is maintained by
265         # registerCommand and used in __str__.  No one else should be touching
266         # it.
267         self._writeOrder = {}
268
269         self._registerCommands(mapping, dataMapping, commandUpdates, dataUpdates)
270
271     def __str__(self):
272         """Return a string formatted for output to a kickstart file."""
273         retval = ""
274
275         if self.platform != "":
276             retval += "#platform=%s\n" % self.platform
277
278         retval += "#version=%s\n" % versionToString(self.version)
279
280         lst = self._writeOrder.keys()
281         lst.sort()
282
283         for prio in lst:
284             for obj in self._writeOrder[prio]:
285                 retval += obj.__str__()
286
287         for script in self.scripts:
288             retval += script.__str__()
289
290         retval += self.packages.__str__()
291
292         return retval
293
294     def _insertSorted(self, lst, obj):
295         length = len(lst)
296         i = 0
297
298         while i < length:
299             # If the two classes have the same name, it's because we are
300             # overriding an existing class with one from a later kickstart
301             # version, so remove the old one in favor of the new one.
302             if obj.__class__.__name__ > lst[i].__class__.__name__:
303                 i += 1
304             elif obj.__class__.__name__ == lst[i].__class__.__name__:
305                 lst[i] = obj
306                 return
307             elif obj.__class__.__name__ < lst[i].__class__.__name__:
308                 break
309
310         if i >= length:
311             lst.append(obj)
312         else:
313             lst.insert(i, obj)
314
315     def _setCommand(self, cmdObj):
316         # Add an attribute on this version object.  We need this to provide a
317         # way for clients to access the command objects.  We also need to strip
318         # off the version part from the front of the name.
319         if cmdObj.__class__.__name__.find("_") != -1:
320             name = unicode(cmdObj.__class__.__name__.split("_", 1)[1])
321         else:
322             name = unicode(cmdObj.__class__.__name__).lower()
323
324         setattr(self, name.lower(), cmdObj)
325
326         # Also, add the object into the _writeOrder dict in the right place.
327         if cmdObj.writePriority is not None:
328             if self._writeOrder.has_key(cmdObj.writePriority):
329                 self._insertSorted(self._writeOrder[cmdObj.writePriority], cmdObj)
330             else:
331                 self._writeOrder[cmdObj.writePriority] = [cmdObj]
332
333     def _registerCommands(self, mapping=None, dataMapping=None, commandUpdates=None,
334                           dataUpdates=None):
335         if mapping == {} or mapping == None:
336             from pykickstart.handlers.control import commandMap
337             cMap = commandMap[self.version]
338         else:
339             cMap = mapping
340
341         if dataMapping == {} or dataMapping == None:
342             from pykickstart.handlers.control import dataMap
343             dMap = dataMap[self.version]
344         else:
345             dMap = dataMapping
346
347         if type(commandUpdates) == types.DictType:
348             cMap.update(commandUpdates)
349
350         if type(dataUpdates) == types.DictType:
351             dMap.update(dataUpdates)
352
353         for (cmdName, cmdClass) in cMap.iteritems():
354             # First make sure we haven't instantiated this command handler
355             # already.  If we have, we just need to make another mapping to
356             # it in self.commands.
357             cmdObj = None
358
359             for (key, val) in self.commands.iteritems():
360                 if val.__class__.__name__ == cmdClass.__name__:
361                     cmdObj = val
362                     break
363
364             # If we didn't find an instance in self.commands, create one now.
365             if cmdObj == None:
366                 cmdObj = cmdClass()
367                 self._setCommand(cmdObj)
368
369             # Finally, add the mapping to the commands dict.
370             self.commands[cmdName] = cmdObj
371             self.commands[cmdName].handler = self
372
373         # We also need to create attributes for the various data objects.
374         # No checks here because dMap is a bijection.  At least, that's what
375         # the comment says.  Hope no one screws that up.
376         for (dataName, dataClass) in dMap.iteritems():
377             setattr(self, dataName, dataClass)
378
379     def dispatcher(self, args, lineno):
380         """Call the appropriate KickstartCommand handler for the current line
381            in the kickstart file.  A handler for the current command should
382            be registered, though a handler of None is not an error.  Returns
383            the data object returned by KickstartCommand.parse.
384
385            args    -- A list of arguments to the current command
386            lineno  -- The line number in the file, for error reporting
387         """
388         cmd = args[0]
389
390         if not self.commands.has_key(cmd):
391             raise KickstartParseError (formatErrorMsg(lineno, msg=_("Unknown command: %s" % cmd)))
392         elif self.commands[cmd] != None:
393             self.commands[cmd].currentCmd = cmd
394             self.commands[cmd].currentLine = self.currentLine
395             self.commands[cmd].lineno = lineno
396
397             # The parser returns the data object that was modified.  This could
398             # be a BaseData subclass that should be put into a list, or it
399             # could be the command handler object itself.
400             obj = self.commands[cmd].parse(args[1:])
401             lst = self.commands[cmd].dataList()
402             if lst is not None:
403                 lst.append(obj)
404
405             return obj
406
407     def maskAllExcept(self, lst):
408         """Set all entries in the commands dict to None, except the ones in
409            the lst.  All other commands will not be processed.
410         """
411         self._writeOrder = {}
412
413         for (key, val) in self.commands.iteritems():
414             if not key in lst:
415                 self.commands[key] = None
416
417     def hasCommand(self, cmd):
418         """Return true if there is a handler for the string cmd."""
419         return hasattr(self, cmd)
420
421
422 ###
423 ### DATA
424 ###
425 class BaseData(KickstartObject):
426     """The base class for all data objects.  This is an abstract class."""
427     removedKeywords = []
428     removedAttrs = []
429
430     def __init__(self, *args, **kwargs):
431         """Create a new BaseData instance.
432
433         lineno -- Line number in the ks-file where this object was defined
434         """
435
436         # We don't want people using this class by itself.
437         if self.__class__ is BaseData:
438             raise TypeError ("BaseData is an abstract class.")
439
440         KickstartObject.__init__(self, *args, **kwargs)
441         self.lineno = 0
442
443     def __str__(self):
444         """Return a string formatted for output to a kickstart file."""
445         return ""
446
447     def __call__(self, *args, **kwargs):
448         """Set multiple attributes on a subclass of BaseData at once via
449            keyword arguments.  Valid attributes are anything specified in a
450            subclass, but unknown attributes will be ignored.
451         """
452         for (key, val) in kwargs.items():
453             # Ignore setting attributes that were removed in a subclass, as
454             # if they were unknown attributes.
455             if key in self.removedAttrs:
456                 continue
457
458             if hasattr(self, key):
459                 setattr(self, key, val)
460
461     def deleteRemovedAttrs(self):
462         """Remove all attributes from self that are given in the removedAttrs
463            list.  This method should be called from __init__ in a subclass,
464            but only after the superclass's __init__ method has been called.
465         """
466         for attr in filter(lambda k: hasattr(self, k), self.removedAttrs):
467             delattr(self, attr)