~ubuntu-branches/ubuntu/jaunty/yum/jaunty

« back to all changes in this revision

Viewing changes to yum/repoMDObject.py

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings
  • Date: 2008-07-28 23:20:59 UTC
  • mfrom: (2.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080728232059-24lo1r17smhr71l8
Tags: 3.2.12-1.2
* Non-maintainer upload
* Updated for compatibility with current python-pyme (Closes: #490368)
  based on patch by Martin Meredith <mez@ubuntu.com>
  - Changed import in yum/misc.py
  - Set versioned dependency on python-pyme

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -tt
 
2
# This program is free software; you can redistribute it and/or modify
 
3
# it under the terms of the GNU General Public License as published by
 
4
# the Free Software Foundation; either version 2 of the License, or
 
5
# (at your option) any later version.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU Library General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
15
# Copyright 2006 Duke University
 
16
 
 
17
try:
 
18
    from xml.etree import cElementTree
 
19
except ImportError:
 
20
    import cElementTree
 
21
iterparse = cElementTree.iterparse
 
22
from Errors import RepoMDError
 
23
 
 
24
import sys
 
25
 
 
26
def ns_cleanup(qn):
 
27
    if qn.find('}') == -1: return qn 
 
28
    return qn.split('}')[1]
 
29
 
 
30
class RepoData:
 
31
    """represents anything beneath a <data> tag"""
 
32
    def __init__(self, elem):
 
33
        self.type = elem.attrib.get('type')
 
34
        self.location = (None, None)
 
35
        self.checksum = (None,None) # type,value
 
36
        self.openchecksum = (None,None) # type,value
 
37
        self.timestamp = None
 
38
        self.dbversion = None
 
39
    
 
40
        self.parse(elem)
 
41
 
 
42
    def parse(self, elem):
 
43
        
 
44
        for child in elem:
 
45
            child_name = ns_cleanup(child.tag)
 
46
            if child_name == 'location':
 
47
                relative = child.attrib.get('href')
 
48
                base = child.attrib.get('base')
 
49
                self.location = (base, relative)
 
50
            
 
51
            elif child_name == 'checksum':
 
52
                csum_value = child.text
 
53
                csum_type = child.attrib.get('type')
 
54
                self.checksum = (csum_type,csum_value)
 
55
 
 
56
            elif child_name == 'open-checksum':
 
57
                csum_value = child.text
 
58
                csum_type = child.attrib.get('type')
 
59
                self.openchecksum = (csum_type, csum_value)
 
60
            
 
61
            elif child_name == 'timestamp':
 
62
                self.timestamp = child.text
 
63
            elif child_name == 'database_version':
 
64
                self.dbversion = child.text
 
65
        
 
66
class RepoMD:
 
67
    """represents the repomd xml file"""
 
68
    
 
69
    def __init__(self, repoid, srcfile):
 
70
        """takes a repoid and a filename for the repomd.xml"""
 
71
        
 
72
        self.repoid = repoid
 
73
        self.repoData = {}
 
74
        
 
75
        if type(srcfile) == type('str'):
 
76
            # srcfile is a filename string
 
77
            infile = open(srcfile, 'rt')
 
78
        else:
 
79
            # srcfile is a file object
 
80
            infile = srcfile
 
81
        
 
82
        parser = iterparse(infile)
 
83
        
 
84
        try:
 
85
            for event, elem in parser:
 
86
                elem_name = ns_cleanup(elem.tag)
 
87
                
 
88
                if elem_name == "data":
 
89
                    thisdata = RepoData(elem=elem)
 
90
                    self.repoData[thisdata.type] = thisdata
 
91
        except SyntaxError, e:
 
92
            raise RepoMDError, "Damaged repomd.xml file"
 
93
            
 
94
    def fileTypes(self):
 
95
        """return list of metadata file types available"""
 
96
        return self.repoData.keys()
 
97
    
 
98
    def getData(self, type):
 
99
        if self.repoData.has_key(type):
 
100
            return self.repoData[type]
 
101
        else:
 
102
            raise RepoMDError, "requested datatype %s not available" % type
 
103
            
 
104
    def dump(self):
 
105
        """dump fun output"""
 
106
        
 
107
        for ft in self.fileTypes():
 
108
            thisdata = self.repoData[ft]
 
109
            print 'datatype: %s' % thisdata.type
 
110
            print 'location: %s %s' % thisdata.location
 
111
            print 'timestamp: %s' % thisdata.timestamp
 
112
            print 'checksum: %s -%s' % thisdata.checksum
 
113
            print 'open checksum: %s - %s' %  thisdata.openchecksum
 
114
            print 'dbversion: %s' % thisdata.dbversion
 
115
 
 
116
def main():
 
117
 
 
118
    try:
 
119
        print sys.argv[1]
 
120
        p = RepoMD('repoid', sys.argv[1])
 
121
        p.dump()
 
122
        
 
123
    except IOError:
 
124
        print >> sys.stderr, "newcomps.py: No such file:\'%s\'" % sys.argv[1]
 
125
        sys.exit(1)
 
126
        
 
127
if __name__ == '__main__':
 
128
    main()
 
129