New version.
[pykickstart.git] / tools / ksvalidator
blob46062869396eb3cf945a0838c384d40a04d2fc6b
1 #!/usr/bin/python
3 # Chris Lumens <clumens@redhat.com>
5 # Copyright 2005-2014 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 argparse
23 import os
24 import sys
25 import warnings
26 import tempfile
27 import urlgrabber
28 import shutil
29 from pykickstart.errors import KickstartError, KickstartParseError, KickstartValueError, KickstartVersionError
30 from pykickstart.parser import KickstartParser, preprocessKickstart
31 from pykickstart.version import DEVEL, makeVersion, versionMap
33 import gettext
34 gettext.install("pykickstart", unicode=True)
35 _ = lambda x: gettext.ldgettext("pykickstart", x)
37 def cleanup(dest, fn=None, exitval=1):
38 shutil.rmtree(dest)
40 # Don't care if this file doesn't exist.
41 if fn is not None:
42 try:
43 os.remove(fn)
44 except Exception:
45 pass
47 sys.exit(exitval)
49 op = argparse.ArgumentParser(usage="%(prog)s [options] ksfile")
50 op.add_argument("ksfile",
51 help=_("filename or URL to read from"))
52 op.add_argument("-e", "--firsterror", dest="firsterror", action="store_true",
53 default=False, help=_("halt after the first error or warning"))
54 op.add_argument("-i", "--followincludes", dest="followincludes",
55 action="store_true", default=False,
56 help=_("parse include files when %%include is seen"))
57 op.add_argument("-l", "--listversions", dest="listversions", action="store_true",
58 default=False,
59 help=_("list the available versions of kickstart syntax"))
60 op.add_argument("-v", "--version", dest="version", default=DEVEL,
61 help=_("version of kickstart syntax to validate against"))
63 opts = op.parse_args(sys.argv[1:])
65 if opts.listversions:
66 for key in sorted(versionMap.keys()):
67 print(key)
69 sys.exit(1)
71 destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
72 try:
73 f = urlgrabber.urlgrab(opts.ksfile, filename="%s/ks.cfg" % destdir)
74 except urlgrabber.grabber.URLGrabError, e:
75 print(_("Error reading %s:\n%s") % (opts.ksfile, e))
76 cleanup(destdir)
78 try:
79 handler = makeVersion(opts.version)
80 except KickstartVersionError:
81 print(_("The version %s is not supported by pykickstart") % opts.version)
82 cleanup(destdir)
84 ksparser = KickstartParser(handler, followIncludes=opts.followincludes,
85 errorsAreFatal=opts.firsterror)
87 # turn DeprecationWarnings into errors
88 warnings.filterwarnings("error")
90 processedFile = None
92 try:
93 processedFile = preprocessKickstart(f)
94 ksparser.readKickstart(processedFile)
95 cleanup(destdir, processedFile, exitval=0)
96 except DeprecationWarning as msg:
97 print(_("File uses a deprecated option or command.\n%s") % msg)
98 cleanup(destdir, processedFile)
99 except (KickstartParseError, KickstartValueError) as msg:
100 print(msg)
101 cleanup(destdir, processedFile)
102 except KickstartError:
103 print(_("General kickstart error in input file"))
104 cleanup(destdir, processedFile)
105 except Exception as e:
106 print(_("General error in input file: %s") % e)
107 cleanup(destdir, processedFile)