Require python-urlgrabber for ksvalidator.
[pykickstart.git] / validator / ksvalidator
blob04cf63676279f1053798ab34dbe34b316272c3f4
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.data import *
23 from pykickstart.parser import *
25 from rhpl.translate import _
26 import rhpl.translate as translate
28 translate.textdomain("pykickstart")
30 def cleanup(destdir, exitval=1):
31     shutil.rmtree(destdir)
32     os._exit(exitval)
34 op = OptionParser(usage="usage: %prog [options] ksfile|url")
35 op.add_option("-e", "--firsterror", dest="firsterror", action="store_true",
36               default=False, help=_("halt after the first error or warning"))
37 op.add_option("-i", "--followincludes", dest="followincludes",
38               action="store_true", default=False,
39               help=_("parse include files when %include is seen"))
41 (opts, extra) = op.parse_args(sys.argv[1:])
43 if len(extra) != 1:
44     op.print_help()
45     os._exit(1)
46 else:
47     destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
48     f = urlgrabber.urlgrab(extra[0], filename="%s/ks.cfg" % destdir)
50 ksdata = KickstartData()
51 kshandlers = KickstartHandlers(ksdata)
52 ksparser = KickstartParser(ksdata, kshandlers,
53                            followIncludes=opts.followincludes,
54                            errorsAreFatal=opts.firsterror)
56 # turn DeprecationWarnings into errors
57 warnings.filterwarnings("error")
59 try:
60     ksparser.readKickstart(f)
61 except DeprecationWarning, msg:
62     print _("File uses a deprecated option or command.\n%s") % msg
63     cleanup(destdir)
64 except (KickstartParseError, KickstartValueError), msg:
65     print msg
66     cleanup(destdir)
67 except KickstartError:
68     print _("General kickstart error in input file")
69     cleanup(destdir)
70 except Exception, e:
71     print _("General error in input file:  %s") % e
72     cleanup(destdir)
74 cleanup(destdir, exitval=0)