def stringToVersion(s):
    """Convert string into one of the provided version constants.  Raises
       KickstartVersionError if string does not match anything.
    """
    # First try these short forms.
    try:
        return versionMap[s.upper()]
    except KeyError:
        pass

    # Now try the Fedora versions.
    m = re.match("^fedora.* (\d+)$", s, re.I)

    if m and m.group(1):
        if versionMap.has_key("FC" + m.group(1)):
            return versionMap["FC" + m.group(1)]
        elif versionMap.has_key("F" + m.group(1)):
            return versionMap["F" + m.group(1)]
        else:
            raise KickstartVersionError(
                _("Unsupported version specified: %s") % s)

    # Now try the RHEL versions.
    m = re.match("^red hat enterprise linux.* (\d+)([\.\d]*)$", s, re.I)

    if m and m.group(1):
        if versionMap.has_key("RHEL" + m.group(1)):
            return versionMap["RHEL" + m.group(1)]
        else:
            raise KickstartVersionError(
                _("Unsupported version specified: %s") % s)

    # If nothing else worked, we're out of options.
    raise KickstartVersionError(_("Unsupported version specified: %s") % s)
def returnClassForVersion(version=DEVEL):
    """Return the class of the syntax handler for version.  version can be
       either a string or the matching constant.  Raises KickstartValueError
       if version does not match anything.
    """
    try:
        version = int(version)
        module = "%s" % versionToString(version, skipDevel=True)
    except ValueError:
        module = "%s" % version
        version = stringToVersion(version)

    module = module.lower()

    try:
        import pykickstart.handlers
        sys.path.extend(pykickstart.handlers.__path__)
        found = imputil.imp.find_module(module)
        loaded = imputil.imp.load_module(module, found[0], found[1], found[2])

        for (k, v) in loaded.__dict__.iteritems():
            if k.lower().endswith("%shandler" % module):
                return v
    except:
        raise KickstartVersionError(
            _("Unsupported version specified: %s") % version)
예제 #3
0
def returnClassForVersion(
        version=DEVEL):  # type: (Union[int, str]) -> Callable[[], BaseHandler]
    """Return the class of the syntax handler for version.  version can be
       either a string or the matching constant.  Raises KickstartVersionError
       if version does not match anything.
    """
    try:
        version = int(version)
        module = "%s" % versionToString(cast(int, version), skipDevel=True)
    except ValueError:
        module = "%s" % version
        version = stringToVersion(cast(str, version))

    module = module.lower()

    try:
        import pykickstart.handlers
        # mypy does not understand module.__path__, skip
        sys.path.extend(pykickstart.handlers.__path__)  # type: ignore
        loaded = importlib.import_module(module)

        for (k, v) in list(loaded.__dict__.items()):
            if k.lower().endswith("%shandler" % module):
                return v
    except:
        raise KickstartVersionError(
            _("Unsupported version specified: %s") % version)
예제 #4
0
def returnClassForVersion(version=DEVEL):
    """Return the class of the syntax handler for version.  version can be
       either a string or the matching constant.  Raises KickstartVersionError
       if version does not match anything.
    """
    try:
        version = int(version)
        module = "%s" % versionToString(version, skipDevel=True)
    except ValueError:
        module = "%s" % version
        version = stringToVersion(version)

    module = module.lower()

    try:
        _path = os.path.join(os.path.dirname(__file__), "handlers/")
        sys.path.extend([_path])
        loaded = importlib.import_module(module)

        for (k, v) in list(loaded.__dict__.items()):
            if k.lower().endswith("%shandler" % module):
                return v
    except:
        raise KickstartVersionError(
            _("Unsupported version specified: %s") % version)
예제 #5
0
def versionToString(version, skipDevel=False):
    """Convert version into a string representation of the version number.
       This is the reverse operation of stringToVersion.  Raises
       KickstartVersionError if version does not match anything.
    """
    if not skipDevel and version == versionMap["DEVEL"]:
        return "DEVEL"

    for (key, val) in list(versionMap.items()):
        if key == "DEVEL":
            continue
        elif val == version:
            return key

    raise KickstartVersionError(_("Unsupported version specified: %s") % version)
