From 75331849ea317899e3833a35728f6b6de65c61aa Mon Sep 17 00:00:00 2001 From: Nikos Skalkotos <skalkoto@grnet.gr> Date: Mon, 7 Apr 2014 11:25:14 +0300 Subject: [PATCH] Canonicalize distro & osfamily params in os_cls() If the distro name as returned by libguestfs's inspect_get_distro was redhat-based, since hyphens are not allowed in module names, importing module redhat-based would create an ImportError exception, even if file redhat-based.py was present. Although we don't have any redhat-based.py module and the code will still correctly fall back to importing linux.py this can be considered as a bug. In order to overcome this, we replace the hyphens with underscores in the distro and osfamily names. In the future, if we have to create a module for redhat-based distros we will name it redhat_based.py. --- image_creator/os_type/__init__.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/image_creator/os_type/__init__.py b/image_creator/os_type/__init__.py index b685814..7a4ab04 100644 --- a/image_creator/os_type/__init__.py +++ b/image_creator/os_type/__init__.py @@ -46,17 +46,27 @@ from functools import wraps def os_cls(distro, osfamily): - """Given the distro name and the osfamily, return the appropriate class""" - module = None - classname = None + """Given the distro name and the osfamily, return the appropriate OSBase + derived class + """ + + # hyphens are not allowed in module names + canonicalize = lambda x: x.replace('-', '_').lower() + + distro = canonicalize(distro) + osfamily = canonicalize(osfamily) + try: module = __import__("image_creator.os_type.%s" % distro, fromlist=['image_creator.os_type']) classname = distro.capitalize() except ImportError: - module = __import__("image_creator.os_type.%s" % osfamily, - fromlist=['image_creator.os_type']) - classname = osfamily.capitalize() + try: + module = __import__("image_creator.os_type.%s" % osfamily, + fromlist=['image_creator.os_type']) + classname = osfamily.capitalize() + except ImportError: + raise FatalError("Unknown OS name: `%s'" % osfamily) return getattr(module, classname) -- GitLab