All xml parsing use xml2obj
authorLingchao Xin <lingchaox.xin@intel.com>
Thu, 28 Nov 2013 08:51:29 +0000 (16:51 +0800)
committerLingchao Xin <lingchaox.xin@intel.com>
Thu, 28 Nov 2013 08:51:29 +0000 (16:51 +0800)
Change-Id: Id807e67ed6a2a689d7bc2bd24cbee393bdca8e92

snapdiff/repo.py

index 4d203651b458faa2dd5ee43b678c38357d0b21c1..1e5350837028e13fdea2ce8fa35b4778be57d8d7 100644 (file)
@@ -1,4 +1,4 @@
-from . import utils
+from .utils import xml2obj
 from .render import output_html
 
 import gzip
@@ -16,18 +16,17 @@ class RepoError(Exception):
 
 def _get_primary_md(url, workspace, name):
     """Get primary.xml.gz file from remote repodata directory"""
-    tree = ET.ElementTree(file=_download(url + '/repodata/repomd.xml', \
-            workspace, name))
-    for elem in tree.iter(tag='{http://linux.duke.edu/metadata/repo}data'):
-        if elem.attrib['type'] == 'primary':
-            for c in elem:
-                if c.tag == '{http://linux.duke.edu/metadata/repo}location':
-                    href = c.attrib['href']
-                    if href:
-                        return _download(url + href, workspace, \
-                                href.split('/')[-1])
-    else:
-        raise RepoError('Repo primary metadata can\'t be found !')
+    repomd_xml = _download(url + '/repodata/repomd.xml', workspace, name)
+    with open(repomd_xml, 'rb') as repomd:
+        data = xml2obj(repomd.read())
+        for item in data['data']:
+            if item.type == 'primary':
+                href = item.location.href
+                if href:
+                    return _download(url + href, workspace,
+                            href.split('/')[-1])
+        else:
+            raise RepoError('Repo primary metadata can\'t be found !')
 
 def _download(url, workspace, name):
     """Download needed xml file to local by given url"""
@@ -50,7 +49,7 @@ class Repo(object):
     def __init__(self, url):
         workspace = tempfile.mkdtemp(dir='/var/tmp')
         primary_md = _get_primary_md(url, workspace, 'repomd.xml')
-        self._et = utils.xml2obj(gzip.open(primary_md))
+        self._et = xml2obj(gzip.open(primary_md))
 
     @property
     def packages(self):