KickstartParser needs to accept a version argument as well.
[pykickstart.git] / validator / ksvalidator
blob170a5be4962c973e59852052f45f7aa96cb57741
1 #!/usr/bin/python2
3 # Chris Lumens <clumens@redhat.com>
5 # Copyright 2005, 2006 Red Hat, Inc.
7 # This software may be freely redistributed under the terms of the GNU
8 # general public license.
10 # You should have received a copy of the GNU General Public License
11 # along with this program; if not, write to the Free Software
12 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 import optparse
16 import os
17 import sys
18 import warnings
19 import tempfile
20 import urlgrabber
21 import shutil
22 from pykickstart.errors import *
23 from pykickstart.parser import *
24 from pykickstart.version import *
26 from rhpl.translate import _
27 import rhpl.translate as translate
29 translate.textdomain("pykickstart")
31 def cleanup(destdir, exitval=1):
32     shutil.rmtree(destdir)
33     os._exit(exitval)
35 op = OptionParser(usage="usage: %prog [options] ksfile|url")
36 op.add_option("-e", "--firsterror", dest="firsterror", action="store_true",
37               default=False, help=_("halt after the first error or warning"))
38 op.add_option("-i", "--followincludes", dest="followincludes",
39               action="store_true", default=False,
40               help=_("parse include files when %include is seen"))
41 op.add_option("-v", "--version", dest="version", default=DEVEL,
42               help=_("version of kickstart syntax to validate against"))
44 (opts, extra) = op.parse_args(sys.argv[1:])
46 if len(extra) != 1:
47     op.print_help()
48     os._exit(1)
49 else:
50     destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
51     f = urlgrabber.urlgrab(extra[0], filename="%s/ks.cfg" % destdir)
53 try:
54     handler = makeVersion(opts.version)
55 except KickstartVersionError:
56     print _("The version %s is not supported by pykickstart" % opts.version)
57     cleanup(destdir)
59 ksparser = KickstartParser(handler, version=opts.version,
60                            followIncludes=opts.followincludes,
61                            errorsAreFatal=opts.firsterror)
63 # turn DeprecationWarnings into errors
64 warnings.filterwarnings("error")
66 try:
67     ksparser.readKickstart(f)
68 except DeprecationWarning, msg:
69     print _("File uses a deprecated option or command.\n%s") % msg
70     cleanup(destdir)
71 except (KickstartParseError, KickstartValueError), msg:
72     print msg
73     cleanup(destdir)
74 except KickstartError:
75     print _("General kickstart error in input file")
76     cleanup(destdir)
77 except Exception, e:
78     print _("General error in input file:  %s") % e
79     cleanup(destdir)
81 cleanup(destdir, exitval=0)