예제 #6
0
    def runTest(self):
        self.assertEqual(str(KickstartError("OH NO!")), "OH NO!")
        self.assertEqual(str(KickstartParseError("OH NO!")), "OH NO!")
        self.assertEqual(str(KickstartVersionError("OH NO!")), "OH NO!")

        err = KickstartError()
        self.assertEqual(err.message, "")
        self.assertEqual(err.lineno, None)
        self.assertEqual(err.value, "")
        self.assertEqual(str(err), "")

        err = KickstartError("OH NO!")
        self.assertEqual(err.message, "OH NO!")
        self.assertEqual(err.lineno, None)
        self.assertEqual(err.value, "OH NO!")
        self.assertEqual(str(err), "OH NO!")

        err = KickstartError(lineno=0)
        self.assertEqual(err.message, "")
        self.assertEqual(err.lineno, 0)
        self.assertEqual(err.value, _format_error_message(msg="", lineno=0))
        self.assertEqual(str(err), _format_error_message(msg="", lineno=0))

        err = KickstartError(lineno=1)
        self.assertEqual(err.message, "")
        self.assertEqual(err.lineno, 1)
        self.assertEqual(err.value, _format_error_message(msg="", lineno=1))
        self.assertEqual(str(err), _format_error_message(msg="", lineno=1))

        err = KickstartError("OH NO!", lineno=0)
        self.assertEqual(err.message, "OH NO!")
        self.assertEqual(err.lineno, 0)
        self.assertEqual(err.value,
                         _format_error_message(msg="OH NO!", lineno=0))
        self.assertEqual(str(err), _format_error_message(msg="OH NO!",
                                                         lineno=0))

        err = KickstartError("OH NO!", lineno=1)
        self.assertEqual(err.message, "OH NO!")
        self.assertEqual(err.lineno, 1)
        self.assertEqual(err.value,
                         _format_error_message(msg="OH NO!", lineno=1))
        self.assertEqual(str(err), _format_error_message(msg="OH NO!",
                                                         lineno=1))

        err = KickstartError("OH NO!", lineno=1, formatting=True)
        self.assertEqual(err.message, "OH NO!")
        self.assertEqual(err.lineno, 1)
        self.assertEqual(err.value,
                         _format_error_message(msg="OH NO!", lineno=1))
        self.assertEqual(str(err), _format_error_message(msg="OH NO!",
                                                         lineno=1))

        err = KickstartError("OH NO!", lineno=1, formatting=False)
        self.assertEqual(err.message, "OH NO!")
        self.assertEqual(err.lineno, 1)
        self.assertEqual(err.value, "OH NO!")
        self.assertEqual(str(err), "OH NO!")

        with self.assertWarns(DeprecationWarning):
            err = KickstartError(formatErrorMsg(lineno=0))
            self.assertEqual(err.message, "")
            self.assertEqual(err.lineno, 0)
            self.assertEqual(err.value, _format_error_message(msg="",
                                                              lineno=0))
            self.assertEqual(str(err), _format_error_message(msg="", lineno=0))

        with self.assertWarns(DeprecationWarning):
            err = KickstartError(formatErrorMsg(lineno=1))
            self.assertEqual(err.message, "")
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.value, _format_error_message(msg="",
                                                              lineno=1))
            self.assertEqual(str(err), _format_error_message(msg="", lineno=1))

        with self.assertWarns(DeprecationWarning):
            err = KickstartError(formatErrorMsg(msg="OH NO!", lineno=0))
            self.assertEqual(err.message, "OH NO!")
            self.assertEqual(err.lineno, 0)
            self.assertEqual(err.value,
                             _format_error_message(msg="OH NO!", lineno=0))
            self.assertEqual(str(err),
                             _format_error_message(msg="OH NO!", lineno=0))

        with self.assertWarns(DeprecationWarning):
            err = KickstartError(formatErrorMsg(msg="OH NO!", lineno=1))
            self.assertEqual(err.message, "OH NO!")
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.value,
                             _format_error_message(msg="OH NO!", lineno=1))
            self.assertEqual(str(err),
                             _format_error_message(msg="OH NO!", lineno=1))
예제 #7
0
 def runTest(self):
     # Yes, I am aware I'm just checking off boxes now.
     self.assertEqual(str(KickstartError("OH NO!")), "OH NO!")
     self.assertEqual(str(KickstartParseError("OH NO!")), "OH NO!")
     self.assertEqual(str(KickstartVersionError("OH NO!")), "OH NO!")