Pass an ElementTree instance into parse_metadata_use() and
authorZac Medico <zmedico@gentoo.org>
Mon, 12 Apr 2010 00:58:18 +0000 (17:58 -0700)
committerZac Medico <zmedico@gentoo.org>
Mon, 12 Apr 2010 00:58:18 +0000 (17:58 -0700)
check_metadata_herds().

bin/repoman
pym/repoman/utilities.py

index a76bc8de50955be43f6d1a25c1758dc668bfc0e0..00a1fd50f421e7ecbfe55bb05e28aea6fbd0dc27 100755 (executable)
@@ -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))
index bb4281c1fb4764c824175ad3ceeb14348b0150e3..52ebba7254c59bdfafce4b70fc8dadc233b60cfb 100644 (file)
@@ -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