Fix ksvalidator tracebacks.
[pykickstart.git] / validator / ksvalidator
blobe583b6dd949625faf62fe90f3dfe347311fd0dfb
1 #!/usr/bin/python2
3 # Chris Lumens <clumens@redhat.com>
5 # Copyright 2005, 2006, 2007 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, file=None, exitval=1):
32     shutil.rmtree(destdir)
34     # Don't care if this file doesn't exist.
35     if file is not None:
36        try:
37            os.remove(file)
38        except:
39            pass
41     os._exit(exitval)
43 op = OptionParser(usage="usage: %prog [options] ksfile|url")
44 op.add_option("-e", "--firsterror", dest="firsterror", action="store_true",
45               default=False, help=_("halt after the first error or warning"))
46 op.add_option("-i", "--followincludes", dest="followincludes",
47               action="store_true", default=False,
48               help=_("parse include files when %include is seen"))
49 op.add_option("-v", "--version", dest="version", default=DEVEL,
50               help=_("version of kickstart syntax to validate against"))
52 (opts, extra) = op.parse_args(sys.argv[1:])
54 if len(extra) != 1:
55     op.print_help()
56     os._exit(1)
57 else:
58     destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
59     f = urlgrabber.urlgrab(extra[0], filename="%s/ks.cfg" % destdir)
61 try:
62     handler = makeVersion(opts.version)
63 except KickstartVersionError:
64     print _("The version %s is not supported by pykickstart" % opts.version)
65     cleanup(destdir)
67 ksparser = KickstartParser(handler, followIncludes=opts.followincludes,
68                            errorsAreFatal=opts.firsterror)
70 # turn DeprecationWarnings into errors
71 warnings.filterwarnings("error")
73 try:
74     processedFile = preprocessKickstart(f)
75     ksparser.readKickstart(processedFile)
76 except DeprecationWarning, msg:
77     print _("File uses a deprecated option or command.\n%s") % msg
78     cleanup(destdir, processedFile)
79 except (KickstartParseError, KickstartValueError), msg:
80     print msg
81     cleanup(destdir, processedFile)
82 except KickstartError:
83     print _("General kickstart error in input file")
84     cleanup(destdir, processedFile)
85 except Exception, e:
86     print _("General error in input file:  %s") % e
87     cleanup(destdir)
89 cleanup(destdir, processedFile, exitval=0)