Added support for <command> element
[zeroinstall.git] / zeroinstall / injector / model.py
blobd9bbba124d076f2727f5cb2b818f7f4e12a10ed9
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 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 mode = mode)
119 if not binding.name: raise InvalidInterface(_("Missing 'name' in binding"))
120 if binding.insert is None: raise InvalidInterface(_("Missing 'insert' in binding"))
121 return binding
122 elif e.name == 'overlay':
123 return OverlayBinding(e.getAttribute('src'), e.getAttribute('mount-point'))
124 else:
125 raise Exception(_("Unknown binding type '%s'") % e.name)
127 def process_depends(item):
128 """Internal"""
129 # Note: also called from selections
130 dep_iface = item.getAttribute('interface')
131 if not dep_iface:
132 raise InvalidInterface(_("Missing 'interface' on <requires>"))
133 dependency = InterfaceDependency(dep_iface, metadata = item.attrs)
135 for e in item.childNodes:
136 if e.uri != XMLNS_IFACE: continue
137 if e.name in binding_names:
138 dependency.bindings.append(process_binding(e))
139 elif e.name == 'version':
140 dependency.restrictions.append(
141 VersionRangeRestriction(not_before = parse_version(e.getAttribute('not-before')),
142 before = parse_version(e.getAttribute('before'))))
143 return dependency
145 def N_(message): return message
147 insecure = Stability(0, N_('insecure'), _('This is a security risk'))
148 buggy = Stability(5, N_('buggy'), _('Known to have serious bugs'))
149 developer = Stability(10, N_('developer'), _('Work-in-progress - bugs likely'))
150 testing = Stability(20, N_('testing'), _('Stability unknown - please test!'))
151 stable = Stability(30, N_('stable'), _('Tested - no serious problems found'))
152 packaged = Stability(35, N_('packaged'), _('Supplied by the local package manager'))
153 preferred = Stability(40, N_('preferred'), _('Best of all - must be set manually'))
155 del N_
157 class Restriction(object):
158 """A Restriction limits the allowed implementations of an Interface."""
159 __slots__ = []
161 def meets_restriction(self, impl):
162 """Called by the L{solver.Solver} to check whether a particular implementation is acceptable.
163 @return: False if this implementation is not a possibility
164 @rtype: bool
166 raise NotImplementedError(_("Abstract"))
168 class VersionRestriction(Restriction):
169 """Only select implementations with a particular version number.
170 @since: 0.40"""
172 def __init__(self, version):
173 """@param version: the required version number
174 @see: L{parse_version}; use this to pre-process the version number
176 self.version = version
178 def meets_restriction(self, impl):
179 return impl.version == self.version
181 def __str__(self):
182 return _("(restriction: version = %s)") % format_version(self.version)
184 class VersionRangeRestriction(Restriction):
185 """Only versions within the given range are acceptable"""
186 __slots__ = ['before', 'not_before']
188 def __init__(self, before, not_before):
189 """@param before: chosen versions must be earlier than this
190 @param not_before: versions must be at least this high
191 @see: L{parse_version}; use this to pre-process the versions
193 self.before = before
194 self.not_before = not_before
196 def meets_restriction(self, impl):
197 if self.not_before and impl.version < self.not_before:
198 return False
199 if self.before and impl.version >= self.before:
200 return False
201 return True
203 def __str__(self):
204 if self.not_before is not None or self.before is not None:
205 range = ''
206 if self.not_before is not None:
207 range += format_version(self.not_before) + ' <= '
208 range += 'version'
209 if self.before is not None:
210 range += ' < ' + format_version(self.before)
211 else:
212 range = 'none'
213 return _("(restriction: %s)") % range
215 class Binding(object):
216 """Information about how the choice of a Dependency is made known
217 to the application being run."""
219 class EnvironmentBinding(Binding):
220 """Indicate the chosen implementation using an environment variable."""
221 __slots__ = ['name', 'insert', 'default', 'mode']
223 PREPEND = 'prepend'
224 APPEND = 'append'
225 REPLACE = 'replace'
227 def __init__(self, name, insert, default = None, mode = PREPEND):
228 """mode argument added in version 0.28"""
229 self.name = name
230 self.insert = insert
231 self.default = default
232 self.mode = mode
234 def __str__(self):
235 return _("<environ %(name)s %(mode)s %(insert)s>") % {'name': self.name,'mode': self.mode, 'insert': self.insert}
237 __repr__ = __str__
239 def get_value(self, path, old_value):
240 """Calculate the new value of the environment variable after applying this binding.
241 @param path: the path to the selected implementation
242 @param old_value: the current value of the environment variable
243 @return: the new value for the environment variable"""
244 extra = os.path.join(path, self.insert)
246 if self.mode == EnvironmentBinding.REPLACE:
247 return extra
249 if old_value is None:
250 old_value = self.default or defaults.get(self.name, None)
251 if old_value is None:
252 return extra
253 if self.mode == EnvironmentBinding.PREPEND:
254 return extra + os.pathsep + old_value
255 else:
256 return old_value + os.pathsep + extra
258 def _toxml(self, doc):
259 """Create a DOM element for this binding.
260 @param doc: document to use to create the element
261 @return: the new element
263 env_elem = doc.createElementNS(XMLNS_IFACE, 'environment')
264 env_elem.setAttributeNS(None, 'name', self.name)
265 env_elem.setAttributeNS(None, 'insert', self.insert)
266 if self.default:
267 env_elem.setAttributeNS(None, 'default', self.default)
268 return env_elem
270 class OverlayBinding(Binding):
271 """Make the chosen implementation available by overlaying it onto another part of the file-system.
272 This is to support legacy programs which use hard-coded paths."""
273 __slots__ = ['src', 'mount_point']
275 def __init__(self, src, mount_point):
276 self.src = src
277 self.mount_point = mount_point
279 def __str__(self):
280 return _("<overlay %(src)s on %(mount_point)s>") % {'src': self.src or '.', 'mount_point': self.mount_point or '/'}
282 __repr__ = __str__
284 def _toxml(self, doc):
285 """Create a DOM element for this binding.
286 @param doc: document to use to create the element
287 @return: the new element
289 env_elem = doc.createElementNS(XMLNS_IFACE, 'overlay')
290 if self.src is not None:
291 env_elem.setAttributeNS(None, 'src', self.src)
292 if self.mount_point is not None:
293 env_elem.setAttributeNS(None, 'mount-point', self.mount_point)
294 return env_elem
296 class Feed(object):
297 """An interface's feeds are other interfaces whose implementations can also be
298 used as implementations of this interface."""
299 __slots__ = ['uri', 'os', 'machine', 'user_override', 'langs']
300 def __init__(self, uri, arch, user_override, langs = None):
301 self.uri = uri
302 # This indicates whether the feed comes from the user's overrides
303 # file. If true, writer.py will write it when saving.
304 self.user_override = user_override
305 self.os, self.machine = _split_arch(arch)
306 self.langs = langs
308 def __str__(self):
309 return "<Feed from %s>" % self.uri
310 __repr__ = __str__
312 arch = property(lambda self: _join_arch(self.os, self.machine))
314 class Dependency(object):
315 """A Dependency indicates that an Implementation requires some additional
316 code to function. This is an abstract base class.
317 @ivar metadata: any extra attributes from the XML element
318 @type metadata: {str: str}
320 __slots__ = ['metadata']
322 def __init__(self, metadata):
323 if metadata is None:
324 metadata = {}
325 else:
326 assert not isinstance(metadata, basestring) # Use InterfaceDependency instead!
327 self.metadata = metadata
329 class InterfaceDependency(Dependency):
330 """A Dependency on a Zero Install interface.
331 @ivar interface: the interface required by this dependency
332 @type interface: str
333 @ivar restrictions: a list of constraints on acceptable implementations
334 @type restrictions: [L{Restriction}]
335 @ivar bindings: how to make the choice of implementation known
336 @type bindings: [L{Binding}]
337 @since: 0.28
339 __slots__ = ['interface', 'restrictions', 'bindings', 'metadata']
341 def __init__(self, interface, restrictions = None, metadata = None):
342 Dependency.__init__(self, metadata)
343 assert isinstance(interface, (str, unicode))
344 assert interface
345 self.interface = interface
346 if restrictions is None:
347 self.restrictions = []
348 else:
349 self.restrictions = restrictions
350 self.bindings = []
352 def __str__(self):
353 return _("<Dependency on %(interface)s; bindings: %(bindings)s%(restrictions)s>") % {'interface': self.interface, 'bindings': self.bindings, 'restrictions': self.restrictions}
355 class RetrievalMethod(object):
356 """A RetrievalMethod provides a way to fetch an implementation."""
357 __slots__ = []
359 class DownloadSource(RetrievalMethod):
360 """A DownloadSource provides a way to fetch an implementation."""
361 __slots__ = ['implementation', 'url', 'size', 'extract', 'start_offset', 'type']
363 def __init__(self, implementation, url, size, extract, start_offset = 0, type = None):
364 self.implementation = implementation
365 self.url = url
366 self.size = size
367 self.extract = extract
368 self.start_offset = start_offset
369 self.type = type # MIME type - see unpack.py
371 class Recipe(RetrievalMethod):
372 """Get an implementation by following a series of steps.
373 @ivar size: the combined download sizes from all the steps
374 @type size: int
375 @ivar steps: the sequence of steps which must be performed
376 @type steps: [L{RetrievalMethod}]"""
377 __slots__ = ['steps']
379 def __init__(self):
380 self.steps = []
382 size = property(lambda self: sum([x.size for x in self.steps]))
384 class DistributionSource(RetrievalMethod):
385 """A package that is installed using the distribution's tools (including PackageKit).
386 @ivar install: a function to call to install this package
387 @type install: (L{handler.Handler}) -> L{tasks.Blocker}
388 @ivar package_id: the package name, in a form recognised by the distribution's tools
389 @type package_id: str
390 @ivar size: the download size in bytes
391 @type size: int
392 @ivar needs_confirmation: whether the user should be asked to confirm before calling install()
393 @type needs_confirmation: bool"""
395 __slots__ = ['package_id', 'size', 'install', 'needs_confirmation']
397 def __init__(self, package_id, size, install, needs_confirmation = True):
398 RetrievalMethod.__init__(self)
399 self.package_id = package_id
400 self.size = size
401 self.install = install
402 self.needs_confirmation = needs_confirmation
404 class Command(object):
405 """A Command is a way of running an Implementation as a program."""
407 __slots__ = ['qdom', '_depends']
409 def __init__(self, qdom):
410 assert qdom.name == 'command', 'not <command>: %s' % qdom
411 self.qdom = qdom
412 self._depends = None
414 path = property(lambda self: self.qdom.attrs.get("path", None))
416 def _toxml(self, doc, prefixes):
417 return self.qdom.toDOM(doc, prefixes)
419 @property
420 def requires(self):
421 if self._depends is None:
422 depends = []
423 for child in self.qdom.childNodes:
424 if child.name == 'requires':
425 dep = process_depends(child)
426 depends.append(dep)
427 self._depends = depends
428 return self._depends
430 class Implementation(object):
431 """An Implementation is a package which implements an Interface.
432 @ivar download_sources: list of methods of getting this implementation
433 @type download_sources: [L{RetrievalMethod}]
434 @ivar feed: the feed owning this implementation (since 0.32)
435 @type feed: [L{ZeroInstallFeed}]
436 @ivar bindings: how to tell this component where it itself is located (since 0.31)
437 @type bindings: [Binding]
438 @ivar upstream_stability: the stability reported by the packager
439 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged]
440 @ivar user_stability: the stability as set by the user
441 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged | preferred]
442 @ivar langs: natural languages supported by this package
443 @type langs: str
444 @ivar requires: interfaces this package depends on
445 @type requires: [L{Dependency}]
446 @ivar commands: ways to execute as a program
447 @type commands: {str: Command}
448 @ivar metadata: extra metadata from the feed
449 @type metadata: {"[URI ]localName": str}
450 @ivar id: a unique identifier for this Implementation
451 @ivar version: a parsed version number
452 @ivar released: release date
453 @ivar local_path: the directory containing this local implementation, or None if it isn't local (id isn't a path)
454 @type local_path: str | None
455 @ivar requires_root_install: whether the user will need admin rights to use this
456 @type requires_root_install: bool
459 # Note: user_stability shouldn't really be here
461 __slots__ = ['upstream_stability', 'user_stability', 'langs',
462 'requires', 'metadata', 'download_sources', 'commands',
463 'id', 'feed', 'version', 'released', 'bindings', 'machine']
465 def __init__(self, feed, id):
466 assert id
467 self.feed = feed
468 self.id = id
469 self.user_stability = None
470 self.upstream_stability = None
471 self.metadata = {} # [URI + " "] + localName -> value
472 self.requires = []
473 self.version = None
474 self.released = None
475 self.download_sources = []
476 self.langs = ""
477 self.machine = None
478 self.bindings = []
479 self.commands = {}
481 def get_stability(self):
482 return self.user_stability or self.upstream_stability or testing
484 def __str__(self):
485 return self.id
487 def __repr__(self):
488 return "v%s (%s)" % (self.get_version(), self.id)
490 def __cmp__(self, other):
491 """Newer versions come first"""
492 d = cmp(other.version, self.version)
493 if d: return d
494 # If the version number is the same, just give a stable sort order, and
495 # ensure that two different implementations don't compare equal.
496 d = cmp(other.feed.url, self.feed.url)
497 if d: return d
498 return cmp(other.id, self.id)
500 def get_version(self):
501 """Return the version as a string.
502 @see: L{format_version}
504 return format_version(self.version)
506 arch = property(lambda self: _join_arch(self.os, self.machine))
508 os = None
509 local_path = None
510 digests = None
511 requires_root_install = False
513 def _get_main(self):
514 """"@deprecated: use commands["run"] instead"""
515 main = self.commands.get("run", None)
516 if main is not None:
517 return main.path
518 return None
519 def _set_main(self, path):
520 """"@deprecated: use commands["run"] instead"""
521 if path is None:
522 if "run" in self.commands:
523 del self.commands["run"]
524 else:
525 self.commands["run"] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': path}))
526 main = property(_get_main, _set_main)
528 class DistributionImplementation(Implementation):
529 """An implementation provided by the distribution. Information such as the version
530 comes from the package manager.
531 @since: 0.28"""
532 __slots__ = ['distro', 'installed']
534 def __init__(self, feed, id, distro):
535 assert id.startswith('package:')
536 Implementation.__init__(self, feed, id)
537 self.distro = distro
538 self.installed = False
540 @property
541 def requires_root_install(self):
542 return not self.installed
544 class ZeroInstallImplementation(Implementation):
545 """An implementation where all the information comes from Zero Install.
546 @ivar digests: a list of "algorith=value" strings (since 0.45)
547 @type digests: [str]
548 @since: 0.28"""
549 __slots__ = ['os', 'size', 'digests', 'local_path']
551 def __init__(self, feed, id, local_path):
552 """id can be a local path (string starting with /) or a manifest hash (eg "sha1=XXX")"""
553 assert not id.startswith('package:'), id
554 Implementation.__init__(self, feed, id)
555 self.size = None
556 self.os = None
557 self.digests = []
558 self.local_path = local_path
560 # Deprecated
561 dependencies = property(lambda self: dict([(x.interface, x) for x in self.requires
562 if isinstance(x, InterfaceDependency)]))
564 def add_download_source(self, url, size, extract, start_offset = 0, type = None):
565 """Add a download source."""
566 self.download_sources.append(DownloadSource(self, url, size, extract, start_offset, type))
568 def set_arch(self, arch):
569 self.os, self.machine = _split_arch(arch)
570 arch = property(lambda self: _join_arch(self.os, self.machine), set_arch)
572 class Interface(object):
573 """An Interface represents some contract of behaviour.
574 @ivar uri: the URI for this interface.
575 @ivar stability_policy: user's configured policy.
576 Implementations at this level or higher are preferred.
577 Lower levels are used only if there is no other choice.
579 __slots__ = ['uri', 'stability_policy', 'extra_feeds']
581 implementations = property(lambda self: self._main_feed.implementations)
582 name = property(lambda self: self._main_feed.name)
583 description = property(lambda self: self._main_feed.description)
584 summary = property(lambda self: self._main_feed.summary)
585 last_modified = property(lambda self: self._main_feed.last_modified)
586 feeds = property(lambda self: self.extra_feeds + self._main_feed.feeds)
587 metadata = property(lambda self: self._main_feed.metadata)
589 last_checked = property(lambda self: self._main_feed.last_checked)
591 def __init__(self, uri):
592 assert uri
593 if uri.startswith('http:') or uri.startswith('https:') or os.path.isabs(uri):
594 self.uri = uri
595 else:
596 raise SafeException(_("Interface name '%s' doesn't start "
597 "with 'http:' or 'https:'") % uri)
598 self.reset()
600 def _get_feed_for(self):
601 retval = {}
602 for key in self._main_feed.feed_for:
603 retval[key] = True
604 return retval
605 feed_for = property(_get_feed_for) # Deprecated (used by 0publish)
607 def reset(self):
608 self.extra_feeds = []
609 self.stability_policy = None
611 def get_name(self):
612 from zeroinstall.injector.iface_cache import iface_cache
613 feed = iface_cache.get_feed(self.uri)
614 if feed:
615 return feed.get_name()
616 return '(' + os.path.basename(self.uri) + ')'
618 def __repr__(self):
619 return _("<Interface %s>") % self.uri
621 def set_stability_policy(self, new):
622 assert new is None or isinstance(new, Stability)
623 self.stability_policy = new
625 def get_feed(self, url):
626 #import warnings
627 #warnings.warn("use iface_cache.get_feed instead", DeprecationWarning, 2)
628 for x in self.extra_feeds:
629 if x.uri == url:
630 return x
631 #return self._main_feed.get_feed(url)
632 return None
634 def get_metadata(self, uri, name):
635 return self._main_feed.get_metadata(uri, name)
637 @property
638 def _main_feed(self):
639 #import warnings
640 #warnings.warn("use the feed instead", DeprecationWarning, 3)
641 from zeroinstall.injector.iface_cache import iface_cache
642 feed = iface_cache.get_feed(self.uri)
643 if feed is None:
644 return _dummy_feed
645 return feed
647 def _merge_attrs(attrs, item):
648 """Add each attribute of item to a copy of attrs and return the copy.
649 @type attrs: {str: str}
650 @type item: L{qdom.Element}
651 @rtype: {str: str}
653 new = attrs.copy()
654 for a in item.attrs:
655 new[str(a)] = item.attrs[a]
656 return new
658 def _get_long(elem, attr_name):
659 val = elem.getAttribute(attr_name)
660 if val is not None:
661 try:
662 val = long(val)
663 except ValueError, ex:
664 raise SafeException(_("Invalid value for integer attribute '%(attribute_name)s': %(value)s") % {'attribute_name': attr_name, 'value': val})
665 return val
667 class ZeroInstallFeed(object):
668 """A feed lists available implementations of an interface.
669 @ivar url: the URL for this feed
670 @ivar implementations: Implementations in this feed, indexed by ID
671 @type implementations: {str: L{Implementation}}
672 @ivar name: human-friendly name
673 @ivar summaries: short textual description (in various languages, since 0.49)
674 @type summaries: {str: str}
675 @ivar descriptions: long textual description (in various languages, since 0.49)
676 @type descriptions: {str: str}
677 @ivar last_modified: timestamp on signature
678 @ivar last_checked: time feed was last successfully downloaded and updated
679 @ivar feeds: list of <feed> elements in this feed
680 @type feeds: [L{Feed}]
681 @ivar feed_for: interfaces for which this could be a feed
682 @type feed_for: set(str)
683 @ivar metadata: extra elements we didn't understand
685 # _main is deprecated
686 __slots__ = ['url', 'implementations', 'name', 'descriptions', 'summaries', '_package_implementations',
687 'last_checked', 'last_modified', 'feeds', 'feed_for', 'metadata']
689 def __init__(self, feed_element, local_path = None, distro = None):
690 """Create a feed object from a DOM.
691 @param feed_element: the root element of a feed file
692 @type feed_element: L{qdom.Element}
693 @param local_path: the pathname of this local feed, or None for remote feeds"""
694 self.implementations = {}
695 self.name = None
696 self.summaries = {} # { lang: str }
697 self.descriptions = {} # { lang: str }
698 self.last_modified = None
699 self.feeds = []
700 self.feed_for = set()
701 self.metadata = []
702 self.last_checked = None
703 self._package_implementations = []
705 if distro is not None:
706 import warnings
707 warnings.warn("distro argument is now ignored", DeprecationWarning, 2)
709 if feed_element is None:
710 return # XXX subclass?
712 assert feed_element.name in ('interface', 'feed'), "Root element should be <interface>, not %s" % feed_element
713 assert feed_element.uri == XMLNS_IFACE, "Wrong namespace on root element: %s" % feed_element.uri
715 main = feed_element.getAttribute('main')
716 #if main: warn("Setting 'main' on the root element is deprecated. Put it on a <group> instead")
718 if local_path:
719 self.url = local_path
720 local_dir = os.path.dirname(local_path)
721 else:
722 self.url = feed_element.getAttribute('uri')
723 if not self.url:
724 raise InvalidInterface(_("<interface> uri attribute missing"))
725 local_dir = None # Can't have relative paths
727 min_injector_version = feed_element.getAttribute('min-injector-version')
728 if min_injector_version:
729 if parse_version(min_injector_version) > parse_version(version):
730 raise InvalidInterface(_("This feed requires version %(min_version)s or later of "
731 "Zero Install, but I am only version %(version)s. "
732 "You can get a newer version from http://0install.net") %
733 {'min_version': min_injector_version, 'version': version})
735 for x in feed_element.childNodes:
736 if x.uri != XMLNS_IFACE:
737 self.metadata.append(x)
738 continue
739 if x.name == 'name':
740 self.name = x.content
741 elif x.name == 'description':
742 self.descriptions[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", None)] = x.content
743 elif x.name == 'summary':
744 self.summaries[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", None)] = x.content
745 elif x.name == 'feed-for':
746 feed_iface = x.getAttribute('interface')
747 if not feed_iface:
748 raise InvalidInterface(_('Missing "interface" attribute in <feed-for>'))
749 self.feed_for.add(feed_iface)
750 # Bug report from a Debian/stable user that --feed gets the wrong value.
751 # Can't reproduce (even in a Debian/stable chroot), but add some logging here
752 # in case it happens again.
753 debug(_("Is feed-for %s"), feed_iface)
754 elif x.name == 'feed':
755 feed_src = x.getAttribute('src')
756 if not feed_src:
757 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
758 if feed_src.startswith('http:') or feed_src.startswith('https:') or local_path:
759 self.feeds.append(Feed(feed_src, x.getAttribute('arch'), False, langs = x.getAttribute('langs')))
760 else:
761 raise InvalidInterface(_("Invalid feed URL '%s'") % feed_src)
762 else:
763 self.metadata.append(x)
765 if not self.name:
766 raise InvalidInterface(_("Missing <name> in feed"))
767 if not self.summary:
768 raise InvalidInterface(_("Missing <summary> in feed"))
770 def process_group(group, group_attrs, base_depends, base_bindings, base_commands):
771 for item in group.childNodes:
772 if item.uri != XMLNS_IFACE: continue
774 if item.name not in ('group', 'implementation', 'package-implementation'):
775 continue
777 depends = base_depends[:]
778 bindings = base_bindings[:]
779 commands = base_commands.copy()
781 item_attrs = _merge_attrs(group_attrs, item)
783 # We've found a group or implementation. Scan for dependencies
784 # and bindings. Doing this here means that:
785 # - We can share the code for groups and implementations here.
786 # - The order doesn't matter, because these get processed first.
787 # A side-effect is that the document root cannot contain
788 # these.
789 for child in item.childNodes:
790 if child.uri != XMLNS_IFACE: continue
791 if child.name == 'requires':
792 dep = process_depends(child)
793 depends.append(dep)
794 elif child.name == 'command':
795 command_name = child.attrs.get('name', None)
796 if not command_name:
797 raise InvalidInterface('Missing name for <command>')
798 commands[command_name] = Command(child)
799 elif child.name in binding_names:
800 bindings.append(process_binding(child))
802 for attr, command in [('main', 'run'),
803 ('self-test', 'test')]:
804 value = item.attrs.get(attr, None)
805 if value is not None:
806 commands[command] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': value}))
808 compile_command = item.attrs.get('http://zero-install.sourceforge.net/2006/namespaces/0compile command')
809 if compile_command is not None:
810 commands['compile'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'shell-command': compile_command}))
812 if item.name == 'group':
813 process_group(item, item_attrs, depends, bindings, commands)
814 elif item.name == 'implementation':
815 process_impl(item, item_attrs, depends, bindings, commands)
816 elif item.name == 'package-implementation':
817 if depends:
818 warn("A <package-implementation> with dependencies in %s!", self.url)
819 self._package_implementations.append((item, item_attrs))
820 else:
821 assert 0
823 def process_impl(item, item_attrs, depends, bindings, commands):
824 id = item.getAttribute('id')
825 if id is None:
826 raise InvalidInterface(_("Missing 'id' attribute on %s") % item)
827 local_path = item_attrs.get('local-path')
828 if local_dir and local_path:
829 abs_local_path = os.path.abspath(os.path.join(local_dir, local_path))
830 impl = ZeroInstallImplementation(self, id, abs_local_path)
831 elif local_dir and (id.startswith('/') or id.startswith('.')):
832 # For old feeds
833 id = os.path.abspath(os.path.join(local_dir, id))
834 impl = ZeroInstallImplementation(self, id, id)
835 else:
836 impl = ZeroInstallImplementation(self, id, None)
837 if '=' in id:
838 # In older feeds, the ID was the (single) digest
839 impl.digests.append(id)
840 if id in self.implementations:
841 warn(_("Duplicate ID '%(id)s' in feed '%(feed)s'"), {'id': id, 'feed': self})
842 self.implementations[id] = impl
844 impl.metadata = item_attrs
845 try:
846 version_mod = item_attrs.get('version-modifier', None)
847 if version_mod:
848 item_attrs['version'] += version_mod
849 del item_attrs['version-modifier']
850 version = item_attrs['version']
851 except KeyError:
852 raise InvalidInterface(_("Missing version attribute"))
853 impl.version = parse_version(version)
855 item_main = commands.get('run', None)
856 impl.commands = commands
858 impl.released = item_attrs.get('released', None)
859 impl.langs = item_attrs.get('langs', '')
861 size = item.getAttribute('size')
862 if size:
863 impl.size = long(size)
864 impl.arch = item_attrs.get('arch', None)
865 try:
866 stability = stability_levels[str(item_attrs['stability'])]
867 except KeyError:
868 stab = str(item_attrs['stability'])
869 if stab != stab.lower():
870 raise InvalidInterface(_('Stability "%s" invalid - use lower case!') % item_attrs.stability)
871 raise InvalidInterface(_('Stability "%s" invalid') % item_attrs['stability'])
872 if stability >= preferred:
873 raise InvalidInterface(_("Upstream can't set stability to preferred!"))
874 impl.upstream_stability = stability
876 impl.bindings = bindings
877 impl.requires = depends
879 for elem in item.childNodes:
880 if elem.uri != XMLNS_IFACE: continue
881 if elem.name == 'archive':
882 url = elem.getAttribute('href')
883 if not url:
884 raise InvalidInterface(_("Missing href attribute on <archive>"))
885 size = elem.getAttribute('size')
886 if not size:
887 raise InvalidInterface(_("Missing size attribute on <archive>"))
888 impl.add_download_source(url = url, size = long(size),
889 extract = elem.getAttribute('extract'),
890 start_offset = _get_long(elem, 'start-offset'),
891 type = elem.getAttribute('type'))
892 elif elem.name == 'manifest-digest':
893 for aname, avalue in elem.attrs.iteritems():
894 if ' ' not in aname:
895 impl.digests.append('%s=%s' % (aname, avalue))
896 elif elem.name == 'recipe':
897 recipe = Recipe()
898 for recipe_step in elem.childNodes:
899 if recipe_step.uri == XMLNS_IFACE and recipe_step.name == 'archive':
900 url = recipe_step.getAttribute('href')
901 if not url:
902 raise InvalidInterface(_("Missing href attribute on <archive>"))
903 size = recipe_step.getAttribute('size')
904 if not size:
905 raise InvalidInterface(_("Missing size attribute on <archive>"))
906 recipe.steps.append(DownloadSource(None, url = url, size = long(size),
907 extract = recipe_step.getAttribute('extract'),
908 start_offset = _get_long(recipe_step, 'start-offset'),
909 type = recipe_step.getAttribute('type')))
910 else:
911 info(_("Unknown step '%s' in recipe; skipping recipe"), recipe_step.name)
912 break
913 else:
914 impl.download_sources.append(recipe)
916 root_attrs = {'stability': 'testing'}
917 root_commands = {}
918 if main:
919 info("Note: @main on document element is deprecated in %s", self)
920 root_commands['run'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': main}))
921 process_group(feed_element, root_attrs, [], [], root_commands)
923 def get_distro_feed(self):
924 """Does this feed contain any <pacakge-implementation> elements?
925 i.e. is it worth asking the package manager for more information?
926 @return: the URL of the virtual feed, or None
927 @since: 0.49"""
928 if self._package_implementations:
929 return "distribution:" + self.url
930 return None
932 def get_package_impls(self, distro):
933 """Find the best <pacakge-implementation> element(s) for the given distribution.
934 @param distro: the distribution to use to rate them
935 @type distro: L{distro.Distribution}
936 @return: a list of tuples for the best ranked elements
937 @rtype: [str]
938 @since: 0.49"""
939 best_score = 0
940 best_impls = []
942 for item, item_attrs in self._package_implementations:
943 distro_names = item_attrs.get('distributions', '')
944 for distro_name in distro_names.split(' '):
945 score = distro.get_score(distro_name)
946 if score > best_score:
947 best_score = score
948 best_impls = []
949 if score == best_score:
950 best_impls.append((item, item_attrs))
951 return best_impls
953 def get_name(self):
954 return self.name or '(' + os.path.basename(self.url) + ')'
956 def __repr__(self):
957 return _("<Feed %s>") % self.url
959 """@deprecated"""
960 def _get_impl(self, id):
961 assert id not in self.implementations
963 if id.startswith('.') or id.startswith('/'):
964 id = os.path.abspath(os.path.join(self.url, id))
965 local_path = id
966 impl = ZeroInstallImplementation(self, id, local_path)
967 else:
968 impl = ZeroInstallImplementation(self, id, None)
969 impl.digests.append(id)
971 self.implementations[id] = impl
972 return impl
974 def set_stability_policy(self, new):
975 assert new is None or isinstance(new, Stability)
976 self.stability_policy = new
978 def get_feed(self, url):
979 for x in self.feeds:
980 if x.uri == url:
981 return x
982 return None
984 def add_metadata(self, elem):
985 self.metadata.append(elem)
987 def get_metadata(self, uri, name):
988 """Return a list of interface metadata elements with this name and namespace URI."""
989 return [m for m in self.metadata if m.name == name and m.uri == uri]
991 @property
992 def summary(self):
993 return _best_language_match(self.summaries)
995 @property
996 def description(self):
997 return _best_language_match(self.descriptions)
999 class DummyFeed(object):
1000 """Temporary class used during API transition."""
1001 last_modified = None
1002 name = '-'
1003 last_checked = property(lambda self: None)
1004 implementations = property(lambda self: {})
1005 feeds = property(lambda self: [])
1006 summary = property(lambda self: '-')
1007 description = property(lambda self: '')
1008 def get_name(self): return self.name
1009 def get_feed(self, url): return None
1010 def get_metadata(self, uri, name): return []
1011 _dummy_feed = DummyFeed()
1013 def unescape(uri):
1014 """Convert each %20 to a space, etc.
1015 @rtype: str"""
1016 uri = uri.replace('#', '/')
1017 if '%' not in uri: return uri
1018 return re.sub('%[0-9a-fA-F][0-9a-fA-F]',
1019 lambda match: chr(int(match.group(0)[1:], 16)),
1020 uri).decode('utf-8')
1022 def escape(uri):
1023 """Convert each space to %20, etc
1024 @rtype: str"""
1025 return re.sub('[^-_.a-zA-Z0-9]',
1026 lambda match: '%%%02x' % ord(match.group(0)),
1027 uri.encode('utf-8'))
1029 def _pretty_escape(uri):
1030 """Convert each space to %20, etc
1031 : is preserved and / becomes #. This makes for nicer strings,
1032 and may replace L{escape} everywhere in future.
1033 @rtype: str"""
1034 return re.sub('[^-_.a-zA-Z0-9:/]',
1035 lambda match: '%%%02x' % ord(match.group(0)),
1036 uri.encode('utf-8')).replace('/', '#')
1038 def canonical_iface_uri(uri):
1039 """If uri is a relative path, convert to an absolute one.
1040 A "file:///foo" URI is converted to "/foo".
1041 An "alias:prog" URI expands to the URI in the 0alias script
1042 Otherwise, return it unmodified.
1043 @rtype: str
1044 @raise SafeException: if uri isn't valid
1046 if uri.startswith('http://') or uri.startswith('https://'):
1047 if uri.count("/") < 3:
1048 raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
1049 return uri
1050 elif uri.startswith('file:///'):
1051 return uri[7:]
1052 elif uri.startswith('alias:'):
1053 from zeroinstall import alias, support
1054 alias_prog = uri[6:]
1055 if not os.path.isabs(alias_prog):
1056 full_path = support.find_in_path(alias_prog)
1057 if not full_path:
1058 raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
1059 else:
1060 full_path = alias_prog
1061 interface_uri, main = alias.parse_script(full_path)
1062 return interface_uri
1063 else:
1064 iface_uri = os.path.realpath(uri)
1065 if os.path.isfile(iface_uri):
1066 return iface_uri
1067 raise SafeException(_("Bad interface name '%(uri)s'.\n"
1068 "(doesn't start with 'http:', and "
1069 "doesn't exist as a local file '%(interface_uri)s' either)") %
1070 {'uri': uri, 'interface_uri': iface_uri})
1072 _version_mod_to_value = {
1073 'pre': -2,
1074 'rc': -1,
1075 '': 0,
1076 'post': 1,
1079 # Reverse mapping
1080 _version_value_to_mod = {}
1081 for x in _version_mod_to_value: _version_value_to_mod[_version_mod_to_value[x]] = x
1082 del x
1084 _version_re = re.compile('-([a-z]*)')
1086 def parse_version(version_string):
1087 """Convert a version string to an internal representation.
1088 The parsed format can be compared quickly using the standard Python functions.
1089 - Version := DottedList ("-" Mod DottedList?)*
1090 - DottedList := (Integer ("." Integer)*)
1091 @rtype: tuple (opaque)
1092 @raise SafeException: if the string isn't a valid version
1093 @since: 0.24 (moved from L{reader}, from where it is still available):"""
1094 if version_string is None: return None
1095 parts = _version_re.split(version_string)
1096 if parts[-1] == '':
1097 del parts[-1] # Ends with a modifier
1098 else:
1099 parts.append('')
1100 if not parts:
1101 raise SafeException(_("Empty version string!"))
1102 l = len(parts)
1103 try:
1104 for x in range(0, l, 2):
1105 part = parts[x]
1106 if part:
1107 parts[x] = map(int, parts[x].split('.'))
1108 else:
1109 parts[x] = [] # (because ''.split('.') == [''], not [])
1110 for x in range(1, l, 2):
1111 parts[x] = _version_mod_to_value[parts[x]]
1112 return parts
1113 except ValueError, ex:
1114 raise SafeException(_("Invalid version format in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1115 except KeyError, ex:
1116 raise SafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1118 def format_version(version):
1119 """Format a parsed version for display. Undoes the effect of L{parse_version}.
1120 @see: L{Implementation.get_version}
1121 @rtype: str
1122 @since: 0.24"""
1123 version = version[:]
1124 l = len(version)
1125 for x in range(0, l, 2):
1126 version[x] = '.'.join(map(str, version[x]))
1127 for x in range(1, l, 2):
1128 version[x] = '-' + _version_value_to_mod[version[x]]
1129 if version[-1] == '-': del version[-1]
1130 return ''.join(version)