Add tests for incorrect command usage detection
[pykickstart.git] / tools / ksflatten
blob284a2976406fa154b238856c1a30c2cfe0e2e989
1 #!/usr/bin/python
3 # Simple script to take a kickstart config, read it in, parse any %includes,
4 # etc to write out a flattened config that is stand-alone
6 # Copyright 2007, Red Hat, Inc.
7 # Jeremy Katz <katzj@redhat.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Library General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 import os, sys
23 import optparse
24 import pykickstart
25 import pykickstart.parser
26 from pykickstart.version import *
28 def parse_args():
29 parser = optparse.OptionParser()
31 parser.add_option("-c", "--config", type="string", dest="kscfg",
32 help="Path to kickstart config file")
33 parser.add_option("-v", "--version", dest="version", default=DEVEL,
34 help="Kickstart version to use for interpreting config")
35 parser.add_option("-o", "--output", type="string", dest="output",
36 help="Write flattened config to OUTPUT")
38 (options, args) = parser.parse_args()
39 return options, args
41 def main():
42 (opts, args) = parse_args()
43 if not opts.kscfg and len(args) >= 1:
44 opts.kscfg = args[0]
45 elif not opts.kscfg:
46 print >> sys.stderr, "Need to specify a config to flatten"
47 sys.exit(1)
49 ksversion = makeVersion(opts.version)
50 ksparser = pykickstart.parser.KickstartParser(ksversion)
51 try:
52 ksparser.readKickstart(opts.kscfg)
53 except IOError, msg:
54 print >> sys.stderr, "Failed to read kickstart file '%s' : %s" % (opts.kscfg, msg)
55 sys.exit(1)
56 except pykickstart.errors.KickstartError, e:
57 print >> sys.stderr, "Failed to parse kickstart file '%s' : %s" % (opts.kscfg, e)
59 if opts.output:
60 try:
61 f = open(opts.output, 'w')
62 except IOError, msg:
63 print >> sys.stderr, "Failed to open output file '%s' : %s" %(opts.output, msg)
64 else:
65 f = sys.stdout
67 f.write("%s" %(ksparser.handler,))
68 f.close()
70 if __name__ == "__main__":
71 main()