From 5a5e51a2737a0855bb562683f50c57cc31587460 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 11 Apr 2010 17:58:18 -0700 Subject: [PATCH] Pass an ElementTree instance into parse_metadata_use() and check_metadata_herds(). --- bin/repoman | 34 ++++++++++++++++++---------------- pym/repoman/utilities.py | 18 ++---------------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/bin/repoman b/bin/repoman index a76bc8de5..00a1fd50f 100755 --- a/bin/repoman +++ b/bin/repoman @@ -10,10 +10,6 @@ from __future__ import print_function import calendar import codecs -try: - from subprocess import getstatusoutput as subprocess_getstatusoutput -except ImportError: - from commands import getstatusoutput as subprocess_getstatusoutput import errno import formatter import logging @@ -25,6 +21,8 @@ import sys import tempfile import time import platform +import xml.etree.ElementTree +from xml.parsers.expat import ExpatError try: from urllib.request import urlopen as urllib_request_urlopen @@ -45,6 +43,7 @@ except ImportError: import portage portage._disable_legacy_globals() from portage import os +from portage import subprocess_getstatusoutput from portage import _encodings from portage import _unicode_encode from portage import StringIO @@ -981,6 +980,14 @@ check_ebuild_notadded = not \ # Build a regex from thirdpartymirrors for the SRC_URI.mirror check. thirdpartymirrors = portage.flatten(list(repoman_settings.thirdpartymirrors().values())) +class _MetadataTreeBuilder(xml.etree.ElementTree.TreeBuilder): + """ + Implements doctype() as required to avoid deprecation warnings with + >=python-2.7. + """ + def doctype(self, name, pubid, system): + pass + try: herd_base = make_herd_base(os.path.join(repoman_settings["PORTDIR"], "metadata/herds.xml")) except (EnvironmentError, ParseError, PermissionDenied) as e: @@ -1307,28 +1314,23 @@ for x in scanlist: # read metadata.xml into memory try: - f = open(os.path.join(checkdir, "metadata.xml")) - _metadata_xml = f.read() - f.close() - except (EnvironmentError, ) as e: + _metadata_xml = xml.etree.ElementTree.parse( + os.path.join(checkdir, "metadata.xml"), + parser=xml.etree.ElementTree.XMLParser( + target=_MetadataTreeBuilder())) + except (ExpatError, EnvironmentError) as e: metadata_bad = True stats["metadata.bad"] += 1 fails["metadata.bad"].append("%s/metadata.xml: %s" % (x, e)) del e else: # load USE flags from metadata.xml - try: - utilities.parse_metadata_use(_metadata_xml, muselist) - except (EnvironmentError, ParseError) as e: - metadata_bad = True - stats["metadata.bad"] += 1 - fails["metadata.bad"].append("%s/metadata.xml: %s" % (x, e)) - del e + utilities.parse_metadata_use(_metadata_xml, muselist) # Run other metadata.xml checkers try: utilities.check_metadata(_metadata_xml, herd_base) - except (EnvironmentError, ParseError, utilities.UnknownHerdsError) as e: + except (utilities.UnknownHerdsError, ) as e: metadata_bad = True stats["metadata.bad"] += 1 fails["metadata.bad"].append("%s/metadata.xml: %s" % (x, e)) diff --git a/pym/repoman/utilities.py b/pym/repoman/utilities.py index bb4281c1f..52ebba725 100644 --- a/pym/repoman/utilities.py +++ b/pym/repoman/utilities.py @@ -23,8 +23,6 @@ import codecs import errno import logging import sys -from xml.parsers.expat import ExpatError -import xml.etree.ElementTree as ET from portage import os from portage import subprocess_getstatusoutput from portage import _encodings @@ -108,18 +106,13 @@ def have_profile_dir(path, maxdepth=3, filename="profiles.desc"): path = normalize_path(path + "/..") maxdepth -= 1 -def parse_metadata_use(metadata_xml_content, uselist=None): +def parse_metadata_use(xml_tree, uselist=None): """ Records are wrapped in XML as per GLEP 56 returns a dict of the form a list of flags""" if uselist is None: uselist = [] - try: - xml_tree = ET.fromstring(metadata_xml_content) - except (ExpatError, ) as e: - raise exception.ParseError("metadata.xml: " + str(e)) - usetag = xml_tree.findall("use") if not usetag: return uselist @@ -154,17 +147,10 @@ def check_metadata_herds(xml_tree, herd_base): if unknown_herds: raise UnknownHerdsError(unknown_herds) - -def check_metadata(metadata_xml_content, herd_base): - try: - xml_tree = ET.fromstring(metadata_xml_content) - except (ExpatError, ) as e: - raise exception.ParseError("metadata.xml: " + str(e)) - +def check_metadata(xml_tree, herd_base): if herd_base is not None: check_metadata_herds(xml_tree, herd_base) - def FindPackagesToScan(settings, startdir, reposplit): """ Try to find packages that need to be scanned -- 2.26.2