Fix F18/F19 cdrom methods
[pykickstart.git] / tools / ksvalidator
blob9f95b6b444296123860c0f548bcd09a793eb6865
1 #!/usr/bin/python2
3 # Chris Lumens <clumens@redhat.com>
5 # Copyright 2005, 2006, 2007 Red Hat, Inc.
7 # This copyrighted material is made available to anyone wishing to use, modify,
8 # copy, or redistribute it subject to the terms and conditions of the GNU
9 # General Public License v.2.  This program is distributed in the hope that it
10 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
11 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 # See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, write to the Free Software Foundation, Inc., 51
16 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat
17 # trademarks that are incorporated in the source code or documentation are not
18 # subject to the GNU General Public License and may only be used or replicated
19 # with the express permission of Red Hat, Inc. 
22 import optparse
23 import os
24 import sys
25 import warnings
26 import tempfile
27 import urlgrabber
28 import shutil
29 from pykickstart.errors import *
30 from pykickstart.parser import *
31 from pykickstart.version import *
33 import gettext
34 gettext.install("pykickstart", unicode=True)
36 def cleanup(destdir, file=None, exitval=1):
37     shutil.rmtree(destdir)
39     # Don't care if this file doesn't exist.
40     if file is not None:
41        try:
42            os.remove(file)
43        except:
44            pass
46     sys.exit(exitval)
48 op = OptionParser(usage="usage: %prog [options] ksfile|url")
49 op.add_option("-e", "--firsterror", dest="firsterror", action="store_true",
50               default=False, help=_("halt after the first error or warning"))
51 op.add_option("-i", "--followincludes", dest="followincludes",
52               action="store_true", default=False,
53               help=_("parse include files when %include is seen"))
54 op.add_option("-l", "--listversions", dest="listversions", action="store_true",
55               default=False,
56               help=_("list the available versions of kickstart syntax"))
57 op.add_option("-v", "--version", dest="version", default=DEVEL,
58               help=_("version of kickstart syntax to validate against"))
60 (opts, extra) = op.parse_args(sys.argv[1:])
62 if opts.listversions:
63     for key in sorted(versionMap.keys()):
64         print key
66     sys.exit(1)
68 if len(extra) != 1:
69     op.print_help()
70     sys.exit(1)
71 else:
72     destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
73     try:
74         f = urlgrabber.urlgrab(extra[0], filename="%s/ks.cfg" % destdir)
75     except urlgrabber.grabber.URLGrabError, e:
76         print _("Error reading %s:\n%s" % (extra[0],e))
77         cleanup(destdir)
79 try:
80     handler = makeVersion(opts.version)
81 except KickstartVersionError:
82     print _("The version %s is not supported by pykickstart" % opts.version)
83     cleanup(destdir)
85 ksparser = KickstartParser(handler, followIncludes=opts.followincludes,
86                            errorsAreFatal=opts.firsterror)
88 # turn DeprecationWarnings into errors
89 warnings.filterwarnings("error")
91 processedFile = None
93 try:
94     processedFile = preprocessKickstart(f)
95     ksparser.readKickstart(processedFile)
96     cleanup(destdir, processedFile, exitval=0)
97 except DeprecationWarning, msg:
98     print _("File uses a deprecated option or command.\n%s") % msg
99     cleanup(destdir, processedFile)
100 except (KickstartParseError, KickstartValueError), msg:
101     print msg
102     cleanup(destdir, processedFile)
103 except KickstartError:
104     print _("General kickstart error in input file")
105     cleanup(destdir, processedFile)
106 except Exception, e:
107     print _("General error in input file:  %s") % e
108     cleanup(destdir, processedFile)