Avoid a TypeError when language is None and feed description is empty
[zeroinstall/solver.git] / zeroinstall / injector / model.py
blob02721def5f92420cffff9d94ec90a5fb333f335d
1 """In-memory representation of interfaces and other data structures.
3 The objects in this module are used to build a representation of an XML interface
4 file in memory.
6 @see: L{reader} constructs these data-structures
7 @see: U{http://0install.net/interface-spec.html} description of the domain model
9 @var defaults: Default values for the 'default' attribute for <environment> bindings of
10 well-known variables.
11 """
13 # Copyright (C) 2009, Thomas Leonard
14 # See the README file for details, or visit http://0install.net.
16 from zeroinstall import _
17 import os, re, locale
18 from logging import info, debug, warn
19 from zeroinstall import SafeException, version
20 from zeroinstall.injector.namespaces import XMLNS_IFACE
21 from zeroinstall.injector import qdom
23 # Element names for bindings in feed files
24 binding_names = frozenset(['environment', 'overlay'])
26 network_offline = 'off-line'
27 network_minimal = 'minimal'
28 network_full = 'full'
29 network_levels = (network_offline, network_minimal, network_full)
31 stability_levels = {} # Name -> Stability
33 defaults = {
34 'PATH': '/bin:/usr/bin',
35 'XDG_CONFIG_DIRS': '/etc/xdg',
36 'XDG_DATA_DIRS': '/usr/local/share:/usr/share',
39 class InvalidInterface(SafeException):
40 """Raised when parsing an invalid feed."""
41 feed_url = None
43 def __init__(self, message, ex = None):
44 if ex:
45 try:
46 message += "\n\n(exact error: %s)" % ex
47 except:
48 # Some Python messages have type str but contain UTF-8 sequences.
49 # (e.g. IOException). Adding these to a Unicode 'message' (e.g.
50 # after gettext translation) will cause an error.
51 import codecs
52 decoder = codecs.lookup('utf-8')
53 decex = decoder.decode(str(ex), errors = 'replace')[0]
54 message += "\n\n(exact error: %s)" % decex
56 SafeException.__init__(self, message)
58 def __str__(self):
59 if self.feed_url:
60 return SafeException.__str__(self) + ' in ' + self.feed_url
61 return SafeException.__str__(self)
63 def _split_arch(arch):
64 """Split an arch into an (os, machine) tuple. Either or both parts may be None."""
65 if not arch:
66 return None, None
67 elif '-' not in arch:
68 raise SafeException(_("Malformed arch '%s'") % arch)
69 else:
70 osys, machine = arch.split('-', 1)
71 if osys == '*': osys = None
72 if machine == '*': machine = None
73 return osys, machine
75 def _join_arch(osys, machine):
76 if osys == machine == None: return None
77 return "%s-%s" % (osys or '*', machine or '*')
79 def _best_language_match(options):
80 (language, encoding) = locale.getlocale(locale.LC_ALL)
81 return (options.get(language, None) or
82 (language and options.get(language.split('_', 1)[0], None)) or
83 options.get(None, None))
85 class Stability(object):
86 """A stability rating. Each implementation has an upstream stability rating and,
87 optionally, a user-set rating."""
88 __slots__ = ['level', 'name', 'description']
89 def __init__(self, level, name, description):
90 self.level = level
91 self.name = name
92 self.description = description
93 assert name not in stability_levels
94 stability_levels[name] = self
96 def __cmp__(self, other):
97 return cmp(self.level, other.level)
99 def __str__(self):
100 return self.name
102 def __repr__(self):
103 return _("<Stability: %s>") % self.description
105 def process_binding(e):
106 """Internal"""
107 if e.name == 'environment':
108 mode = {
109 None: EnvironmentBinding.PREPEND,
110 'prepend': EnvironmentBinding.PREPEND,
111 'append': EnvironmentBinding.APPEND,
112 'replace': EnvironmentBinding.REPLACE,
113 }[e.getAttribute('mode')]
115 binding = EnvironmentBinding(e.getAttribute('name'),
116 insert = e.getAttribute('insert'),
117 default = e.getAttribute('default'),
118 value = e.getAttribute('value'),
119 mode = mode)
120 if not binding.name: raise InvalidInterface(_("Missing 'name' in binding"))
121 if binding.insert is None and binding.value is None:
122 raise InvalidInterface(_("Missing 'insert' or 'value' in binding"))
123 if binding.insert is not None and binding.value is not None:
124 raise InvalidInterface(_("Binding contains both 'insert' and 'value'"))
125 return binding
126 elif e.name == 'overlay':
127 return OverlayBinding(e.getAttribute('src'), e.getAttribute('mount-point'))
128 else:
129 raise Exception(_("Unknown binding type '%s'") % e.name)
131 def process_depends(item, local_feed_dir):
132 """Internal"""
133 # Note: also called from selections
134 attrs = item.attrs
135 dep_iface = item.getAttribute('interface')
136 if not dep_iface:
137 raise InvalidInterface(_("Missing 'interface' on <%s>") % item.name)
138 if dep_iface.startswith('./'):
139 if local_feed_dir:
140 dep_iface = os.path.abspath(os.path.join(local_feed_dir, dep_iface))
141 # (updates the element too, in case we write it out again)
142 attrs['interface'] = dep_iface
143 else:
144 raise InvalidInterface(_('Relative interface URI "%s" in non-local feed') % dep_iface)
145 dependency = InterfaceDependency(dep_iface, element = item)
147 for e in item.childNodes:
148 if e.uri != XMLNS_IFACE: continue
149 if e.name in binding_names:
150 dependency.bindings.append(process_binding(e))
151 elif e.name == 'version':
152 dependency.restrictions.append(
153 VersionRangeRestriction(not_before = parse_version(e.getAttribute('not-before')),
154 before = parse_version(e.getAttribute('before'))))
155 return dependency
157 def N_(message): return message
159 insecure = Stability(0, N_('insecure'), _('This is a security risk'))
160 buggy = Stability(5, N_('buggy'), _('Known to have serious bugs'))
161 developer = Stability(10, N_('developer'), _('Work-in-progress - bugs likely'))
162 testing = Stability(20, N_('testing'), _('Stability unknown - please test!'))
163 stable = Stability(30, N_('stable'), _('Tested - no serious problems found'))
164 packaged = Stability(35, N_('packaged'), _('Supplied by the local package manager'))
165 preferred = Stability(40, N_('preferred'), _('Best of all - must be set manually'))
167 del N_
169 class Restriction(object):
170 """A Restriction limits the allowed implementations of an Interface."""
171 __slots__ = []
173 def meets_restriction(self, impl):
174 """Called by the L{solver.Solver} to check whether a particular implementation is acceptable.
175 @return: False if this implementation is not a possibility
176 @rtype: bool
178 raise NotImplementedError(_("Abstract"))
180 class VersionRestriction(Restriction):
181 """Only select implementations with a particular version number.
182 @since: 0.40"""
184 def __init__(self, version):
185 """@param version: the required version number
186 @see: L{parse_version}; use this to pre-process the version number
188 self.version = version
190 def meets_restriction(self, impl):
191 return impl.version == self.version
193 def __str__(self):
194 return _("(restriction: version = %s)") % format_version(self.version)
196 class VersionRangeRestriction(Restriction):
197 """Only versions within the given range are acceptable"""
198 __slots__ = ['before', 'not_before']
200 def __init__(self, before, not_before):
201 """@param before: chosen versions must be earlier than this
202 @param not_before: versions must be at least this high
203 @see: L{parse_version}; use this to pre-process the versions
205 self.before = before
206 self.not_before = not_before
208 def meets_restriction(self, impl):
209 if self.not_before and impl.version < self.not_before:
210 return False
211 if self.before and impl.version >= self.before:
212 return False
213 return True
215 def __str__(self):
216 if self.not_before is not None or self.before is not None:
217 range = ''
218 if self.not_before is not None:
219 range += format_version(self.not_before) + ' <= '
220 range += 'version'
221 if self.before is not None:
222 range += ' < ' + format_version(self.before)
223 else:
224 range = 'none'
225 return _("(restriction: %s)") % range
227 class Binding(object):
228 """Information about how the choice of a Dependency is made known
229 to the application being run."""
231 class EnvironmentBinding(Binding):
232 """Indicate the chosen implementation using an environment variable."""
233 __slots__ = ['name', 'insert', 'default', 'mode', 'value']
235 PREPEND = 'prepend'
236 APPEND = 'append'
237 REPLACE = 'replace'
239 def __init__(self, name, insert, default = None, mode = PREPEND, value=None):
241 mode argument added in version 0.28
242 value argument added in version 0.52
244 self.name = name
245 self.insert = insert
246 self.default = default
247 self.mode = mode
248 self.value = value
250 def __str__(self):
251 return _("<environ %(name)s %(mode)s %(insert)s %(value)s>") % {'name': self.name,'mode': self.mode, 'insert': self.insert, 'value': self.value}
253 __repr__ = __str__
255 def get_value(self, path, old_value):
256 """Calculate the new value of the environment variable after applying this binding.
257 @param path: the path to the selected implementation
258 @param old_value: the current value of the environment variable
259 @return: the new value for the environment variable"""
261 if self.insert is not None:
262 extra = os.path.join(path, self.insert)
263 else:
264 assert self.value is not None
265 extra = self.value
267 if self.mode == EnvironmentBinding.REPLACE:
268 return extra
270 if old_value is None:
271 old_value = self.default or defaults.get(self.name, None)
272 if old_value is None:
273 return extra
274 if self.mode == EnvironmentBinding.PREPEND:
275 return extra + os.pathsep + old_value
276 else:
277 return old_value + os.pathsep + extra
279 def _toxml(self, doc):
280 """Create a DOM element for this binding.
281 @param doc: document to use to create the element
282 @return: the new element
284 env_elem = doc.createElementNS(XMLNS_IFACE, 'environment')
285 env_elem.setAttributeNS(None, 'name', self.name)
286 if self.insert is not None:
287 env_elem.setAttributeNS(None, 'insert', self.insert)
288 else:
289 env_elem.setAttributeNS(None, 'value', self.value)
290 if self.default:
291 env_elem.setAttributeNS(None, 'default', self.default)
292 return env_elem
294 class OverlayBinding(Binding):
295 """Make the chosen implementation available by overlaying it onto another part of the file-system.
296 This is to support legacy programs which use hard-coded paths."""
297 __slots__ = ['src', 'mount_point']
299 def __init__(self, src, mount_point):
300 self.src = src
301 self.mount_point = mount_point
303 def __str__(self):
304 return _("<overlay %(src)s on %(mount_point)s>") % {'src': self.src or '.', 'mount_point': self.mount_point or '/'}
306 __repr__ = __str__
308 def _toxml(self, doc):
309 """Create a DOM element for this binding.
310 @param doc: document to use to create the element
311 @return: the new element
313 env_elem = doc.createElementNS(XMLNS_IFACE, 'overlay')
314 if self.src is not None:
315 env_elem.setAttributeNS(None, 'src', self.src)
316 if self.mount_point is not None:
317 env_elem.setAttributeNS(None, 'mount-point', self.mount_point)
318 return env_elem
320 class Feed(object):
321 """An interface's feeds are other interfaces whose implementations can also be
322 used as implementations of this interface."""
323 __slots__ = ['uri', 'os', 'machine', 'user_override', 'langs']
324 def __init__(self, uri, arch, user_override, langs = None):
325 self.uri = uri
326 # This indicates whether the feed comes from the user's overrides
327 # file. If true, writer.py will write it when saving.
328 self.user_override = user_override
329 self.os, self.machine = _split_arch(arch)
330 self.langs = langs
332 def __str__(self):
333 return "<Feed from %s>" % self.uri
334 __repr__ = __str__
336 arch = property(lambda self: _join_arch(self.os, self.machine))
338 class Dependency(object):
339 """A Dependency indicates that an Implementation requires some additional
340 code to function. This is an abstract base class.
341 @ivar qdom: the XML element for this Dependency (since 0launch 0.51)
342 @type qdom: L{qdom.Element}
343 @ivar metadata: any extra attributes from the XML element
344 @type metadata: {str: str}
346 __slots__ = ['qdom']
348 def __init__(self, element):
349 assert isinstance(element, qdom.Element), type(element) # Use InterfaceDependency instead!
350 self.qdom = element
352 @property
353 def metadata(self):
354 return self.qdom.attrs
356 class InterfaceDependency(Dependency):
357 """A Dependency on a Zero Install interface.
358 @ivar interface: the interface required by this dependency
359 @type interface: str
360 @ivar restrictions: a list of constraints on acceptable implementations
361 @type restrictions: [L{Restriction}]
362 @ivar bindings: how to make the choice of implementation known
363 @type bindings: [L{Binding}]
364 @since: 0.28
366 __slots__ = ['interface', 'restrictions', 'bindings']
368 def __init__(self, interface, restrictions = None, element = None):
369 Dependency.__init__(self, element)
370 assert isinstance(interface, (str, unicode))
371 assert interface
372 self.interface = interface
373 if restrictions is None:
374 self.restrictions = []
375 else:
376 self.restrictions = restrictions
377 self.bindings = []
379 def __str__(self):
380 return _("<Dependency on %(interface)s; bindings: %(bindings)s%(restrictions)s>") % {'interface': self.interface, 'bindings': self.bindings, 'restrictions': self.restrictions}
382 class RetrievalMethod(object):
383 """A RetrievalMethod provides a way to fetch an implementation."""
384 __slots__ = []
386 class DownloadSource(RetrievalMethod):
387 """A DownloadSource provides a way to fetch an implementation."""
388 __slots__ = ['implementation', 'url', 'size', 'extract', 'start_offset', 'type']
390 def __init__(self, implementation, url, size, extract, start_offset = 0, type = None):
391 self.implementation = implementation
392 self.url = url
393 self.size = size
394 self.extract = extract
395 self.start_offset = start_offset
396 self.type = type # MIME type - see unpack.py
398 class Recipe(RetrievalMethod):
399 """Get an implementation by following a series of steps.
400 @ivar size: the combined download sizes from all the steps
401 @type size: int
402 @ivar steps: the sequence of steps which must be performed
403 @type steps: [L{RetrievalMethod}]"""
404 __slots__ = ['steps']
406 def __init__(self):
407 self.steps = []
409 size = property(lambda self: sum([x.size for x in self.steps]))
411 class DistributionSource(RetrievalMethod):
412 """A package that is installed using the distribution's tools (including PackageKit).
413 @ivar install: a function to call to install this package
414 @type install: (L{handler.Handler}) -> L{tasks.Blocker}
415 @ivar package_id: the package name, in a form recognised by the distribution's tools
416 @type package_id: str
417 @ivar size: the download size in bytes
418 @type size: int
419 @ivar needs_confirmation: whether the user should be asked to confirm before calling install()
420 @type needs_confirmation: bool"""
422 __slots__ = ['package_id', 'size', 'install', 'needs_confirmation']
424 def __init__(self, package_id, size, install, needs_confirmation = True):
425 RetrievalMethod.__init__(self)
426 self.package_id = package_id
427 self.size = size
428 self.install = install
429 self.needs_confirmation = needs_confirmation
431 class Command(object):
432 """A Command is a way of running an Implementation as a program."""
434 __slots__ = ['qdom', '_depends', '_local_dir', '_runner']
436 def __init__(self, qdom, local_dir):
437 """@param qdom: the <command> element
438 @param local_dir: the directory containing the feed (for relative dependencies), or None if not local
440 assert qdom.name == 'command', 'not <command>: %s' % qdom
441 self.qdom = qdom
442 self._local_dir = local_dir
443 self._depends = None
445 path = property(lambda self: self.qdom.attrs.get("path", None))
447 def _toxml(self, doc, prefixes):
448 return self.qdom.toDOM(doc, prefixes)
450 @property
451 def requires(self):
452 if self._depends is None:
453 self._runner = None
454 depends = []
455 for child in self.qdom.childNodes:
456 if child.name == 'requires':
457 dep = process_depends(child, self._local_dir)
458 depends.append(dep)
459 elif child.name == 'runner':
460 if self._runner:
461 raise InvalidInterface(_("Multiple <runner>s in <command>!"))
462 dep = process_depends(child, self._local_dir)
463 depends.append(dep)
464 self._runner = dep
465 self._depends = depends
466 return self._depends
468 def get_runner(self):
469 self.requires # (sets _runner)
470 return self._runner
472 class Implementation(object):
473 """An Implementation is a package which implements an Interface.
474 @ivar download_sources: list of methods of getting this implementation
475 @type download_sources: [L{RetrievalMethod}]
476 @ivar feed: the feed owning this implementation (since 0.32)
477 @type feed: [L{ZeroInstallFeed}]
478 @ivar bindings: how to tell this component where it itself is located (since 0.31)
479 @type bindings: [Binding]
480 @ivar upstream_stability: the stability reported by the packager
481 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged]
482 @ivar user_stability: the stability as set by the user
483 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged | preferred]
484 @ivar langs: natural languages supported by this package
485 @type langs: str
486 @ivar requires: interfaces this package depends on
487 @type requires: [L{Dependency}]
488 @ivar commands: ways to execute as a program
489 @type commands: {str: Command}
490 @ivar metadata: extra metadata from the feed
491 @type metadata: {"[URI ]localName": str}
492 @ivar id: a unique identifier for this Implementation
493 @ivar version: a parsed version number
494 @ivar released: release date
495 @ivar local_path: the directory containing this local implementation, or None if it isn't local (id isn't a path)
496 @type local_path: str | None
497 @ivar requires_root_install: whether the user will need admin rights to use this
498 @type requires_root_install: bool
501 # Note: user_stability shouldn't really be here
503 __slots__ = ['upstream_stability', 'user_stability', 'langs',
504 'requires', 'metadata', 'download_sources', 'commands',
505 'id', 'feed', 'version', 'released', 'bindings', 'machine']
507 def __init__(self, feed, id):
508 assert id
509 self.feed = feed
510 self.id = id
511 self.user_stability = None
512 self.upstream_stability = None
513 self.metadata = {} # [URI + " "] + localName -> value
514 self.requires = []
515 self.version = None
516 self.released = None
517 self.download_sources = []
518 self.langs = ""
519 self.machine = None
520 self.bindings = []
521 self.commands = {}
523 def get_stability(self):
524 return self.user_stability or self.upstream_stability or testing
526 def __str__(self):
527 return self.id
529 def __repr__(self):
530 return "v%s (%s)" % (self.get_version(), self.id)
532 def __cmp__(self, other):
533 """Newer versions come first"""
534 d = cmp(other.version, self.version)
535 if d: return d
536 # If the version number is the same, just give a stable sort order, and
537 # ensure that two different implementations don't compare equal.
538 d = cmp(other.feed.url, self.feed.url)
539 if d: return d
540 return cmp(other.id, self.id)
542 def get_version(self):
543 """Return the version as a string.
544 @see: L{format_version}
546 return format_version(self.version)
548 arch = property(lambda self: _join_arch(self.os, self.machine))
550 os = None
551 local_path = None
552 digests = None
553 requires_root_install = False
555 def _get_main(self):
556 """"@deprecated: use commands["run"] instead"""
557 main = self.commands.get("run", None)
558 if main is not None:
559 return main.path
560 return None
561 def _set_main(self, path):
562 """"@deprecated: use commands["run"] instead"""
563 if path is None:
564 if "run" in self.commands:
565 del self.commands["run"]
566 else:
567 self.commands["run"] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': path}), None)
568 main = property(_get_main, _set_main)
570 class DistributionImplementation(Implementation):
571 """An implementation provided by the distribution. Information such as the version
572 comes from the package manager.
573 @since: 0.28"""
574 __slots__ = ['distro', 'installed']
576 def __init__(self, feed, id, distro):
577 assert id.startswith('package:')
578 Implementation.__init__(self, feed, id)
579 self.distro = distro
580 self.installed = False
582 @property
583 def requires_root_install(self):
584 return not self.installed
586 class ZeroInstallImplementation(Implementation):
587 """An implementation where all the information comes from Zero Install.
588 @ivar digests: a list of "algorith=value" strings (since 0.45)
589 @type digests: [str]
590 @since: 0.28"""
591 __slots__ = ['os', 'size', 'digests', 'local_path']
593 def __init__(self, feed, id, local_path):
594 """id can be a local path (string starting with /) or a manifest hash (eg "sha1=XXX")"""
595 assert not id.startswith('package:'), id
596 Implementation.__init__(self, feed, id)
597 self.size = None
598 self.os = None
599 self.digests = []
600 self.local_path = local_path
602 # Deprecated
603 dependencies = property(lambda self: dict([(x.interface, x) for x in self.requires
604 if isinstance(x, InterfaceDependency)]))
606 def add_download_source(self, url, size, extract, start_offset = 0, type = None):
607 """Add a download source."""
608 self.download_sources.append(DownloadSource(self, url, size, extract, start_offset, type))
610 def set_arch(self, arch):
611 self.os, self.machine = _split_arch(arch)
612 arch = property(lambda self: _join_arch(self.os, self.machine), set_arch)
614 class Interface(object):
615 """An Interface represents some contract of behaviour.
616 @ivar uri: the URI for this interface.
617 @ivar stability_policy: user's configured policy.
618 Implementations at this level or higher are preferred.
619 Lower levels are used only if there is no other choice.
621 __slots__ = ['uri', 'stability_policy', 'extra_feeds']
623 implementations = property(lambda self: self._main_feed.implementations)
624 name = property(lambda self: self._main_feed.name)
625 description = property(lambda self: self._main_feed.description)
626 summary = property(lambda self: self._main_feed.summary)
627 last_modified = property(lambda self: self._main_feed.last_modified)
628 feeds = property(lambda self: self.extra_feeds + self._main_feed.feeds)
629 metadata = property(lambda self: self._main_feed.metadata)
631 last_checked = property(lambda self: self._main_feed.last_checked)
633 def __init__(self, uri):
634 assert uri
635 if uri.startswith('http:') or uri.startswith('https:') or os.path.isabs(uri):
636 self.uri = uri
637 else:
638 raise SafeException(_("Interface name '%s' doesn't start "
639 "with 'http:' or 'https:'") % uri)
640 self.reset()
642 def _get_feed_for(self):
643 retval = {}
644 for key in self._main_feed.feed_for:
645 retval[key] = True
646 return retval
647 feed_for = property(_get_feed_for) # Deprecated (used by 0publish)
649 def reset(self):
650 self.extra_feeds = []
651 self.stability_policy = None
653 def get_name(self):
654 from zeroinstall.injector.iface_cache import iface_cache
655 feed = iface_cache.get_feed(self.uri)
656 if feed:
657 return feed.get_name()
658 return '(' + os.path.basename(self.uri) + ')'
660 def __repr__(self):
661 return _("<Interface %s>") % self.uri
663 def set_stability_policy(self, new):
664 assert new is None or isinstance(new, Stability)
665 self.stability_policy = new
667 def get_feed(self, url):
668 #import warnings
669 #warnings.warn("use iface_cache.get_feed instead", DeprecationWarning, 2)
670 for x in self.extra_feeds:
671 if x.uri == url:
672 return x
673 #return self._main_feed.get_feed(url)
674 return None
676 def get_metadata(self, uri, name):
677 return self._main_feed.get_metadata(uri, name)
679 @property
680 def _main_feed(self):
681 #import warnings
682 #warnings.warn("use the feed instead", DeprecationWarning, 3)
683 from zeroinstall.injector import policy
684 iface_cache = policy.get_deprecated_singleton_config().iface_cache
685 feed = iface_cache.get_feed(self.uri)
686 if feed is None:
687 return _dummy_feed
688 return feed
690 def _merge_attrs(attrs, item):
691 """Add each attribute of item to a copy of attrs and return the copy.
692 @type attrs: {str: str}
693 @type item: L{qdom.Element}
694 @rtype: {str: str}
696 new = attrs.copy()
697 for a in item.attrs:
698 new[str(a)] = item.attrs[a]
699 return new
701 def _get_long(elem, attr_name):
702 val = elem.getAttribute(attr_name)
703 if val is not None:
704 try:
705 val = long(val)
706 except ValueError:
707 raise SafeException(_("Invalid value for integer attribute '%(attribute_name)s': %(value)s") % {'attribute_name': attr_name, 'value': val})
708 return val
710 class ZeroInstallFeed(object):
711 """A feed lists available implementations of an interface.
712 @ivar url: the URL for this feed
713 @ivar implementations: Implementations in this feed, indexed by ID
714 @type implementations: {str: L{Implementation}}
715 @ivar name: human-friendly name
716 @ivar summaries: short textual description (in various languages, since 0.49)
717 @type summaries: {str: str}
718 @ivar descriptions: long textual description (in various languages, since 0.49)
719 @type descriptions: {str: str}
720 @ivar last_modified: timestamp on signature
721 @ivar last_checked: time feed was last successfully downloaded and updated
722 @ivar feeds: list of <feed> elements in this feed
723 @type feeds: [L{Feed}]
724 @ivar feed_for: interfaces for which this could be a feed
725 @type feed_for: set(str)
726 @ivar metadata: extra elements we didn't understand
728 # _main is deprecated
729 __slots__ = ['url', 'implementations', 'name', 'descriptions', 'summaries', '_package_implementations',
730 'last_checked', 'last_modified', 'feeds', 'feed_for', 'metadata']
732 def __init__(self, feed_element, local_path = None, distro = None):
733 """Create a feed object from a DOM.
734 @param feed_element: the root element of a feed file
735 @type feed_element: L{qdom.Element}
736 @param local_path: the pathname of this local feed, or None for remote feeds"""
737 self.implementations = {}
738 self.name = None
739 self.summaries = {} # { lang: str }
740 self.descriptions = {} # { lang: str }
741 self.last_modified = None
742 self.feeds = []
743 self.feed_for = set()
744 self.metadata = []
745 self.last_checked = None
746 self._package_implementations = []
748 if distro is not None:
749 import warnings
750 warnings.warn("distro argument is now ignored", DeprecationWarning, 2)
752 if feed_element is None:
753 return # XXX subclass?
755 assert feed_element.name in ('interface', 'feed'), "Root element should be <interface>, not %s" % feed_element
756 assert feed_element.uri == XMLNS_IFACE, "Wrong namespace on root element: %s" % feed_element.uri
758 main = feed_element.getAttribute('main')
759 #if main: warn("Setting 'main' on the root element is deprecated. Put it on a <group> instead")
761 if local_path:
762 self.url = local_path
763 local_dir = os.path.dirname(local_path)
764 else:
765 self.url = feed_element.getAttribute('uri')
766 if not self.url:
767 raise InvalidInterface(_("<interface> uri attribute missing"))
768 local_dir = None # Can't have relative paths
770 min_injector_version = feed_element.getAttribute('min-injector-version')
771 if min_injector_version:
772 if parse_version(min_injector_version) > parse_version(version):
773 raise InvalidInterface(_("This feed requires version %(min_version)s or later of "
774 "Zero Install, but I am only version %(version)s. "
775 "You can get a newer version from http://0install.net") %
776 {'min_version': min_injector_version, 'version': version})
778 for x in feed_element.childNodes:
779 if x.uri != XMLNS_IFACE:
780 self.metadata.append(x)
781 continue
782 if x.name == 'name':
783 self.name = x.content
784 elif x.name == 'description':
785 self.descriptions[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", None)] = x.content
786 elif x.name == 'summary':
787 self.summaries[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", None)] = x.content
788 elif x.name == 'feed-for':
789 feed_iface = x.getAttribute('interface')
790 if not feed_iface:
791 raise InvalidInterface(_('Missing "interface" attribute in <feed-for>'))
792 self.feed_for.add(feed_iface)
793 # Bug report from a Debian/stable user that --feed gets the wrong value.
794 # Can't reproduce (even in a Debian/stable chroot), but add some logging here
795 # in case it happens again.
796 debug(_("Is feed-for %s"), feed_iface)
797 elif x.name == 'feed':
798 feed_src = x.getAttribute('src')
799 if not feed_src:
800 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
801 if feed_src.startswith('http:') or feed_src.startswith('https:') or local_path:
802 self.feeds.append(Feed(feed_src, x.getAttribute('arch'), False, langs = x.getAttribute('langs')))
803 else:
804 raise InvalidInterface(_("Invalid feed URL '%s'") % feed_src)
805 else:
806 self.metadata.append(x)
808 if not self.name:
809 raise InvalidInterface(_("Missing <name> in feed"))
810 if not self.summary:
811 raise InvalidInterface(_("Missing <summary> in feed"))
813 def process_group(group, group_attrs, base_depends, base_bindings, base_commands):
814 for item in group.childNodes:
815 if item.uri != XMLNS_IFACE: continue
817 if item.name not in ('group', 'implementation', 'package-implementation'):
818 continue
820 depends = base_depends[:]
821 bindings = base_bindings[:]
822 commands = base_commands.copy()
824 item_attrs = _merge_attrs(group_attrs, item)
826 # We've found a group or implementation. Scan for dependencies
827 # and bindings. Doing this here means that:
828 # - We can share the code for groups and implementations here.
829 # - The order doesn't matter, because these get processed first.
830 # A side-effect is that the document root cannot contain
831 # these.
832 for child in item.childNodes:
833 if child.uri != XMLNS_IFACE: continue
834 if child.name == 'requires':
835 dep = process_depends(child, local_dir)
836 depends.append(dep)
837 elif child.name == 'command':
838 command_name = child.attrs.get('name', None)
839 if not command_name:
840 raise InvalidInterface('Missing name for <command>')
841 commands[command_name] = Command(child, local_dir)
842 elif child.name in binding_names:
843 bindings.append(process_binding(child))
845 for attr, command in [('main', 'run'),
846 ('self-test', 'test')]:
847 value = item.attrs.get(attr, None)
848 if value is not None:
849 commands[command] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': value}), None)
851 compile_command = item.attrs.get('http://zero-install.sourceforge.net/2006/namespaces/0compile command')
852 if compile_command is not None:
853 commands['compile'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'shell-command': compile_command}), None)
855 if item.name == 'group':
856 process_group(item, item_attrs, depends, bindings, commands)
857 elif item.name == 'implementation':
858 process_impl(item, item_attrs, depends, bindings, commands)
859 elif item.name == 'package-implementation':
860 if depends:
861 warn("A <package-implementation> with dependencies in %s!", self.url)
862 self._package_implementations.append((item, item_attrs))
863 else:
864 assert 0
866 def process_impl(item, item_attrs, depends, bindings, commands):
867 id = item.getAttribute('id')
868 if id is None:
869 raise InvalidInterface(_("Missing 'id' attribute on %s") % item)
870 local_path = item_attrs.get('local-path')
871 if local_dir and local_path:
872 abs_local_path = os.path.abspath(os.path.join(local_dir, local_path))
873 impl = ZeroInstallImplementation(self, id, abs_local_path)
874 elif local_dir and (id.startswith('/') or id.startswith('.')):
875 # For old feeds
876 id = os.path.abspath(os.path.join(local_dir, id))
877 impl = ZeroInstallImplementation(self, id, id)
878 else:
879 impl = ZeroInstallImplementation(self, id, None)
880 if '=' in id:
881 # In older feeds, the ID was the (single) digest
882 impl.digests.append(id)
883 if id in self.implementations:
884 warn(_("Duplicate ID '%(id)s' in feed '%(feed)s'"), {'id': id, 'feed': self})
885 self.implementations[id] = impl
887 impl.metadata = item_attrs
888 try:
889 version_mod = item_attrs.get('version-modifier', None)
890 if version_mod:
891 item_attrs['version'] += version_mod
892 del item_attrs['version-modifier']
893 version = item_attrs['version']
894 except KeyError:
895 raise InvalidInterface(_("Missing version attribute"))
896 impl.version = parse_version(version)
898 impl.commands = commands
900 impl.released = item_attrs.get('released', None)
901 impl.langs = item_attrs.get('langs', '')
903 size = item.getAttribute('size')
904 if size:
905 impl.size = long(size)
906 impl.arch = item_attrs.get('arch', None)
907 try:
908 stability = stability_levels[str(item_attrs['stability'])]
909 except KeyError:
910 stab = str(item_attrs['stability'])
911 if stab != stab.lower():
912 raise InvalidInterface(_('Stability "%s" invalid - use lower case!') % item_attrs.stability)
913 raise InvalidInterface(_('Stability "%s" invalid') % item_attrs['stability'])
914 if stability >= preferred:
915 raise InvalidInterface(_("Upstream can't set stability to preferred!"))
916 impl.upstream_stability = stability
918 impl.bindings = bindings
919 impl.requires = depends
921 for elem in item.childNodes:
922 if elem.uri != XMLNS_IFACE: continue
923 if elem.name == 'archive':
924 url = elem.getAttribute('href')
925 if not url:
926 raise InvalidInterface(_("Missing href attribute on <archive>"))
927 size = elem.getAttribute('size')
928 if not size:
929 raise InvalidInterface(_("Missing size attribute on <archive>"))
930 impl.add_download_source(url = url, size = long(size),
931 extract = elem.getAttribute('extract'),
932 start_offset = _get_long(elem, 'start-offset'),
933 type = elem.getAttribute('type'))
934 elif elem.name == 'manifest-digest':
935 for aname, avalue in elem.attrs.iteritems():
936 if ' ' not in aname:
937 impl.digests.append('%s=%s' % (aname, avalue))
938 elif elem.name == 'recipe':
939 recipe = Recipe()
940 for recipe_step in elem.childNodes:
941 if recipe_step.uri == XMLNS_IFACE and recipe_step.name == 'archive':
942 url = recipe_step.getAttribute('href')
943 if not url:
944 raise InvalidInterface(_("Missing href attribute on <archive>"))
945 size = recipe_step.getAttribute('size')
946 if not size:
947 raise InvalidInterface(_("Missing size attribute on <archive>"))
948 recipe.steps.append(DownloadSource(None, url = url, size = long(size),
949 extract = recipe_step.getAttribute('extract'),
950 start_offset = _get_long(recipe_step, 'start-offset'),
951 type = recipe_step.getAttribute('type')))
952 else:
953 info(_("Unknown step '%s' in recipe; skipping recipe"), recipe_step.name)
954 break
955 else:
956 impl.download_sources.append(recipe)
958 root_attrs = {'stability': 'testing'}
959 root_commands = {}
960 if main:
961 info("Note: @main on document element is deprecated in %s", self)
962 root_commands['run'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': main}), None)
963 process_group(feed_element, root_attrs, [], [], root_commands)
965 def get_distro_feed(self):
966 """Does this feed contain any <pacakge-implementation> elements?
967 i.e. is it worth asking the package manager for more information?
968 @return: the URL of the virtual feed, or None
969 @since: 0.49"""
970 if self._package_implementations:
971 return "distribution:" + self.url
972 return None
974 def get_package_impls(self, distro):
975 """Find the best <pacakge-implementation> element(s) for the given distribution.
976 @param distro: the distribution to use to rate them
977 @type distro: L{distro.Distribution}
978 @return: a list of tuples for the best ranked elements
979 @rtype: [str]
980 @since: 0.49"""
981 best_score = 0
982 best_impls = []
984 for item, item_attrs in self._package_implementations:
985 distro_names = item_attrs.get('distributions', '')
986 for distro_name in distro_names.split(' '):
987 score = distro.get_score(distro_name)
988 if score > best_score:
989 best_score = score
990 best_impls = []
991 if score == best_score:
992 best_impls.append((item, item_attrs))
993 return best_impls
995 def get_name(self):
996 return self.name or '(' + os.path.basename(self.url) + ')'
998 def __repr__(self):
999 return _("<Feed %s>") % self.url
1001 """@deprecated"""
1002 def _get_impl(self, id):
1003 assert id not in self.implementations
1005 if id.startswith('.') or id.startswith('/'):
1006 id = os.path.abspath(os.path.join(self.url, id))
1007 local_path = id
1008 impl = ZeroInstallImplementation(self, id, local_path)
1009 else:
1010 impl = ZeroInstallImplementation(self, id, None)
1011 impl.digests.append(id)
1013 self.implementations[id] = impl
1014 return impl
1016 def set_stability_policy(self, new):
1017 assert new is None or isinstance(new, Stability)
1018 self.stability_policy = new
1020 def get_feed(self, url):
1021 for x in self.feeds:
1022 if x.uri == url:
1023 return x
1024 return None
1026 def add_metadata(self, elem):
1027 self.metadata.append(elem)
1029 def get_metadata(self, uri, name):
1030 """Return a list of interface metadata elements with this name and namespace URI."""
1031 return [m for m in self.metadata if m.name == name and m.uri == uri]
1033 @property
1034 def summary(self):
1035 return _best_language_match(self.summaries)
1037 @property
1038 def description(self):
1039 return _best_language_match(self.descriptions)
1041 class DummyFeed(object):
1042 """Temporary class used during API transition."""
1043 last_modified = None
1044 name = '-'
1045 last_checked = property(lambda self: None)
1046 implementations = property(lambda self: {})
1047 feeds = property(lambda self: [])
1048 summary = property(lambda self: '-')
1049 description = property(lambda self: '')
1050 def get_name(self): return self.name
1051 def get_feed(self, url): return None
1052 def get_metadata(self, uri, name): return []
1053 _dummy_feed = DummyFeed()
1055 def unescape(uri):
1056 """Convert each %20 to a space, etc.
1057 @rtype: str"""
1058 uri = uri.replace('#', '/')
1059 if '%' not in uri: return uri
1060 return re.sub('%[0-9a-fA-F][0-9a-fA-F]',
1061 lambda match: chr(int(match.group(0)[1:], 16)),
1062 uri).decode('utf-8')
1064 def escape(uri):
1065 """Convert each space to %20, etc
1066 @rtype: str"""
1067 return re.sub('[^-_.a-zA-Z0-9]',
1068 lambda match: '%%%02x' % ord(match.group(0)),
1069 uri.encode('utf-8'))
1071 def _pretty_escape(uri):
1072 """Convert each space to %20, etc
1073 : is preserved and / becomes #. This makes for nicer strings,
1074 and may replace L{escape} everywhere in future.
1075 @rtype: str"""
1076 return re.sub('[^-_.a-zA-Z0-9:/]',
1077 lambda match: '%%%02x' % ord(match.group(0)),
1078 uri.encode('utf-8')).replace('/', '#')
1080 def canonical_iface_uri(uri):
1081 """If uri is a relative path, convert to an absolute one.
1082 A "file:///foo" URI is converted to "/foo".
1083 An "alias:prog" URI expands to the URI in the 0alias script
1084 Otherwise, return it unmodified.
1085 @rtype: str
1086 @raise SafeException: if uri isn't valid
1088 if uri.startswith('http://') or uri.startswith('https://'):
1089 if uri.count("/") < 3:
1090 raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
1091 return uri
1092 elif uri.startswith('file:///'):
1093 return uri[7:]
1094 elif uri.startswith('alias:'):
1095 from zeroinstall import alias, support
1096 alias_prog = uri[6:]
1097 if not os.path.isabs(alias_prog):
1098 full_path = support.find_in_path(alias_prog)
1099 if not full_path:
1100 raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
1101 else:
1102 full_path = alias_prog
1103 interface_uri, main = alias.parse_script(full_path)
1104 return interface_uri
1105 else:
1106 iface_uri = os.path.realpath(uri)
1107 if os.path.isfile(iface_uri):
1108 return iface_uri
1109 raise SafeException(_("Bad interface name '%(uri)s'.\n"
1110 "(doesn't start with 'http:', and "
1111 "doesn't exist as a local file '%(interface_uri)s' either)") %
1112 {'uri': uri, 'interface_uri': iface_uri})
1114 _version_mod_to_value = {
1115 'pre': -2,
1116 'rc': -1,
1117 '': 0,
1118 'post': 1,
1121 # Reverse mapping
1122 _version_value_to_mod = {}
1123 for x in _version_mod_to_value: _version_value_to_mod[_version_mod_to_value[x]] = x
1124 del x
1126 _version_re = re.compile('-([a-z]*)')
1128 def parse_version(version_string):
1129 """Convert a version string to an internal representation.
1130 The parsed format can be compared quickly using the standard Python functions.
1131 - Version := DottedList ("-" Mod DottedList?)*
1132 - DottedList := (Integer ("." Integer)*)
1133 @rtype: tuple (opaque)
1134 @raise SafeException: if the string isn't a valid version
1135 @since: 0.24 (moved from L{reader}, from where it is still available):"""
1136 if version_string is None: return None
1137 parts = _version_re.split(version_string)
1138 if parts[-1] == '':
1139 del parts[-1] # Ends with a modifier
1140 else:
1141 parts.append('')
1142 if not parts:
1143 raise SafeException(_("Empty version string!"))
1144 l = len(parts)
1145 try:
1146 for x in range(0, l, 2):
1147 part = parts[x]
1148 if part:
1149 parts[x] = map(int, parts[x].split('.'))
1150 else:
1151 parts[x] = [] # (because ''.split('.') == [''], not [])
1152 for x in range(1, l, 2):
1153 parts[x] = _version_mod_to_value[parts[x]]
1154 return parts
1155 except ValueError, ex:
1156 raise SafeException(_("Invalid version format in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1157 except KeyError, ex:
1158 raise SafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1160 def format_version(version):
1161 """Format a parsed version for display. Undoes the effect of L{parse_version}.
1162 @see: L{Implementation.get_version}
1163 @rtype: str
1164 @since: 0.24"""
1165 version = version[:]
1166 l = len(version)
1167 for x in range(0, l, 2):
1168 version[x] = '.'.join(map(str, version[x]))
1169 for x in range(1, l, 2):
1170 version[x] = '-' + _version_value_to_mod[version[x]]
1171 if version[-1] == '-': del version[-1]
1172 return ''.join(version)