~registry/pykickstart/trunk

« back to all changes in this revision

Viewing changes to tools/ksvalidator

  • Committer: Chris Lumens
  • Date: 2014-10-22 14:19:44 UTC
  • Revision ID: git-v1:982498c0647c4bc3b4460209d4a76698c0eb6f8c
Add a note that the repo has moved.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
#
3
 
# Chris Lumens <clumens@redhat.com>
4
 
#
5
 
# Copyright 2005-2014 Red Hat, Inc.
6
 
#
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.
13
 
#
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. 
20
 
#
21
 
 
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
32
 
 
33
 
import gettext
34
 
gettext.install("pykickstart", unicode=True)
35
 
_ = lambda x: gettext.ldgettext("pykickstart", x)
36
 
 
37
 
def cleanup(dest, fn=None, exitval=1):
38
 
    shutil.rmtree(dest)
39
 
 
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
46
 
 
47
 
    sys.exit(exitval)
48
 
 
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"))
62
 
 
63
 
opts = op.parse_args(sys.argv[1:])
64
 
 
65
 
if opts.listversions:
66
 
    for key in sorted(versionMap.keys()):
67
 
        print(key)
68
 
 
69
 
    sys.exit(1)
70
 
 
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)
77
 
 
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)
83
 
 
84
 
ksparser = KickstartParser(handler, followIncludes=opts.followincludes,
85
 
                           errorsAreFatal=opts.firsterror)
86
 
 
87
 
# turn DeprecationWarnings into errors
88
 
warnings.filterwarnings("error")
89
 
 
90
 
processedFile = None
91
 
 
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)