3 # Copyright(c) 2009, Gentoo Foundation
5 # Licensed under the GNU General Public License, v2
9 """Provides an easy-to-use python interface to Gentoo's metadata.xml file.
12 >>> from gentoolkit.metadata import MetaData
13 >>> pkg_md = MetaData('/usr/portage/app-misc/gourmet/metadata.xml')
15 <MetaData '/usr/portage/app-misc/gourmet/metadata.xml'>
18 >>> for maint in pkg_md.maintainers():
19 ... print "{0} ({1})".format(maint.email, maint.name)
21 nixphoeni@gentoo.org (Joe Sapp)
22 >>> for flag in pkg_md.use():
23 ... print flag.name, "->", flag.description
25 rtf -> Enable export to RTF
26 gnome-print -> Enable printing support using gnome-print
27 >>> upstream = pkg_md.upstream()
29 [<_Upstream {'docs': [], 'remoteid': [], 'maintainer':
30 [<_Maintainer 'Thomas_Hinkle@alumni.brown.edu'>], 'bugtracker': [],
32 >>> upstream[0].maintainer[0].name
36 __all__ = ('MetaData',)
37 __docformat__ = 'epytext'
45 import xml.etree.cElementTree as etree
47 from portage import settings
53 class _Maintainer(object):
54 """An object for representing one maintainer.
56 @type email: str or None
57 @ivar email: Maintainer's email address. Used for both Gentoo and upstream.
58 @type name: str or None
59 @ivar name: Maintainer's name. Used for both Gentoo and upstream.
60 @type description: str or None
61 @ivar description: Description of what a maintainer does. Gentoo only.
62 @type restrict: str or None
63 @ivar restrict: e.g. >=portage-2.2 means only maintains versions
64 of Portage greater than 2.2. Should be DEPEND string with < and >
65 converted to < and > respectively.
66 @type status: str or None
67 @ivar status: If set, either 'active' or 'inactive'. Upstream only.
70 def __init__(self, node):
73 self.description = None
74 self.restrict = node.get('restrict')
75 self.status = node.get('status')
76 maint_attrs = node.getchildren()
77 for attr in maint_attrs:
78 setattr(self, attr.tag, attr.text)
81 return "<%s %r>" % (self.__class__.__name__, self.email)
84 class _Useflag(object):
85 """An object for representing one USE flag.
87 @todo: Is there any way to have a keyword option to leave in
88 <pkg> and <cat> for later processing?
89 @type name: str or None
91 @type restrict: str or None
92 @ivar restrict: e.g. >=portage-2.2 means flag is only avaiable in
93 versions greater than 2.2
94 @type description: str
95 @ivar description: description of the USE flag
98 def __init__(self, node):
99 self.name = node.get('name')
100 self.restrict = node.get('restrict')
104 for child in node.getchildren():
105 _desc += child.text if child.text else ''
106 _desc += child.tail if child.tail else ''
107 # This takes care of tabs and newlines left from the file
108 self.description = re.sub('\s+', ' ', _desc)
111 return "<%s %r>" % (self.__class__.__name__, self.name)
114 class _Upstream(object):
115 """An object for representing one package's upstream.
117 @type maintainers: list
118 @ivar maintainers: L{_Maintainer} objects for each upstream maintainer
119 @type changelogs: list
120 @ivar changelogs: URLs to upstream's ChangeLog file in str format
122 @ivar docs: Sequence of tuples containing URLs to upstream documentation
123 in the first slot and 'lang' attribute in the second, e.g.,
124 [('http.../docs/en/tut.html', None), ('http.../doc/fr/tut.html', 'fr')]
125 @type bugtrackers: list
126 @ivar bugtrackers: URLs to upstream's bugtracker. May also contain an email
127 address if prepended with 'mailto:'
128 @type remoteids: list
129 @ivar remoteids: Sequence of tuples containing the project's hosting site
130 name in the first slot and the project's ID name or number for that
131 site in the second, e.g., [('sourceforge', 'systemrescuecd')]
134 def __init__(self, node):
136 self.maintainers = self.upstream_maintainers()
137 self.changelogs = self.upstream_changelogs()
138 self.docs = self.upstream_documentation()
139 self.bugtrackers = self.upstream_bugtrackers()
140 self.remoteids = self.upstream_remoteids()
143 return "<%s %r>" % (self.__class__.__name__, self.__dict__)
145 def upstream_bugtrackers(self):
146 """Retrieve upstream bugtracker location from xml node."""
147 return [e.text for e in self.node.findall('bugs-to')]
149 def upstream_changelogs(self):
150 """Retrieve upstream changelog location from xml node."""
151 return [e.text for e in self.node.findall('changelog')]
153 def upstream_documentation(self):
154 """Retrieve upstream documentation location from xml node."""
156 for elem in self.node.findall('doc'):
157 lang = elem.get('lang')
158 result.append((elem.text, lang))
161 def upstream_maintainers(self):
162 """Retrieve upstream maintainer information from xml node."""
163 return [_Maintainer(m) for m in self.node.findall('maintainer')]
165 def upstream_remoteids(self):
166 """Retrieve upstream remote ID from xml node."""
167 return [(e.text, e.get('type')) for e in self.node.findall('remote-id')]
170 class MetaData(object):
171 """Access metadata.xml"""
173 def __init__(self, metadata_path):
174 """Parse a valid metadata.xml file.
176 @type metadata_path: str
177 @param metadata_path: path to a valid metadata.xml file
178 @raise IOError: if C{metadata_path} can not be read
181 self.metadata_path = metadata_path
182 self._xml_tree = etree.parse(metadata_path)
185 self._herdstree = None
186 self._descriptions = None
187 self._maintainers = None
188 self._useflags = None
189 self._upstream = None
192 return "<%s %r>" % (self.__class__.__name__, self.metadata_path)
194 def _get_herd_email(self, herd):
195 """Get a herd's email address.
198 @param herd: herd whose email you want
200 @return: email address or None if herd is not in herds.xml
201 @raise IOError: if $PORTDIR/metadata/herds.xml can not be read
204 if self._herdstree is None:
205 herds_path = os.path.join(settings['PORTDIR'], 'metadata/herds.xml')
207 self._herdstree = etree.parse(herds_path)
209 # For some trees, herds.xml may not exist. Bug #300108.
212 # Some special herds are not listed in herds.xml
213 if herd in ('no-herd', 'maintainer-wanted', 'maintainer-needed'):
216 for node in self._herdstree.getiterator('herd'):
217 if node.findtext('name') == herd:
218 return node.findtext('email')
220 def herds(self, include_email=False):
221 """Return a list of text nodes for <herd>.
223 @type include_email: bool
224 @keyword include_email: if True, also look up the herd's email
226 @return: if include_email is False, return a list of strings;
227 if include_email is True, return a list of tuples containing:
228 [('herd1', 'herd1@gentoo.org'), ('no-herd', None);
232 for elem in self._xml_tree.findall('herd'):
237 herd_mail = self._get_herd_email(text)
238 result.append((text, herd_mail))
244 def descriptions(self):
245 """Return a list of text nodes for <longdescription>.
248 @return: package description in string format
249 @todo: Support the C{lang} attribute
252 if self._descriptions is not None:
253 return self._descriptions
255 long_descriptions = self._xml_tree.findall("longdescription")
256 self._descriptions = [e.text for e in long_descriptions]
257 return self._descriptions
259 def maintainers(self):
260 """Get maintainers' name, email and description.
263 @return: a sequence of L{_Maintainer} objects in document order.
266 if self._maintainers is not None:
267 return self._maintainers
269 self._maintainers = []
270 for node in self._xml_tree.findall('maintainer'):
271 self._maintainers.append(_Maintainer(node))
273 return self._maintainers
276 """Get names and descriptions for USE flags defined in metadata.
279 @return: a sequence of L{_Useflag} objects in document order.
282 if self._useflags is not None:
283 return self._useflags
286 for node in self._xml_tree.getiterator('flag'):
287 self._useflags.append(_Useflag(node))
289 return self._useflags
292 """Get upstream contact information.
295 @return: a sequence of L{_Upstream} objects in document order.
298 if self._upstream is not None:
299 return self._upstream
302 for node in self._xml_tree.findall('upstream'):
303 self._upstream.append(_Upstream(node))
305 return self._upstream
307 # vim: set ts=4 sw=4 tw=79: