Exemplo n.º 1
0
    def runTest(self, _print):
        retval = ksverdiff.main(['-l'])
        self.assertEqual(retval, 0)

        _print_calls = []
        for key in sorted(versionMap.keys()):
            _print_calls.append(call(key))
        _print.assert_has_calls(_print_calls)
Exemplo n.º 2
0
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])
Exemplo n.º 3
0
    return set(map(lambda o: o.get_opt_string(), lst))

def printList(lst):
    print(' '.join(lst))

op = argparse.ArgumentParser()
op.add_argument("-f", "--from", dest="f")
op.add_argument("-t", "--to", dest="t")
op.add_argument("-l", "--listversions", dest="listversions", action="store_true",
                default=False,
                help=_("list the available versions of kickstart syntax"))

opts = op.parse_args(sys.argv[1:])

if opts.listversions:
    for key in sorted(versionMap.keys()):
        print(key)

    sys.exit(1)

if not opts.f or not opts.t:
    print(_("You must specify two syntax versions."))
    sys.exit(1)

try:
    fromHandler = makeVersion(opts.f)
    toHandler = makeVersion(opts.t)
except KickstartVersionError as exn:
    print(_("The version %s is not supported by pykickstart") % exn)
    sys.exit(1)
Exemplo n.º 4
0
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])
Exemplo n.º 5
0
def main(argv=None):
    op = argparse.ArgumentParser()
    op.add_argument("-f", "--from", dest="f")
    op.add_argument("-t", "--to", dest="t")
    op.add_argument("-l", "--listversions", dest="listversions", action="store_true",
                    default=False,
                    help=_("list the available versions of kickstart syntax"))

    opts = op.parse_args(argv)

    if opts.listversions:
        for key in sorted(versionMap.keys()):
            print(key)
        return 0

    if not opts.f or not opts.t:
        print(_("You must specify two syntax versions."))
        return 1

    try:
        fromHandler = makeVersion(opts.f)
        toHandler = makeVersion(opts.t)
    except KickstartVersionError as exn:
        print(_("The version %s is not supported by pykickstart") % exn)
        return 1

    fromCmdSet = getCommandSet(fromHandler)
    toCmdSet = getCommandSet(toHandler)
    bothSet = fromCmdSet & toCmdSet

    print(_("The following commands were removed in %s:") % opts.t)
    printList(sorted(fromCmdSet - toCmdSet))

    print(_("The following commands were deprecated in %s:") % opts.t)
    printList(sorted([cmd for cmd in bothSet if isinstance(toHandler.commands[cmd], DeprecatedCommand)]))

    print(_("The following commands were added in %s:") % opts.t)
    printList(sorted(toCmdSet - fromCmdSet))

    for cmd in sorted(bothSet):
        newOptList = []
        deprecatedOptList = []
        removedOptList = []

        fromCmd = fromHandler.commands[cmd]
        toCmd = toHandler.commands[cmd]

        if not hasattr(fromCmd, "op") or not hasattr(toCmd, "op"):
            continue

        fromOpt = fromCmd.op._actions
        toOpt = toCmd.op._actions

        newOptList = getOptSet(toOpt) - getOptSet(fromOpt)
        removedOptList = getOptSet(fromOpt) - getOptSet(toOpt)
        deprecatedOptList = getOptSet([cmd for cmd in toOpt if cmd.deprecated])

        if len(newOptList) > 0:
            print(_("The following options were added to the %(command_name)s command in %(version)s:") % {"command_name": cmd, "version": opts.t})
            printList(sorted(newOptList))

        if len(deprecatedOptList) > 0:
            print(_("The following options were deprecated from the %(command_name)s command in %(version)s:") % {"command_name": cmd, "version": opts.t})
            printList(sorted(deprecatedOptList))

        if len(removedOptList) > 0:
            print(_("The following options were removed from the %(command_name)s command in %(version)s:") % {"command_name": cmd, "version": opts.t})
            printList(sorted(removedOptList))

    return 0
Exemplo n.º 6
0
 def setUp(self):
     self.versions_list = sorted(versionMap.keys())
Exemplo n.º 7
0

op = argparse.ArgumentParser()
op.add_argument("-f", "--from", dest="f")
op.add_argument("-t", "--to", dest="t")
op.add_argument("-l",
                "--listversions",
                dest="listversions",
                action="store_true",
                default=False,
                help=_("list the available versions of kickstart syntax"))

opts = op.parse_args(sys.argv[1:])

if opts.listversions:
    for key in sorted(versionMap.keys()):
        print(key)

    sys.exit(1)

if not opts.f or not opts.t:
    print(_("You must specify two syntax versions."))
    sys.exit(1)

try:
    fromHandler = makeVersion(opts.f)
except KickstartVersionError:
    print(_("The version %s is not supported by pykickstart") % opts.f)
    sys.exit(1)

try:
Exemplo n.º 8
0
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