def do_ksappend_substitution(runtime_folder, test_file): """Do the ksappend substitution. This is done by changing PWD to the runtime folder, (required by how Pykickstart currently does the substitution) running Pykickstart to do the substitution and then reverting PWD back. Individual substitution runs are logged as are failed substitution attempts. The result of the substitution is printed to stdout. :param str runtime_folder: path to the ksappend runtime folder :param str test_file: path to the kickstart .ks.in file """ abs_test_file = os.path.abspath(test_file) # change working directory to the runtime folder or else # Pykickstart will not find the fragments os.chdir(runtime_folder) # do the substitution logging.info("running ksappend substitution on: %s", test_file) # the result should be a temp file result_path = kickstart_parser.preprocessKickstart(abs_test_file) if result_path: # print result to stdout and clean up the temp file with open(result_path) as f: print(f.read()) os.remove(result_path) else: logging.error("ksappend substitution failed for: %s", test_file) exit(1)
def do_ksappend_substitution(runtime_folder): """Do the ksappend substitution. This is done by changing PWD to the runtime folder, (required by how Pykickstart currently does the substitution) running Pykickstart to do the substitution and then reverting PWD back. Individual substitution runs are printed to stdout as are failed substitution attempts. We keep all the input files and rename them to <name>.ks.input and rename all files that failed the substitution to <name>.ks.input.FAILED. :param str runtime_folder: path to the ksappend runtime folder :returns: list of successfully processed kickstart test files :rtype: list of str """ # save current PWD pwd = os.path.abspath(".") # change working directory to the runtime folder or else # Pykickstart will not find the fragments os.chdir(runtime_folder) # do the substitution resolved_ks_files = [] for test_file in glob.glob("*.ks"): test_base_name = os.path.split(test_file)[1].rsplit(".ks")[0] print("running ksappend substitution on: {}".format(test_file)) # the result should be a temp file result_path = kickstart_parser.preprocessKickstart(test_file) if result_path: # rename the input file and keep it for reference input_file = "{}.ks.input".format(test_base_name) shutil.move(test_file, input_file) # move the result file in place of the original ks file shutil.move(result_path, test_file) # register the successfully resolved file resolved_ks_files.append(test_file) else: print("ksappend substitution failed for: {}".format(test_file)) # rename the input file to mark it as failed failed_input_file = "{}.ks.input.FAILED".format(test_base_name) shutil.move(test_file, failed_input_file) # restore working directory back os.chdir(pwd) # return a list of all successfully processed file names & paths in runtime folder # in the [(<file name>, <path to file in runtime folder>),] format return resolved_ks_files
def main(argv): op = argparse.ArgumentParser(usage="%(prog)s [options] ksfile", add_help=False) op.add_argument("ksfile", nargs="?", help=_("filename or URL to read from")) op.add_argument("-e", "--firsterror", dest="firsterror", action="store_true", default=False, help=_("halt after the first error or warning")) op.add_argument("-i", "--followincludes", dest="followincludes", action="store_true", default=False, help=_("parse include files when %%include is seen")) op.add_argument("-l", "--listversions", dest="listversions", action="store_true", default=False, help=_("list the available versions of kickstart syntax")) op.add_argument("-v", "--version", dest="version", default=DEVEL, help=_("version of kickstart syntax to validate against")) op.add_argument("-h", "--help", dest="help", action="store_true", default=False, help=_("show this help message and exit")) opts = op.parse_args(argv) # parse --help manually b/c we don't want to sys.exit before the # tests have finished if opts.help: return (0, op.format_help().split("\n")) if opts.listversions: versions = [] for key in sorted(versionMap.keys()): versions.append(key) return (0, versions) if not opts.ksfile: return (1, op.format_usage().split("\n")) destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp") try: f = load_to_file(opts.ksfile, "%s/ks.cfg" % destdir) except KickstartError as e: return (cleanup(destdir), [_("Error reading %(filename)s:\n%(version)s") % {"filename": opts.ksfile, "version": e}]) try: handler = makeVersion(opts.version) except KickstartVersionError: return (cleanup(destdir), [_("The version %s is not supported by pykickstart") % opts.version]) ksparser = KickstartParser(handler, followIncludes=opts.followincludes, errorsAreFatal=opts.firsterror) # turn DeprecationWarnings into errors warnings.filterwarnings("error") processedFile = None try: processedFile = preprocessKickstart(f) ksparser.readKickstart(processedFile) return (cleanup(destdir, processedFile, exitval=ksparser.errorsCount), []) except DeprecationWarning as err: return (cleanup(destdir, processedFile), [_("File uses a deprecated option or command.\n%s") % err]) except KickstartParseError as err: return (cleanup(destdir, processedFile), [str(err)]) except KickstartError: return (cleanup(destdir, processedFile), [_("General kickstart error in input file")]) except Exception as e: return (cleanup(destdir, processedFile), [_("General error in input file: %s") % e])
def main(): ## ## OPTION PROCESSING ## op = argparse.ArgumentParser() op.add_argument("-i", "--input", dest="input", help=_("a basis file to use for seeding the kickstart data (optional)")) op.add_argument("-o", "--output", dest="output", help=_("the location to write the finished kickstart file, or stdout if not given")) op.add_argument("-v", "--version", dest="version", default=DEVEL, help=_("version of kickstart syntax to validate against")) opts = op.parse_args(sys.argv[1:]) ## ## SETTING UP PYKICKSTART ## try: kshandler = makeVersion(opts.version) except KickstartVersionError: print(_("The version %s is not supported by pykickstart") % opts.version) sys.exit(1) ksparser = KickstartParser(kshandler, followIncludes=True, errorsAreFatal=False) if opts.input: try: processedFile = preprocessKickstart(opts.input) ksparser.readKickstart(processedFile) os.remove(processedFile) except KickstartError as e: # Errors should just dump you to the prompt anyway. print(_("Warning: The following error occurred when processing the input file:\n%s\n") % e) internalCommands = {".clear": ClearCommand(), ".show": ShowCommand(), ".quit": QuitCommand()} ## ## SETTING UP READLINE ## readline.parse_and_bind("tab: complete") readline.set_completer(KickstartCompleter(kshandler, internalCommands).complete) # Since everything in kickstart looks like a command line arg, we need to # remove '-' from the delimiter string. delims = readline.get_completer_delims() readline.set_completer_delims(delims.replace('-', '')) ## ## REPL ## print("Press ^D to exit.") while True: try: line = six.moves.input("ks> ") # pylint: disable=no-member except EOFError: # ^D was hit, time to quit. break except KeyboardInterrupt: # ^C was hit, time to quit. Don't be like other programs. break # All internal commands start with a ., so if that's the beginning of the # line, we need to dispatch ourselves. if line.startswith("."): words = line.split() if words[0] in internalCommands: try: internalCommands[words[0]].execute(ksparser) except EOFError: # ".quit" was typed, time to quit. break else: print(_("Internal command %s not recognized.") % words[0]) continue # Now process the line of input as if it were a kickstart file - just an # extremely short one. try: ksparser.readKickstartFromString(line) except KickstartError as e: print(e) # And finally, print the output kickstart file. if opts.output: with open(opts.output, "w") as fd: fd.write(str(ksparser.handler)) else: print("\n" + str(ksparser.handler))
def main(argv): op = argparse.ArgumentParser(usage="%(prog)s [options] ksfile", add_help=False) op.add_argument("ksfile", nargs="?", help=_("filename or URL to read from")) op.add_argument("-e", "--firsterror", dest="firsterror", action="store_true", default=False, help=_("halt after the first error or warning")) op.add_argument("-i", "--followincludes", dest="followincludes", action="store_true", default=False, help=_("parse include files when %%include is seen")) op.add_argument("-l", "--listversions", dest="listversions", action="store_true", default=False, help=_("list the available versions of kickstart syntax")) op.add_argument("-v", "--version", dest="version", default=DEVEL, help=_("version of kickstart syntax to validate against")) op.add_argument("-h", "--help", dest="help", action="store_true", default=False, help=_("show this help message and exit")) opts = op.parse_args(argv) # parse --help manually b/c we don't want to sys.exit before the # tests have finished if opts.help: return (0, op.format_help().split("\n")) if opts.listversions: versions = [] for key in sorted(versionMap.keys()): versions.append(key) return (0, versions) if not opts.ksfile: return (1, op.format_usage().split("\n")) destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp") try: f = load_to_file(opts.ksfile, "%s/ks.cfg" % destdir) except KickstartError as e: return (cleanup(destdir), [_("Error reading %(filename)s:\n%(version)s") % {"filename": opts.ksfile, "version": e}]) try: handler = makeVersion(opts.version) except KickstartVersionError: return (cleanup(destdir), [_("The version %s is not supported by pykickstart") % opts.version]) ksparser = KickstartParser(handler, followIncludes=opts.followincludes, errorsAreFatal=opts.firsterror) # turn kickstart parse warnings into errors warnings.filterwarnings(action="error", category=KickstartParseWarning) processedFile = None try: processedFile = preprocessKickstart(f) ksparser.readKickstart(processedFile) return (cleanup(destdir, processedFile, exitval=ksparser.errorsCount), []) except KickstartDeprecationWarning as err: return (cleanup(destdir, processedFile), [_("File uses a deprecated option or command.\n%s") % err]) except KickstartParseError as err: return (cleanup(destdir, processedFile), [str(err)]) except KickstartError: return (cleanup(destdir, processedFile), [_("General kickstart error in input file")]) except Exception as e: return (cleanup(destdir, processedFile), [_("General error in input file: %s") % e])
def runTest(self): self._processedPath = preprocessKickstart(self._path) with open(self._processedPath) as f: self.assertEqual(f.read(), self.ks + self.ksappend)
## ## SETTING UP PYKICKSTART ## try: kshandler = makeVersion(opts.version) except KickstartVersionError: print(_("The version %s is not supported by pykickstart") % opts.version) sys.exit(1) ksparser = KickstartParser(kshandler, followIncludes=True, errorsAreFatal=False) if opts.input: try: processedFile = preprocessKickstart(opts.input) ksparser.readKickstart(processedFile) os.remove(processedFile) except KickstartError as e: # Errors should just dump you to the prompt anyway. print(_("Warning: The following error occurred when processing the input file:\n%s\n") % e) internalCommands = {".clear": ClearCommand(), ".show": ShowCommand(), ".quit": QuitCommand()} ## ## SETTING UP READLINE ## readline.parse_and_bind("tab: complete")
def main(argv=None): ## ## OPTION PROCESSING ## op = argparse.ArgumentParser() op.add_argument("-i", "--input", dest="input", help=_("a basis file to use for seeding the kickstart data (optional)")) op.add_argument("-o", "--output", dest="output", help=_("the location to write the finished kickstart file, or stdout if not given")) op.add_argument("-v", "--version", dest="version", default=DEVEL, help=_("version of kickstart syntax to validate against")) opts = op.parse_args(argv) ## ## SETTING UP PYKICKSTART ## try: kshandler = makeVersion(opts.version) except KickstartVersionError: print(_("The version %s is not supported by pykickstart") % opts.version) return 1 ksparser = KickstartParser(kshandler, followIncludes=True, errorsAreFatal=False) if opts.input: try: processedFile = preprocessKickstart(opts.input) ksparser.readKickstart(processedFile) os.remove(processedFile) except KickstartError as e: # Errors should just dump you to the prompt anyway. print(_("Warning: The following error occurred when processing the input file:\n%s\n") % e) internalCommands = {".clear": ClearCommand(), ".show": ShowCommand(), ".quit": QuitCommand()} ## ## SETTING UP READLINE ## readline.parse_and_bind("tab: complete") readline.set_completer(KickstartCompleter(kshandler, internalCommands).complete) # Since everything in kickstart looks like a command line arg, we need to # remove '-' from the delimiter string. delims = readline.get_completer_delims() readline.set_completer_delims(delims.replace('-', '')) ## ## REPL ## print("Press ^D to exit.") while True: try: line = six.moves.input("ks> ") # pylint: disable=no-member except EOFError: # ^D was hit, time to quit. break except KeyboardInterrupt: # ^C was hit, time to quit. Don't be like other programs. break # All internal commands start with a ., so if that's the beginning of the # line, we need to dispatch ourselves. if line.startswith("."): words = line.split() if words[0] in internalCommands: try: internalCommands[words[0]].execute(ksparser) except EOFError: # ".quit" was typed, time to quit. break else: print(_("Internal command %s not recognized.") % words[0]) continue # Now process the line of input as if it were a kickstart file - just an # extremely short one. try: ksparser.readKickstartFromString(line) except KickstartError as e: print(e) # And finally, print the output kickstart file. if opts.output: with open(opts.output, "w") as fd: fd.write(str(ksparser.handler)) else: print("\n" + str(ksparser.handler)) return 0
handler = makeVersion(opts.version) except KickstartVersionError: print(_("The version %s is not supported by pykickstart") % opts.version) cleanup(destdir) ksparser = KickstartParser(handler, followIncludes=opts.followincludes, errorsAreFatal=opts.firsterror) # turn DeprecationWarnings into errors warnings.filterwarnings("error") processedFile = None try: processedFile = preprocessKickstart(f) ksparser.readKickstart(processedFile) cleanup(destdir, processedFile, exitval=0) except DeprecationWarning as msg: print(_("File uses a deprecated option or command.\n%s") % msg) cleanup(destdir, processedFile) except KickstartParseError as msg: print(msg) cleanup(destdir, processedFile) except KickstartError: print(_("General kickstart error in input file")) cleanup(destdir, processedFile) except Exception as e: print(_("General error in input file: %s") % e) cleanup(destdir, processedFile)
try: handler = makeVersion(opts.version) except KickstartVersionError: print(_("The version %s is not supported by pykickstart") % opts.version) cleanup(destdir) ksparser = KickstartParser(handler, followIncludes=opts.followincludes, errorsAreFatal=opts.firsterror) # turn DeprecationWarnings into errors warnings.filterwarnings("error") processedFile = None try: processedFile = preprocessKickstart(f) ksparser.readKickstart(processedFile) cleanup(destdir, processedFile, exitval=0) except DeprecationWarning as msg: print(_("File uses a deprecated option or command.\n%s") % msg) cleanup(destdir, processedFile) except KickstartParseError as msg: print(msg) cleanup(destdir, processedFile) except KickstartError: print(_("General kickstart error in input file")) cleanup(destdir, processedFile) except Exception as e: print(_("General error in input file: %s") % e) cleanup(destdir, processedFile)
## SETTING UP PYKICKSTART ## try: kshandler = makeVersion(opts.version) except KickstartVersionError: print(_("The version %s is not supported by pykickstart") % opts.version) sys.exit(1) ksparser = KickstartParser(kshandler, followIncludes=True, errorsAreFatal=False) if opts.input: try: processedFile = preprocessKickstart(opts.input) ksparser.readKickstart(processedFile) os.remove(processedFile) except KickstartError as e: # Errors should just dump you to the prompt anyway. print( _("Warning: The following error occurred when processing the input file:\n%s\n" ) % e) internalCommands = { ".clear": ClearCommand(), ".show": ShowCommand(), ".quit": QuitCommand() } ##
def main(argv): op = argparse.ArgumentParser(usage="%(prog)s [options] ksfile [ksfile...]", add_help=False) op.add_argument("ksfile", nargs="*", help=_("filename or URL to read from")) op.add_argument("-e", "--firsterror", dest="firsterror", action="store_true", default=False, help=_("halt after the first error or warning")) op.add_argument("-i", "--followincludes", dest="followincludes", action="store_true", default=False, help=_("parse include files when %%include is seen")) op.add_argument("-l", "--listversions", dest="listversions", action="store_true", default=False, help=_("list the available versions of kickstart syntax")) op.add_argument("-v", "--version", dest="version", default=DEVEL, help=_("version of kickstart syntax to validate against")) op.add_argument("-h", "--help", dest="help", action="store_true", default=False, help=_("show this help message and exit")) opts = op.parse_args(argv) # parse --help manually b/c we don't want to sys.exit before the # tests have finished if opts.help: return (0, op.format_help().split("\n")) if opts.listversions: versions = [] for key in sorted(versionMap.keys()): versions.append(key) return (0, versions) if not opts.ksfile: return (1, op.format_usage().split("\n")) rc = 0 retmsg = [] # unpack any globs ksfiles = [] for inksfile in opts.ksfile: if is_url(inksfile): ksfiles.append(inksfile) else: ksfiles.extend(glob.glob(inksfile)) # check if there are any files to check if not ksfiles: return (1, ["No files match the patterns."]) # iterate over files to check them with tempfile.TemporaryDirectory(prefix="ksvalidator-tmp-") as destdir: for ksfile in ksfiles: print( _("\nChecking kickstart file %(filename)s\n" % {"filename": ksfile})) try: f = load_to_file(ksfile, os.path.join(destdir, "ks.cfg")) except KickstartError as e: rc += 1 retmsg.append( _("Error reading %(filename)s:\n%(version)s") % { "filename": ksfile, "version": e }) try: handler = makeVersion(opts.version) except KickstartVersionError: # return immediately because bad version is fatal for all files return (1, [ _("The version %s is not supported by pykickstart") % opts.version ]) # turn kickstart parse warnings into errors warnings.filterwarnings(action="error", category=KickstartParseWarning) ksparser = KickstartParser(handler, followIncludes=opts.followincludes, errorsAreFatal=opts.firsterror) try: processedFile = preprocessKickstart(f) if processedFile is None: raise RuntimeError("Empty file") ksparser.readKickstart(processedFile) rc += ksparser.errorsCount except KickstartDeprecationWarning as err: rc += 1 retmsg.append( _("File uses a deprecated option or command.\n%s") % err) except KickstartParseError as err: rc += 1 retmsg.append(str(err)) except KickstartError: rc += 1 retmsg.append(_("General kickstart error in input file")) except Exception as e: # pylint: disable=broad-except rc += 1 retmsg.append(_("General error in input file: %s") % e) return rc, retmsg