New version.
[pykickstart.git] / tools / ksflatten
blob46c4d2c1c504cd65d0cbcac50151f3d9b45556f7
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-2014, 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 from __future__ import print_function
24 import sys
25 import argparse
26 import pykickstart
27 import pykickstart.parser
28 from pykickstart.version import DEVEL, makeVersion
30 import gettext
31 gettext.textdomain("pykickstart")
32 _ = lambda x: gettext.ldgettext("pykickstart", x)
34 def parse_args():
35 parser = argparse.ArgumentParser()
36 parser.add_argument("-c", "--config", dest="kscfg", required=True,
37 help=_("Path to kickstart config file"))
38 parser.add_argument("-v", "--version", dest="version", default=DEVEL,
39 help=_("Kickstart version to use for interpreting config"))
40 parser.add_argument("-o", "--output", dest="output",
41 help=_("Write flattened config to OUTPUT"))
43 return parser.parse_args()
45 def main():
46 opts = parse_args()
47 if not opts.kscfg:
48 print(_("Need to specify a config to flatten"), file=sys.stderr)
49 sys.exit(1)
51 ksversion = makeVersion(opts.version)
52 ksparser = pykickstart.parser.KickstartParser(ksversion)
53 try:
54 ksparser.readKickstart(opts.kscfg)
55 except IOError as msg:
56 print(_("Failed to read kickstart file '%s' : %s") % (opts.kscfg, msg), file=sys.stderr)
57 sys.exit(1)
58 except pykickstart.errors.KickstartError as e:
59 print(_("Failed to parse kickstart file '%s' : %s") % (opts.kscfg, e), file=sys.stderr)
61 if opts.output:
62 try:
63 f = open(opts.output, 'w')
64 except IOError as msg:
65 print(_("Failed to open output file '%s' : %s") % (opts.output, msg), file=sys.stderr)
66 else:
67 f = sys.stdout
69 f.write("%s" % ksparser.handler)
70 f.close()
72 if __name__ == "__main__":
73 main()