Cope with the distribution cache changing while we're running
[zeroinstall/solver.git] / zeroinstall / injector / model.py
blobdf88b94bdf28b5f7b7d5e937891b19b36da8b65c
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
18 from logging import info, debug, warn
19 from zeroinstall import SafeException, version
20 from zeroinstall.injector.namespaces import XMLNS_IFACE
22 # Element names for bindings in feed files
23 binding_names = frozenset(['environment', 'overlay'])
25 network_offline = 'off-line'
26 network_minimal = 'minimal'
27 network_full = 'full'
28 network_levels = (network_offline, network_minimal, network_full)
30 stability_levels = {} # Name -> Stability
32 defaults = {
33 'PATH': '/bin:/usr/bin',
34 'XDG_CONFIG_DIRS': '/etc/xdg',
35 'XDG_DATA_DIRS': '/usr/local/share:/usr/share',
38 class InvalidInterface(SafeException):
39 """Raised when parsing an invalid feed."""
40 def __init__(self, message, ex = None):
41 if ex:
42 try:
43 message += "\n\n(exact error: %s)" % ex
44 except:
45 # Some Python messages have type str but contain UTF-8 sequences.
46 # (e.g. IOException). Adding these to a Unicode 'message' (e.g.
47 # after gettext translation) will cause an error.
48 import codecs
49 decoder = codecs.lookup('utf-8')
50 decex = decoder.decode(str(ex), errors = 'replace')[0]
51 message += "\n\n(exact error: %s)" % decex
53 SafeException.__init__(self, message)
55 def _split_arch(arch):
56 """Split an arch into an (os, machine) tuple. Either or both parts may be None."""
57 if not arch:
58 return None, None
59 elif '-' not in arch:
60 raise SafeException(_("Malformed arch '%s'") % arch)
61 else:
62 osys, machine = arch.split('-', 1)
63 if osys == '*': osys = None
64 if machine == '*': machine = None
65 return osys, machine
67 def _join_arch(osys, machine):
68 if osys == machine == None: return None
69 return "%s-%s" % (osys or '*', machine or '*')
71 class Stability(object):
72 """A stability rating. Each implementation has an upstream stability rating and,
73 optionally, a user-set rating."""
74 __slots__ = ['level', 'name', 'description']
75 def __init__(self, level, name, description):
76 self.level = level
77 self.name = name
78 self.description = description
79 assert name not in stability_levels
80 stability_levels[name] = self
82 def __cmp__(self, other):
83 return cmp(self.level, other.level)
85 def __str__(self):
86 return self.name
88 def __repr__(self):
89 return _("<Stability: %s>") % self.description
91 def process_binding(e):
92 """Internal"""
93 if e.name == 'environment':
94 mode = {
95 None: EnvironmentBinding.PREPEND,
96 'prepend': EnvironmentBinding.PREPEND,
97 'append': EnvironmentBinding.APPEND,
98 'replace': EnvironmentBinding.REPLACE,
99 }[e.getAttribute('mode')]
101 binding = EnvironmentBinding(e.getAttribute('name'),
102 insert = e.getAttribute('insert'),
103 default = e.getAttribute('default'),
104 mode = mode)
105 if not binding.name: raise InvalidInterface(_("Missing 'name' in binding"))
106 if binding.insert is None: raise InvalidInterface(_("Missing 'insert' in binding"))
107 return binding
108 elif e.name == 'overlay':
109 return OverlayBinding(e.getAttribute('src'), e.getAttribute('mount-point'))
110 else:
111 raise Exception(_("Unknown binding type '%s'") % e.name)
113 def process_depends(item):
114 """Internal"""
115 # Note: also called from selections
116 dep_iface = item.getAttribute('interface')
117 if not dep_iface:
118 raise InvalidInterface(_("Missing 'interface' on <requires>"))
119 dependency = InterfaceDependency(dep_iface, metadata = item.attrs)
121 for e in item.childNodes:
122 if e.uri != XMLNS_IFACE: continue
123 if e.name in binding_names:
124 dependency.bindings.append(process_binding(e))
125 elif e.name == 'version':
126 dependency.restrictions.append(
127 VersionRangeRestriction(not_before = parse_version(e.getAttribute('not-before')),
128 before = parse_version(e.getAttribute('before'))))
129 return dependency
131 def N_(message): return message
133 insecure = Stability(0, N_('insecure'), _('This is a security risk'))
134 buggy = Stability(5, N_('buggy'), _('Known to have serious bugs'))
135 developer = Stability(10, N_('developer'), _('Work-in-progress - bugs likely'))
136 testing = Stability(20, N_('testing'), _('Stability unknown - please test!'))
137 stable = Stability(30, N_('stable'), _('Tested - no serious problems found'))
138 packaged = Stability(35, N_('packaged'), _('Supplied by the local package manager'))
139 preferred = Stability(40, N_('preferred'), _('Best of all - must be set manually'))
141 del N_
143 class Restriction(object):
144 """A Restriction limits the allowed implementations of an Interface."""
145 __slots__ = []
147 def meets_restriction(self, impl):
148 """Called by the L{Solver} to check whether a particular implementation is acceptable.
149 @return: False if this implementation is not a possibility
150 @rtype: bool
152 raise NotImplementedError(_("Abstract"))
154 class VersionRestriction(Restriction):
155 """Only select implementations with a particular version number.
156 @since: 0.40"""
158 def __init__(self, version):
159 """@param version: the required version number
160 @see: L{parse_version}; use this to pre-process the version number
162 self.version = version
164 def meets_restriction(self, impl):
165 return impl.version == self.version
167 def __str__(self):
168 return _("(restriction: version = %s)") % format_version(self.version)
170 class VersionRangeRestriction(Restriction):
171 """Only versions within the given range are acceptable"""
172 __slots__ = ['before', 'not_before']
174 def __init__(self, before, not_before):
175 """@param before: chosen versions must be earlier than this
176 @param not_before: versions must be at least this high
177 @see: L{parse_version}; use this to pre-process the versions
179 self.before = before
180 self.not_before = not_before
182 def meets_restriction(self, impl):
183 if self.not_before and impl.version < self.not_before:
184 return False
185 if self.before and impl.version >= self.before:
186 return False
187 return True
189 def __str__(self):
190 if self.not_before is not None or self.before is not None:
191 range = ''
192 if self.not_before is not None:
193 range += format_version(self.not_before) + ' <= '
194 range += 'version'
195 if self.before is not None:
196 range += ' < ' + format_version(self.before)
197 else:
198 range = 'none'
199 return _("(restriction: %s)") % range
201 class Binding(object):
202 """Information about how the choice of a Dependency is made known
203 to the application being run."""
205 class EnvironmentBinding(Binding):
206 """Indicate the chosen implementation using an environment variable."""
207 __slots__ = ['name', 'insert', 'default', 'mode']
209 PREPEND = 'prepend'
210 APPEND = 'append'
211 REPLACE = 'replace'
213 def __init__(self, name, insert, default = None, mode = PREPEND):
214 """mode argument added in version 0.28"""
215 self.name = name
216 self.insert = insert
217 self.default = default
218 self.mode = mode
220 def __str__(self):
221 return _("<environ %(name)s %(mode)s %(insert)s>") % {'name': self.name,'mode': self.mode, 'insert': self.insert}
223 __repr__ = __str__
225 def get_value(self, path, old_value):
226 """Calculate the new value of the environment variable after applying this binding.
227 @param path: the path to the selected implementation
228 @param old_value: the current value of the environment variable
229 @return: the new value for the environment variable"""
230 extra = os.path.join(path, self.insert)
232 if self.mode == EnvironmentBinding.REPLACE:
233 return extra
235 if old_value is None:
236 old_value = self.default or defaults.get(self.name, None)
237 if old_value is None:
238 return extra
239 if self.mode == EnvironmentBinding.PREPEND:
240 return extra + ':' + old_value
241 else:
242 return old_value + ':' + extra
244 def _toxml(self, doc):
245 """Create a DOM element for this binding.
246 @param doc: document to use to create the element
247 @return: the new element
249 env_elem = doc.createElementNS(XMLNS_IFACE, 'environment')
250 env_elem.setAttributeNS(None, 'name', self.name)
251 env_elem.setAttributeNS(None, 'insert', self.insert)
252 if self.default:
253 env_elem.setAttributeNS(None, 'default', self.default)
254 return env_elem
256 class OverlayBinding(Binding):
257 """Make the chosen implementation available by overlaying it onto another part of the file-system.
258 This is to support legacy programs which use hard-coded paths."""
259 __slots__ = ['src', 'mount_point']
261 def __init__(self, src, mount_point):
262 self.src = src
263 self.mount_point = mount_point
265 def __str__(self):
266 return _("<overlay %(src)s on %(mount_point)s>") % {'src': self.src or '.', 'mount_point': self.mount_point or '/'}
268 __repr__ = __str__
270 def _toxml(self, doc):
271 """Create a DOM element for this binding.
272 @param doc: document to use to create the element
273 @return: the new element
275 env_elem = doc.createElementNS(XMLNS_IFACE, 'overlay')
276 if self.src is not None:
277 env_elem.setAttributeNS(None, 'src', self.src)
278 if self.mount_point is not None:
279 env_elem.setAttributeNS(None, 'mount-point', self.mount_point)
280 return env_elem
282 class Feed(object):
283 """An interface's feeds are other interfaces whose implementations can also be
284 used as implementations of this interface."""
285 __slots__ = ['uri', 'os', 'machine', 'user_override', 'langs']
286 def __init__(self, uri, arch, user_override, langs = None):
287 self.uri = uri
288 # This indicates whether the feed comes from the user's overrides
289 # file. If true, writer.py will write it when saving.
290 self.user_override = user_override
291 self.os, self.machine = _split_arch(arch)
292 self.langs = langs
294 def __str__(self):
295 return "<Feed from %s>" % self.uri
296 __repr__ = __str__
298 arch = property(lambda self: _join_arch(self.os, self.machine))
300 class Dependency(object):
301 """A Dependency indicates that an Implementation requires some additional
302 code to function. This is an abstract base class.
303 @ivar metadata: any extra attributes from the XML element
304 @type metadata: {str: str}
306 __slots__ = ['metadata']
308 def __init__(self, metadata):
309 if metadata is None:
310 metadata = {}
311 else:
312 assert not isinstance(metadata, basestring) # Use InterfaceDependency instead!
313 self.metadata = metadata
315 class InterfaceDependency(Dependency):
316 """A Dependency on a Zero Install interface.
317 @ivar interface: the interface required by this dependency
318 @type interface: str
319 @ivar restrictions: a list of constraints on acceptable implementations
320 @type restrictions: [L{Restriction}]
321 @ivar bindings: how to make the choice of implementation known
322 @type bindings: [L{Binding}]
323 @since: 0.28
325 __slots__ = ['interface', 'restrictions', 'bindings', 'metadata']
327 def __init__(self, interface, restrictions = None, metadata = None):
328 Dependency.__init__(self, metadata)
329 assert isinstance(interface, (str, unicode))
330 assert interface
331 self.interface = interface
332 if restrictions is None:
333 self.restrictions = []
334 else:
335 self.restrictions = restrictions
336 self.bindings = []
338 def __str__(self):
339 return _("<Dependency on %(interface)s; bindings: %(bindings)s%(restrictions)s>") % {'interface': self.interface, 'bindings': self.bindings, 'restrictions': self.restrictions}
341 class RetrievalMethod(object):
342 """A RetrievalMethod provides a way to fetch an implementation."""
343 __slots__ = []
345 class DownloadSource(RetrievalMethod):
346 """A DownloadSource provides a way to fetch an implementation."""
347 __slots__ = ['implementation', 'url', 'size', 'extract', 'start_offset', 'type']
349 def __init__(self, implementation, url, size, extract, start_offset = 0, type = None):
350 self.implementation = implementation
351 self.url = url
352 self.size = size
353 self.extract = extract
354 self.start_offset = start_offset
355 self.type = type # MIME type - see unpack.py
357 class Recipe(RetrievalMethod):
358 """Get an implementation by following a series of steps.
359 @ivar size: the combined download sizes from all the steps
360 @type size: int
361 @ivar steps: the sequence of steps which must be performed
362 @type steps: [L{RetrievalMethod}]"""
363 __slots__ = ['steps']
365 def __init__(self):
366 self.steps = []
368 size = property(lambda self: sum([x.size for x in self.steps]))
370 class DistributionSource(RetrievalMethod):
371 """A package that is installed using the distribution's tools (including PackageKit).
372 @ivar package_id: the package name, in a form recognised by the distribution's tools
373 @type package_id: str
374 @ivar size: the download size in bytes
375 @type size: int"""
377 __slots__ = ['package_id', 'size']
379 def __init__(self, package_id, size):
380 RetrievalMethod.__init__(self)
381 self.package_id = package_id
382 self.size = size
384 class Implementation(object):
385 """An Implementation is a package which implements an Interface.
386 @ivar download_sources: list of methods of getting this implementation
387 @type download_sources: [L{RetrievalMethod}]
388 @ivar feed: the feed owning this implementation (since 0.32)
389 @type feed: [L{ZeroInstallFeed}]
390 @ivar bindings: how to tell this component where it itself is located (since 0.31)
391 @type bindings: [Binding]
392 @ivar upstream_stability: the stability reported by the packager
393 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged]
394 @ivar user_stability: the stability as set by the user
395 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged | preferred]
396 @ivar langs: natural languages supported by this package
397 @ivar requires: interfaces this package depends on
398 @type requires: [L{Dependency}]
399 @ivar main: the default file to execute when running as a program
400 @ivar metadata: extra metadata from the feed
401 @type metadata: {"[URI ]localName": str}
402 @ivar id: a unique identifier for this Implementation
403 @ivar version: a parsed version number
404 @ivar released: release date
405 @ivar local_path: the directory containing this local implementation, or None if it isn't local (id isn't a path)
406 @type local_path: str | None
407 @ivar requires_root_install: whether the user will need admin rights to use this
408 @type requires_root_install: bool
411 # Note: user_stability shouldn't really be here
413 __slots__ = ['upstream_stability', 'user_stability', 'langs',
414 'requires', 'main', 'metadata', 'download_sources',
415 'id', 'feed', 'version', 'released', 'bindings', 'machine']
417 def __init__(self, feed, id):
418 assert id
419 self.feed = feed
420 self.id = id
421 self.main = None
422 self.user_stability = None
423 self.upstream_stability = None
424 self.metadata = {} # [URI + " "] + localName -> value
425 self.requires = []
426 self.version = None
427 self.released = None
428 self.download_sources = []
429 self.langs = None
430 self.machine = None
431 self.bindings = []
433 def get_stability(self):
434 return self.user_stability or self.upstream_stability or testing
436 def __str__(self):
437 return self.id
439 def __repr__(self):
440 return "v%s (%s)" % (self.get_version(), self.id)
442 def __cmp__(self, other):
443 """Newer versions come first"""
444 d = cmp(other.version, self.version)
445 if d: return d
446 # If the version number is the same, just give a stable sort order, and
447 # ensure that two different implementations don't compare equal.
448 d = cmp(other.feed.url, self.feed.url)
449 if d: return d
450 return cmp(other.id, self.id)
452 def get_version(self):
453 """Return the version as a string.
454 @see: L{format_version}
456 return format_version(self.version)
458 arch = property(lambda self: _join_arch(self.os, self.machine))
460 os = None
461 local_path = None
462 digests = None
463 requires_root_install = False
465 class DistributionImplementation(Implementation):
466 """An implementation provided by the distribution. Information such as the version
467 comes from the package manager.
468 @since: 0.28"""
470 def __init__(self, feed, id, distro):
471 assert id.startswith('package:')
472 Implementation.__init__(self, feed, id)
473 self.distro = distro
475 @property
476 def requires_root_install(self):
477 return not self.installed
479 @property
480 def installed(self):
481 return self.distro.get_installed(self.id)
483 class ZeroInstallImplementation(Implementation):
484 """An implementation where all the information comes from Zero Install.
485 @ivar digests: a list of "algorith=value" strings (since 0.45)
486 @type digests: [str]
487 @since: 0.28"""
488 __slots__ = ['os', 'size', 'digests', 'local_path']
490 def __init__(self, feed, id, local_path):
491 """id can be a local path (string starting with /) or a manifest hash (eg "sha1=XXX")"""
492 assert not id.startswith('package:'), id
493 Implementation.__init__(self, feed, id)
494 self.size = None
495 self.os = None
496 self.digests = []
497 self.local_path = local_path
499 # Deprecated
500 dependencies = property(lambda self: dict([(x.interface, x) for x in self.requires
501 if isinstance(x, InterfaceDependency)]))
503 def add_download_source(self, url, size, extract, start_offset = 0, type = None):
504 """Add a download source."""
505 self.download_sources.append(DownloadSource(self, url, size, extract, start_offset, type))
507 def set_arch(self, arch):
508 self.os, self.machine = _split_arch(arch)
509 arch = property(lambda self: _join_arch(self.os, self.machine), set_arch)
511 class Interface(object):
512 """An Interface represents some contract of behaviour.
513 @ivar uri: the URI for this interface.
514 @ivar stability_policy: user's configured policy.
515 Implementations at this level or higher are preferred.
516 Lower levels are used only if there is no other choice.
518 __slots__ = ['uri', 'stability_policy', '_main_feed', 'extra_feeds']
520 implementations = property(lambda self: self._main_feed.implementations)
521 name = property(lambda self: self._main_feed.name)
522 description = property(lambda self: self._main_feed.description)
523 summary = property(lambda self: self._main_feed.summary)
524 last_modified = property(lambda self: self._main_feed.last_modified)
525 feeds = property(lambda self: self.extra_feeds + self._main_feed.feeds)
526 metadata = property(lambda self: self._main_feed.metadata)
528 last_checked = property(lambda self: self._main_feed.last_checked)
530 def __init__(self, uri):
531 assert uri
532 if uri.startswith('http:') or uri.startswith('https:') or uri.startswith('/'):
533 self.uri = uri
534 else:
535 raise SafeException(_("Interface name '%s' doesn't start "
536 "with 'http:' or 'https:'") % uri)
537 self.reset()
539 def _get_feed_for(self):
540 retval = {}
541 for key in self._main_feed.feed_for:
542 retval[key] = True
543 return retval
544 feed_for = property(_get_feed_for) # Deprecated (used by 0publish)
546 def reset(self):
547 self.extra_feeds = []
548 self._main_feed = _dummy_feed
549 self.stability_policy = None
551 def get_name(self):
552 if self._main_feed is not _dummy_feed:
553 return self._main_feed.get_name()
554 return '(' + os.path.basename(self.uri) + ')'
556 def __repr__(self):
557 return _("<Interface %s>") % self.uri
559 def set_stability_policy(self, new):
560 assert new is None or isinstance(new, Stability)
561 self.stability_policy = new
563 def get_feed(self, url):
564 for x in self.extra_feeds:
565 if x.uri == url:
566 return x
567 return self._main_feed.get_feed(url)
569 def get_metadata(self, uri, name):
570 return self._main_feed.get_metadata(uri, name)
572 def _merge_attrs(attrs, item):
573 """Add each attribute of item to a copy of attrs and return the copy.
574 @type attrs: {str: str}
575 @type item: L{qdom.Element}
576 @rtype: {str: str}
578 new = attrs.copy()
579 for a in item.attrs:
580 new[str(a)] = item.attrs[a]
581 return new
583 def _get_long(elem, attr_name):
584 val = elem.getAttribute(attr_name)
585 if val is not None:
586 try:
587 val = long(val)
588 except ValueError, ex:
589 raise SafeException(_("Invalid value for integer attribute '%(attribute_name)s': %(value)s") % {'attribute_name': attr_name, 'value': val})
590 return val
592 class ZeroInstallFeed(object):
593 """A feed lists available implementations of an interface.
594 @ivar url: the URL for this feed
595 @ivar implementations: Implementations in this feed, indexed by ID
596 @type implementations: {str: L{Implementation}}
597 @ivar name: human-friendly name
598 @ivar summary: short textual description
599 @ivar description: long textual description
600 @ivar last_modified: timestamp on signature
601 @ivar last_checked: time feed was last successfully downloaded and updated
602 @ivar feeds: list of <feed> elements in this feed
603 @type feeds: [L{Feed}]
604 @ivar feed_for: interfaces for which this could be a feed
605 @type feed_for: set(str)
606 @ivar metadata: extra elements we didn't understand
608 # _main is deprecated
609 __slots__ = ['url', 'implementations', 'name', 'description', 'summary',
610 'last_checked', 'last_modified', 'feeds', 'feed_for', 'metadata']
612 def __init__(self, feed_element, local_path = None, distro = None):
613 """Create a feed object from a DOM.
614 @param feed_element: the root element of a feed file
615 @type feed_element: L{qdom.Element}
616 @param local_path: the pathname of this local feed, or None for remote feeds
617 @param distro: used to resolve distribution package references
618 @type distro: L{distro.Distribution} or None"""
619 assert feed_element
620 self.implementations = {}
621 self.name = None
622 self.summary = None
623 self.description = ""
624 self.last_modified = None
625 self.feeds = []
626 self.feed_for = set()
627 self.metadata = []
628 self.last_checked = None
630 assert feed_element.name in ('interface', 'feed'), "Root element should be <interface>, not %s" % feed_element
631 assert feed_element.uri == XMLNS_IFACE, "Wrong namespace on root element: %s" % feed_element.uri
633 main = feed_element.getAttribute('main')
634 #if main: warn("Setting 'main' on the root element is deprecated. Put it on a <group> instead")
636 if local_path:
637 self.url = local_path
638 local_dir = os.path.dirname(local_path)
639 else:
640 self.url = feed_element.getAttribute('uri')
641 if not self.url:
642 raise InvalidInterface(_("<interface> uri attribute missing"))
643 local_dir = None # Can't have relative paths
645 min_injector_version = feed_element.getAttribute('min-injector-version')
646 if min_injector_version:
647 if parse_version(min_injector_version) > parse_version(version):
648 raise InvalidInterface(_("This feed requires version %(min_version)s or later of "
649 "Zero Install, but I am only version %(version)s. "
650 "You can get a newer version from http://0install.net") %
651 {'min_version': min_injector_version, 'version': version})
653 for x in feed_element.childNodes:
654 if x.uri != XMLNS_IFACE:
655 self.metadata.append(x)
656 continue
657 if x.name == 'name':
658 self.name = x.content
659 elif x.name == 'description':
660 self.description = x.content
661 elif x.name == 'summary':
662 self.summary = x.content
663 elif x.name == 'feed-for':
664 feed_iface = x.getAttribute('interface')
665 if not feed_iface:
666 raise InvalidInterface(_('Missing "interface" attribute in <feed-for>'))
667 self.feed_for.add(feed_iface)
668 # Bug report from a Debian/stable user that --feed gets the wrong value.
669 # Can't reproduce (even in a Debian/stable chroot), but add some logging here
670 # in case it happens again.
671 debug(_("Is feed-for %s"), feed_iface)
672 elif x.name == 'feed':
673 feed_src = x.getAttribute('src')
674 if not feed_src:
675 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
676 if feed_src.startswith('http:') or feed_src.startswith('https:') or local_path:
677 self.feeds.append(Feed(feed_src, x.getAttribute('arch'), False, langs = x.getAttribute('langs')))
678 else:
679 raise InvalidInterface(_("Invalid feed URL '%s'") % feed_src)
680 else:
681 self.metadata.append(x)
683 if not self.name:
684 raise InvalidInterface(_("Missing <name> in feed"))
685 if not self.summary:
686 raise InvalidInterface(_("Missing <summary> in feed"))
688 package_impls = [0, []] # Best score so far and packages with that score
690 def process_group(group, group_attrs, base_depends, base_bindings):
691 for item in group.childNodes:
692 if item.uri != XMLNS_IFACE: continue
694 if item.name not in ('group', 'implementation', 'package-implementation'):
695 continue
697 depends = base_depends[:]
698 bindings = base_bindings[:]
700 item_attrs = _merge_attrs(group_attrs, item)
702 # We've found a group or implementation. Scan for dependencies
703 # and bindings. Doing this here means that:
704 # - We can share the code for groups and implementations here.
705 # - The order doesn't matter, because these get processed first.
706 # A side-effect is that the document root cannot contain
707 # these.
708 for child in item.childNodes:
709 if child.uri != XMLNS_IFACE: continue
710 if child.name == 'requires':
711 dep = process_depends(child)
712 depends.append(dep)
713 elif child.name in binding_names:
714 bindings.append(process_binding(child))
716 if item.name == 'group':
717 process_group(item, item_attrs, depends, bindings)
718 elif item.name == 'implementation':
719 process_impl(item, item_attrs, depends, bindings)
720 elif item.name == 'package-implementation':
721 distro_names = item_attrs.get('distributions', '')
722 for distro_name in distro_names.split(' '):
723 score = distro.get_score(distro_name)
724 if score > package_impls[0]:
725 package_impls[0] = score
726 package_impls[1] = []
727 if score == package_impls[0]:
728 package_impls[1].append((item, item_attrs, depends))
729 else:
730 assert 0
732 def process_impl(item, item_attrs, depends, bindings):
733 id = item.getAttribute('id')
734 if id is None:
735 raise InvalidInterface(_("Missing 'id' attribute on %s") % item)
736 local_path = item_attrs.get('local-path')
737 if local_dir and local_path:
738 abs_local_path = os.path.abspath(os.path.join(local_dir, local_path))
739 impl = ZeroInstallImplementation(self, id, abs_local_path)
740 elif local_dir and (id.startswith('/') or id.startswith('.')):
741 # For old feeds
742 id = os.path.abspath(os.path.join(local_dir, id))
743 impl = ZeroInstallImplementation(self, id, id)
744 else:
745 impl = ZeroInstallImplementation(self, id, None)
746 if '=' in id:
747 # In older feeds, the ID was the (single) digest
748 impl.digests.append(id)
749 if id in self.implementations:
750 warn(_("Duplicate ID '%s' in feed '%s'"), id, self)
751 self.implementations[id] = impl
753 impl.metadata = item_attrs
754 try:
755 version_mod = item_attrs.get('version-modifier', None)
756 if version_mod:
757 item_attrs['version'] += version_mod
758 del item_attrs['version-modifier']
759 version = item_attrs['version']
760 except KeyError:
761 raise InvalidInterface(_("Missing version attribute"))
762 impl.version = parse_version(version)
764 item_main = item_attrs.get('main', None)
765 if item_main and item_main.startswith('/'):
766 raise InvalidInterface(_("'main' attribute must be relative, but '%s' starts with '/'!") %
767 item_main)
768 impl.main = item_main
770 impl.released = item_attrs.get('released', None)
771 impl.langs = item_attrs.get('langs', None)
773 size = item.getAttribute('size')
774 if size:
775 impl.size = long(size)
776 impl.arch = item_attrs.get('arch', None)
777 try:
778 stability = stability_levels[str(item_attrs['stability'])]
779 except KeyError:
780 stab = str(item_attrs['stability'])
781 if stab != stab.lower():
782 raise InvalidInterface(_('Stability "%s" invalid - use lower case!') % item_attrs.stability)
783 raise InvalidInterface(_('Stability "%s" invalid') % item_attrs['stability'])
784 if stability >= preferred:
785 raise InvalidInterface(_("Upstream can't set stability to preferred!"))
786 impl.upstream_stability = stability
788 impl.bindings = bindings
789 impl.requires = depends
791 for elem in item.childNodes:
792 if elem.uri != XMLNS_IFACE: continue
793 if elem.name == 'archive':
794 url = elem.getAttribute('href')
795 if not url:
796 raise InvalidInterface(_("Missing href attribute on <archive>"))
797 size = elem.getAttribute('size')
798 if not size:
799 raise InvalidInterface(_("Missing size attribute on <archive>"))
800 impl.add_download_source(url = url, size = long(size),
801 extract = elem.getAttribute('extract'),
802 start_offset = _get_long(elem, 'start-offset'),
803 type = elem.getAttribute('type'))
804 elif elem.name == 'manifest-digest':
805 for aname, avalue in elem.attrs.iteritems():
806 if ' ' not in aname:
807 impl.digests.append('%s=%s' % (aname, avalue))
808 elif elem.name == 'recipe':
809 recipe = Recipe()
810 for recipe_step in elem.childNodes:
811 if recipe_step.uri == XMLNS_IFACE and recipe_step.name == 'archive':
812 url = recipe_step.getAttribute('href')
813 if not url:
814 raise InvalidInterface(_("Missing href attribute on <archive>"))
815 size = recipe_step.getAttribute('size')
816 if not size:
817 raise InvalidInterface(_("Missing size attribute on <archive>"))
818 recipe.steps.append(DownloadSource(None, url = url, size = long(size),
819 extract = recipe_step.getAttribute('extract'),
820 start_offset = _get_long(recipe_step, 'start-offset'),
821 type = recipe_step.getAttribute('type')))
822 else:
823 info(_("Unknown step '%s' in recipe; skipping recipe"), recipe_step.name)
824 break
825 else:
826 impl.download_sources.append(recipe)
828 def process_native_impl(item, item_attrs, depends):
829 package = item_attrs.get('package', None)
830 if package is None:
831 raise InvalidInterface(_("Missing 'package' attribute on %s") % item)
833 def factory(id):
834 assert id.startswith('package:')
835 if id in self.implementations:
836 warn(_("Duplicate ID '%s' for DistributionImplementation"), id)
837 impl = DistributionImplementation(self, id, distro)
838 self.implementations[id] = impl
840 impl.metadata = item_attrs
842 item_main = item_attrs.get('main', None)
843 if item_main and not item_main.startswith('/'):
844 raise InvalidInterface(_("'main' attribute must be absolute, but '%s' doesn't start with '/'!") %
845 item_main)
846 impl.main = item_main
847 impl.upstream_stability = packaged
848 impl.requires = depends
850 return impl
852 distro.get_package_info(package, factory)
854 root_attrs = {'stability': 'testing'}
855 if main:
856 root_attrs['main'] = main
857 process_group(feed_element, root_attrs, [], [])
859 for args in package_impls[1]:
860 process_native_impl(*args)
862 def get_name(self):
863 return self.name or '(' + os.path.basename(self.url) + ')'
865 def __repr__(self):
866 return _("<Feed %s>") % self.url
868 """@deprecated"""
869 def _get_impl(self, id):
870 assert id not in self.implementations
872 if id.startswith('.') or id.startswith('/'):
873 id = os.path.abspath(os.path.join(self.url, id))
874 local_path = id
875 impl = ZeroInstallImplementation(self, id, local_path)
876 else:
877 impl = ZeroInstallImplementation(self, id, None)
878 impl.digests.append(id)
880 self.implementations[id] = impl
881 return impl
883 def set_stability_policy(self, new):
884 assert new is None or isinstance(new, Stability)
885 self.stability_policy = new
887 def get_feed(self, url):
888 for x in self.feeds:
889 if x.uri == url:
890 return x
891 return None
893 def add_metadata(self, elem):
894 self.metadata.append(elem)
896 def get_metadata(self, uri, name):
897 """Return a list of interface metadata elements with this name and namespace URI."""
898 return [m for m in self.metadata if m.name == name and m.uri == uri]
900 class DummyFeed(object):
901 """Temporary class used during API transition."""
902 last_modified = None
903 name = '-'
904 last_checked = property(lambda self: None)
905 implementations = property(lambda self: {})
906 feeds = property(lambda self: [])
907 summary = property(lambda self: '-')
908 description = property(lambda self: '')
909 def get_name(self): return self.name
910 def get_feed(self, url): return None
911 def get_metadata(self, uri, name): return []
912 _dummy_feed = DummyFeed()
914 def unescape(uri):
915 """Convert each %20 to a space, etc.
916 @rtype: str"""
917 uri = uri.replace('#', '/')
918 if '%' not in uri: return uri
919 return re.sub('%[0-9a-fA-F][0-9a-fA-F]',
920 lambda match: chr(int(match.group(0)[1:], 16)),
921 uri).decode('utf-8')
923 def escape(uri):
924 """Convert each space to %20, etc
925 @rtype: str"""
926 return re.sub('[^-_.a-zA-Z0-9]',
927 lambda match: '%%%02x' % ord(match.group(0)),
928 uri.encode('utf-8'))
930 def _pretty_escape(uri):
931 """Convert each space to %20, etc
932 : is preserved and / becomes #. This makes for nicer strings,
933 and may replace L{escape} everywhere in future.
934 @rtype: str"""
935 return re.sub('[^-_.a-zA-Z0-9:/]',
936 lambda match: '%%%02x' % ord(match.group(0)),
937 uri.encode('utf-8')).replace('/', '#')
939 def canonical_iface_uri(uri):
940 """If uri is a relative path, convert to an absolute one.
941 A "file:///foo" URI is converted to "/foo".
942 Otherwise, return it unmodified.
943 @rtype: str
944 @raise SafeException: if uri isn't valid
946 if uri.startswith('http://') or uri.startswith('https://'):
947 if uri.count("/") < 3:
948 raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
949 return uri
950 elif uri.startswith('file:///'):
951 return uri[7:]
952 else:
953 iface_uri = os.path.realpath(uri)
954 if os.path.isfile(iface_uri):
955 return iface_uri
956 raise SafeException(_("Bad interface name '%(uri)s'.\n"
957 "(doesn't start with 'http:', and "
958 "doesn't exist as a local file '%(interface_uri)s' either)") %
959 {'uri': uri, 'interface_uri': iface_uri})
961 _version_mod_to_value = {
962 'pre': -2,
963 'rc': -1,
964 '': 0,
965 'post': 1,
968 # Reverse mapping
969 _version_value_to_mod = {}
970 for x in _version_mod_to_value: _version_value_to_mod[_version_mod_to_value[x]] = x
971 del x
973 _version_re = re.compile('-([a-z]*)')
975 def parse_version(version_string):
976 """Convert a version string to an internal representation.
977 The parsed format can be compared quickly using the standard Python functions.
978 - Version := DottedList ("-" Mod DottedList?)*
979 - DottedList := (Integer ("." Integer)*)
980 @rtype: tuple (opaque)
981 @raise SafeException: if the string isn't a valid version
982 @since: 0.24 (moved from L{reader}, from where it is still available):"""
983 if version_string is None: return None
984 parts = _version_re.split(version_string)
985 if parts[-1] == '':
986 del parts[-1] # Ends with a modifier
987 else:
988 parts.append('')
989 if not parts:
990 raise SafeException(_("Empty version string!"))
991 l = len(parts)
992 try:
993 for x in range(0, l, 2):
994 part = parts[x]
995 if part:
996 parts[x] = map(int, parts[x].split('.'))
997 else:
998 parts[x] = [] # (because ''.split('.') == [''], not [])
999 for x in range(1, l, 2):
1000 parts[x] = _version_mod_to_value[parts[x]]
1001 return parts
1002 except ValueError, ex:
1003 raise SafeException(_("Invalid version format in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1004 except KeyError, ex:
1005 raise SafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1007 def format_version(version):
1008 """Format a parsed version for display. Undoes the effect of L{parse_version}.
1009 @see: L{Implementation.get_version}
1010 @rtype: str
1011 @since: 0.24"""
1012 version = version[:]
1013 l = len(version)
1014 for x in range(0, l, 2):
1015 version[x] = '.'.join(map(str, version[x]))
1016 for x in range(1, l, 2):
1017 version[x] = '-' + _version_value_to_mod[version[x]]
1018 if version[-1] == '-': del version[-1]
1019 return ''.join(version)