Project

Profile

Help

Issue #1088 ยป repomd.py

Example repomd.xml reader. - jortel@redhat.com, 06/13/2016 05:52 PM

 
1
import os
2
import re
3

    
4
from xml.etree.cElementTree import ElementTree
5

    
6

    
7
class Repomd(object):
8

    
9
    FILE_NAME = 'repomd.xml'
10

    
11
    def __init__(self):
12
        self.files = {}
13

    
14
    def reset(self):
15
        self.files = {}
16

    
17
    def get_ns(self, root):
18
        match = re.match(r'\{.*\}', root.tag)
19
        return match.group(0)
20

    
21
    def read(self, path):
22
        self.reset()
23
        _dir = os.path.dirname(path)
24
        tree = ElementTree()
25
        tree.parse(path)
26
        root = tree.getroot()
27
        ns = self.get_ns(root)
28
        for data in root.iter('{ns}data'.format(ns=ns)):
29
            name = data.get('type')
30
            location = data.find('{ns}location'.format(ns=ns))
31
            href = location.get('href')
32
            basename = os.path.basename(href)
33
            self.files[name] = os.path.join(_dir, basename)
34

    
35

    
36
if __name__ == '__main__':
37
    repomd = Repomd()
38
    repomd.read('repomd.xml')
39
    for x in repomd.files.iteritems():
40
        print x
    (1-1/1)