[variants] Stop printing stuff to stderr unconditionally

Instead of looking for the DTD at module load time do it only when
required. Use a logger (if supplied) to print the warning.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2016-04-01 09:24:17 +02:00
parent e5f37016e0
commit 620cecc409

View File

@ -29,22 +29,31 @@ if sys.version_info[0] == 3:
return (a > b) - (a < b) return (a > b) - (a < b)
VARIANTS_DTD = "/usr/share/pungi/variants.dtd" def get_variants_dtd(logger=None):
if not os.path.isfile(VARIANTS_DTD): """
DEVEL_VARIANTS_DTD = os.path.normpath(os.path.realpath(os.path.join(os.path.dirname(__file__), "..", "..", "share", "variants.dtd"))) Find the DTD for variants file. First look into the system directory, and
msg = "Variants DTD not found: %s" % VARIANTS_DTD fall back to local directory.
if os.path.isfile(DEVEL_VARIANTS_DTD): """
sys.stderr.write("%s\n" % msg) variants_dtd = "/usr/share/pungi/variants.dtd"
sys.stderr.write("Using alternative DTD: %s\n" % DEVEL_VARIANTS_DTD) if not os.path.isfile(variants_dtd):
VARIANTS_DTD = DEVEL_VARIANTS_DTD devel_variants_dtd = os.path.normpath(os.path.realpath(
else: os.path.join(os.path.dirname(__file__), "..", "..", "share", "variants.dtd")))
raise RuntimeError(msg) msg = "Variants DTD not found: %s" % variants_dtd
if os.path.isfile(devel_variants_dtd):
if logger:
logger.warning("%s", msg)
logger.warning("Using alternative DTD: %s", devel_variants_dtd)
variants_dtd = devel_variants_dtd
else:
raise RuntimeError(msg)
return variants_dtd
class VariantsXmlParser(object): class VariantsXmlParser(object):
def __init__(self, file_obj, tree_arches=None, tree_variants=None, logger=None): def __init__(self, file_obj, tree_arches=None, tree_variants=None, logger=None):
self.tree = lxml.etree.parse(file_obj) self.tree = lxml.etree.parse(file_obj)
self.dtd = lxml.etree.DTD(open(VARIANTS_DTD, "r")) with open(get_variants_dtd(logger), 'r') as f:
self.dtd = lxml.etree.DTD(f)
self.addons = {} self.addons = {}
self.layered_products = {} self.layered_products = {}
self.tree_arches = tree_arches self.tree_arches = tree_arches