Add the kickstart documentation from the Fedora wiki.
[pykickstart.git] / validator / ksvalidator
blob5e9590e73f2d5e818bb1e9bde62dd16881cfc7e7
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 from rhpl.translate import _
34 import rhpl.translate as translate
36 translate.textdomain("pykickstart")
38 def cleanup(destdir, file=None, exitval=1):
39     shutil.rmtree(destdir)
41     # Don't care if this file doesn't exist.
42     if file is not None:
43        try:
44            os.remove(file)
45        except:
46            pass
48     os._exit(exitval)
50 op = OptionParser(usage="usage: %prog [options] ksfile|url")
51 op.add_option("-e", "--firsterror", dest="firsterror", action="store_true",
52               default=False, help=_("halt after the first error or warning"))
53 op.add_option("-i", "--followincludes", dest="followincludes",
54               action="store_true", default=False,
55               help=_("parse include files when %include is seen"))
56 op.add_option("-v", "--version", dest="version", default=DEVEL,
57               help=_("version of kickstart syntax to validate against"))
59 (opts, extra) = op.parse_args(sys.argv[1:])
61 if len(extra) != 1:
62     op.print_help()
63     os._exit(1)
64 else:
65     destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
66     f = urlgrabber.urlgrab(extra[0], filename="%s/ks.cfg" % destdir)
68 try:
69     handler = makeVersion(opts.version)
70 except KickstartVersionError:
71     print _("The version %s is not supported by pykickstart" % opts.version)
72     cleanup(destdir)
74 ksparser = KickstartParser(handler, followIncludes=opts.followincludes,
75                            errorsAreFatal=opts.firsterror)
77 # turn DeprecationWarnings into errors
78 warnings.filterwarnings("error")
80 processedFile = None
82 try:
83     processedFile = preprocessKickstart(f)
84     ksparser.readKickstart(processedFile)
85     cleanup(destdir, processedFile, exitval=0)
86 except DeprecationWarning, msg:
87     print _("File uses a deprecated option or command.\n%s") % msg
88     cleanup(destdir, processedFile, exitval=0)
89 except (KickstartParseError, KickstartValueError), msg:
90     print msg
91     cleanup(destdir, processedFile, exitval=0)
92 except KickstartError:
93     print _("General kickstart error in input file")
94     cleanup(destdir, processedFile, exitval=0)
95 except Exception, e:
96     print _("General error in input file:  %s") % e
97     cleanup(destdir, processedFile)