Package yum :: Module repoMDObject
[hide private]
[frames] | no frames]

Source Code for Module yum.repoMDObject

  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  import types 
 26  from misc import AutoFileChecksums 
 27   
28 -def ns_cleanup(qn):
29 if qn.find('}') == -1: return qn 30 return qn.split('}')[1]
31
32 -class RepoData:
33 """represents anything beneath a <data> tag"""
34 - def __init__(self, elem):
35 self.type = elem.attrib.get('type') 36 self.location = (None, None) 37 self.checksum = (None,None) # type,value 38 self.openchecksum = (None,None) # type,value 39 self.timestamp = None 40 self.dbversion = None 41 self.size = None 42 self.opensize = None 43 44 self.parse(elem)
45
46 - def parse(self, elem):
47 48 for child in elem: 49 child_name = ns_cleanup(child.tag) 50 if child_name == 'location': 51 relative = child.attrib.get('href') 52 base = child.attrib.get('base') 53 self.location = (base, relative) 54 55 elif child_name == 'checksum': 56 csum_value = child.text 57 csum_type = child.attrib.get('type') 58 self.checksum = (csum_type,csum_value) 59 60 elif child_name == 'open-checksum': 61 csum_value = child.text 62 csum_type = child.attrib.get('type') 63 self.openchecksum = (csum_type, csum_value) 64 65 elif child_name == 'timestamp': 66 self.timestamp = child.text 67 elif child_name == 'database_version': 68 self.dbversion = child.text 69 elif child_name == 'size': 70 self.size = child.text 71 elif child_name == 'open-size': 72 self.opensize = child.text
73
74 -class RepoMD:
75 """represents the repomd xml file""" 76
77 - def __init__(self, repoid, srcfile):
78 """takes a repoid and a filename for the repomd.xml""" 79 80 self.timestamp = 0 81 self.repoid = repoid 82 self.repoData = {} 83 self.checksums = {} 84 self.length = 0 85 self.revision = None 86 self.tags = {'content' : set(), 'distro' : {}} 87 88 if type(srcfile) in types.StringTypes: 89 # srcfile is a filename string 90 try: 91 infile = open(srcfile, 'rt') 92 except IOError: 93 raise RepoMDError, "Unable to open %s" %(srcfile,) 94 else: 95 # srcfile is a file object 96 infile = srcfile 97 98 # We trust any of these to mean the repomd.xml is valid. 99 infile = AutoFileChecksums(infile, ['sha256', 'sha512'], 100 ignore_missing=True, ignore_none=True) 101 parser = iterparse(infile) 102 103 try: 104 for event, elem in parser: 105 elem_name = ns_cleanup(elem.tag) 106 107 if elem_name == "data": 108 thisdata = RepoData(elem=elem) 109 self.repoData[thisdata.type] = thisdata 110 try: 111 nts = int(thisdata.timestamp) 112 if nts > self.timestamp: # max() not in old python 113 self.timestamp = nts 114 except: 115 pass 116 elif elem_name == "revision": 117 self.revision = elem.text 118 elif elem_name == "tags": 119 for child in elem: 120 child_name = ns_cleanup(child.tag) 121 if child_name == 'content': 122 self.tags['content'].add(child.text) 123 if child_name == 'distro': 124 cpeid = child.attrib.get('cpeid', '') 125 distro = self.tags['distro'].setdefault(cpeid,set()) 126 distro.add(child.text) 127 128 self.checksums = infile.checksums.hexdigests() 129 self.length = len(infile.checksums) 130 except SyntaxError, e: 131 raise RepoMDError, "Damaged repomd.xml file"
132
133 - def fileTypes(self):
134 """return list of metadata file types available""" 135 return self.repoData.keys()
136
137 - def getData(self, type):
138 if type in self.repoData: 139 return self.repoData[type] 140 else: 141 raise RepoMDError, "requested datatype %s not available" % type
142
143 - def dump(self):
144 """dump fun output""" 145 146 print "file timestamp: %s" % self.timestamp 147 print "file length : %s" % self.length 148 for csum in sorted(self.checksums): 149 print "file checksum : %s/%s" % (csum, self.checksums[csum]) 150 if self.revision is not None: 151 print 'revision: %s' % self.revision 152 if self.tags['content']: 153 print 'tags content: %s' % ", ".join(sorted(self.tags['content'])) 154 if self.tags['distro']: 155 for distro in sorted(self.tags['distro']): 156 print 'tags distro: %s' % distro 157 tags = self.tags['distro'][distro] 158 print ' tags: %s' % ", ".join(sorted(tags)) 159 print '\n---- Data ----' 160 for ft in sorted(self.fileTypes()): 161 thisdata = self.repoData[ft] 162 print ' datatype: %s' % thisdata.type 163 print ' location : %s %s' % thisdata.location 164 print ' timestamp : %s' % thisdata.timestamp 165 print ' size : %s' % thisdata.size 166 print ' open size : %s' % thisdata.opensize 167 print ' checksum : %s - %s' % thisdata.checksum 168 print ' open checksum: %s - %s' % thisdata.openchecksum 169 print ' dbversion : %s' % thisdata.dbversion 170 print ''
171
172 -def main():
173 174 try: 175 print "file : %s" % sys.argv[1] 176 p = RepoMD('repoid', sys.argv[1]) 177 p.dump() 178 179 except IOError: 180 print >> sys.stderr, "newcomps.py: No such file:\'%s\'" % sys.argv[1] 181 sys.exit(1)
182 183 if __name__ == '__main__': 184 main() 185