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
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
13 # Copyright (C) 2009, Thomas Leonard
14 # See the README file for details, or visit http://0install.net.
16 from zeroinstall
import _
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'
28 network_levels
= (network_offline
, network_minimal
, network_full
)
30 stability_levels
= {} # Name -> Stability
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):
43 message
+= "\n\n(exact error: %s)" % ex
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.
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."""
60 raise SafeException(_("Malformed arch '%s'") % arch
)
62 osys
, machine
= arch
.split('-', 1)
63 if osys
== '*': osys
= None
64 if machine
== '*': machine
= None
67 def _join_arch(osys
, machine
):
68 if osys
== machine
== None: return None
69 return "%s-%s" % (osys
or '*', machine
or '*')
71 def _best_language_match(options
):
72 (language
, encoding
) = locale
.getlocale(locale
.LC_ALL
)
73 return (options
.get(language
, None) or
74 options
.get(language
.split('_', 1)[0], None) or
75 options
.get(None, None))
77 class Stability(object):
78 """A stability rating. Each implementation has an upstream stability rating and,
79 optionally, a user-set rating."""
80 __slots__
= ['level', 'name', 'description']
81 def __init__(self
, level
, name
, description
):
84 self
.description
= description
85 assert name
not in stability_levels
86 stability_levels
[name
] = self
88 def __cmp__(self
, other
):
89 return cmp(self
.level
, other
.level
)
95 return _("<Stability: %s>") % self
.description
97 def process_binding(e
):
99 if e
.name
== 'environment':
101 None: EnvironmentBinding
.PREPEND
,
102 'prepend': EnvironmentBinding
.PREPEND
,
103 'append': EnvironmentBinding
.APPEND
,
104 'replace': EnvironmentBinding
.REPLACE
,
105 }[e
.getAttribute('mode')]
107 binding
= EnvironmentBinding(e
.getAttribute('name'),
108 insert
= e
.getAttribute('insert'),
109 default
= e
.getAttribute('default'),
111 if not binding
.name
: raise InvalidInterface(_("Missing 'name' in binding"))
112 if binding
.insert
is None: raise InvalidInterface(_("Missing 'insert' in binding"))
114 elif e
.name
== 'overlay':
115 return OverlayBinding(e
.getAttribute('src'), e
.getAttribute('mount-point'))
117 raise Exception(_("Unknown binding type '%s'") % e
.name
)
119 def process_depends(item
):
121 # Note: also called from selections
122 dep_iface
= item
.getAttribute('interface')
124 raise InvalidInterface(_("Missing 'interface' on <requires>"))
125 dependency
= InterfaceDependency(dep_iface
, metadata
= item
.attrs
)
127 for e
in item
.childNodes
:
128 if e
.uri
!= XMLNS_IFACE
: continue
129 if e
.name
in binding_names
:
130 dependency
.bindings
.append(process_binding(e
))
131 elif e
.name
== 'version':
132 dependency
.restrictions
.append(
133 VersionRangeRestriction(not_before
= parse_version(e
.getAttribute('not-before')),
134 before
= parse_version(e
.getAttribute('before'))))
137 def N_(message
): return message
139 insecure
= Stability(0, N_('insecure'), _('This is a security risk'))
140 buggy
= Stability(5, N_('buggy'), _('Known to have serious bugs'))
141 developer
= Stability(10, N_('developer'), _('Work-in-progress - bugs likely'))
142 testing
= Stability(20, N_('testing'), _('Stability unknown - please test!'))
143 stable
= Stability(30, N_('stable'), _('Tested - no serious problems found'))
144 packaged
= Stability(35, N_('packaged'), _('Supplied by the local package manager'))
145 preferred
= Stability(40, N_('preferred'), _('Best of all - must be set manually'))
149 class Restriction(object):
150 """A Restriction limits the allowed implementations of an Interface."""
153 def meets_restriction(self
, impl
):
154 """Called by the L{solver.Solver} to check whether a particular implementation is acceptable.
155 @return: False if this implementation is not a possibility
158 raise NotImplementedError(_("Abstract"))
160 class VersionRestriction(Restriction
):
161 """Only select implementations with a particular version number.
164 def __init__(self
, version
):
165 """@param version: the required version number
166 @see: L{parse_version}; use this to pre-process the version number
168 self
.version
= version
170 def meets_restriction(self
, impl
):
171 return impl
.version
== self
.version
174 return _("(restriction: version = %s)") % format_version(self
.version
)
176 class VersionRangeRestriction(Restriction
):
177 """Only versions within the given range are acceptable"""
178 __slots__
= ['before', 'not_before']
180 def __init__(self
, before
, not_before
):
181 """@param before: chosen versions must be earlier than this
182 @param not_before: versions must be at least this high
183 @see: L{parse_version}; use this to pre-process the versions
186 self
.not_before
= not_before
188 def meets_restriction(self
, impl
):
189 if self
.not_before
and impl
.version
< self
.not_before
:
191 if self
.before
and impl
.version
>= self
.before
:
196 if self
.not_before
is not None or self
.before
is not None:
198 if self
.not_before
is not None:
199 range += format_version(self
.not_before
) + ' <= '
201 if self
.before
is not None:
202 range += ' < ' + format_version(self
.before
)
205 return _("(restriction: %s)") % range
207 class Binding(object):
208 """Information about how the choice of a Dependency is made known
209 to the application being run."""
211 class EnvironmentBinding(Binding
):
212 """Indicate the chosen implementation using an environment variable."""
213 __slots__
= ['name', 'insert', 'default', 'mode']
219 def __init__(self
, name
, insert
, default
= None, mode
= PREPEND
):
220 """mode argument added in version 0.28"""
223 self
.default
= default
227 return _("<environ %(name)s %(mode)s %(insert)s>") % {'name': self
.name
,'mode': self
.mode
, 'insert': self
.insert
}
231 def get_value(self
, path
, old_value
):
232 """Calculate the new value of the environment variable after applying this binding.
233 @param path: the path to the selected implementation
234 @param old_value: the current value of the environment variable
235 @return: the new value for the environment variable"""
236 extra
= os
.path
.join(path
, self
.insert
)
238 if self
.mode
== EnvironmentBinding
.REPLACE
:
241 if old_value
is None:
242 old_value
= self
.default
or defaults
.get(self
.name
, None)
243 if old_value
is None:
245 if self
.mode
== EnvironmentBinding
.PREPEND
:
246 return extra
+ os
.pathsep
+ old_value
248 return old_value
+ os
.pathsep
+ extra
250 def _toxml(self
, doc
):
251 """Create a DOM element for this binding.
252 @param doc: document to use to create the element
253 @return: the new element
255 env_elem
= doc
.createElementNS(XMLNS_IFACE
, 'environment')
256 env_elem
.setAttributeNS(None, 'name', self
.name
)
257 env_elem
.setAttributeNS(None, 'insert', self
.insert
)
259 env_elem
.setAttributeNS(None, 'default', self
.default
)
262 class OverlayBinding(Binding
):
263 """Make the chosen implementation available by overlaying it onto another part of the file-system.
264 This is to support legacy programs which use hard-coded paths."""
265 __slots__
= ['src', 'mount_point']
267 def __init__(self
, src
, mount_point
):
269 self
.mount_point
= mount_point
272 return _("<overlay %(src)s on %(mount_point)s>") % {'src': self
.src
or '.', 'mount_point': self
.mount_point
or '/'}
276 def _toxml(self
, doc
):
277 """Create a DOM element for this binding.
278 @param doc: document to use to create the element
279 @return: the new element
281 env_elem
= doc
.createElementNS(XMLNS_IFACE
, 'overlay')
282 if self
.src
is not None:
283 env_elem
.setAttributeNS(None, 'src', self
.src
)
284 if self
.mount_point
is not None:
285 env_elem
.setAttributeNS(None, 'mount-point', self
.mount_point
)
289 """An interface's feeds are other interfaces whose implementations can also be
290 used as implementations of this interface."""
291 __slots__
= ['uri', 'os', 'machine', 'user_override', 'langs']
292 def __init__(self
, uri
, arch
, user_override
, langs
= None):
294 # This indicates whether the feed comes from the user's overrides
295 # file. If true, writer.py will write it when saving.
296 self
.user_override
= user_override
297 self
.os
, self
.machine
= _split_arch(arch
)
301 return "<Feed from %s>" % self
.uri
304 arch
= property(lambda self
: _join_arch(self
.os
, self
.machine
))
306 class Dependency(object):
307 """A Dependency indicates that an Implementation requires some additional
308 code to function. This is an abstract base class.
309 @ivar metadata: any extra attributes from the XML element
310 @type metadata: {str: str}
312 __slots__
= ['metadata']
314 def __init__(self
, metadata
):
318 assert not isinstance(metadata
, basestring
) # Use InterfaceDependency instead!
319 self
.metadata
= metadata
321 class InterfaceDependency(Dependency
):
322 """A Dependency on a Zero Install interface.
323 @ivar interface: the interface required by this dependency
325 @ivar restrictions: a list of constraints on acceptable implementations
326 @type restrictions: [L{Restriction}]
327 @ivar bindings: how to make the choice of implementation known
328 @type bindings: [L{Binding}]
331 __slots__
= ['interface', 'restrictions', 'bindings', 'metadata']
333 def __init__(self
, interface
, restrictions
= None, metadata
= None):
334 Dependency
.__init
__(self
, metadata
)
335 assert isinstance(interface
, (str, unicode))
337 self
.interface
= interface
338 if restrictions
is None:
339 self
.restrictions
= []
341 self
.restrictions
= restrictions
345 return _("<Dependency on %(interface)s; bindings: %(bindings)s%(restrictions)s>") % {'interface': self
.interface
, 'bindings': self
.bindings
, 'restrictions': self
.restrictions
}
347 class RetrievalMethod(object):
348 """A RetrievalMethod provides a way to fetch an implementation."""
351 class DownloadSource(RetrievalMethod
):
352 """A DownloadSource provides a way to fetch an implementation."""
353 __slots__
= ['implementation', 'url', 'size', 'extract', 'start_offset', 'type']
355 def __init__(self
, implementation
, url
, size
, extract
, start_offset
= 0, type = None):
356 self
.implementation
= implementation
359 self
.extract
= extract
360 self
.start_offset
= start_offset
361 self
.type = type # MIME type - see unpack.py
363 class Recipe(RetrievalMethod
):
364 """Get an implementation by following a series of steps.
365 @ivar size: the combined download sizes from all the steps
367 @ivar steps: the sequence of steps which must be performed
368 @type steps: [L{RetrievalMethod}]"""
369 __slots__
= ['steps']
374 size
= property(lambda self
: sum([x
.size
for x
in self
.steps
]))
376 class DistributionSource(RetrievalMethod
):
377 """A package that is installed using the distribution's tools (including PackageKit).
378 @ivar package_id: the package name, in a form recognised by the distribution's tools
379 @type package_id: str
380 @ivar size: the download size in bytes
383 __slots__
= ['package_id', 'size']
385 def __init__(self
, package_id
, size
):
386 RetrievalMethod
.__init
__(self
)
387 self
.package_id
= package_id
390 class Implementation(object):
391 """An Implementation is a package which implements an Interface.
392 @ivar download_sources: list of methods of getting this implementation
393 @type download_sources: [L{RetrievalMethod}]
394 @ivar feed: the feed owning this implementation (since 0.32)
395 @type feed: [L{ZeroInstallFeed}]
396 @ivar bindings: how to tell this component where it itself is located (since 0.31)
397 @type bindings: [Binding]
398 @ivar upstream_stability: the stability reported by the packager
399 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged]
400 @ivar user_stability: the stability as set by the user
401 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged | preferred]
402 @ivar langs: natural languages supported by this package
404 @ivar requires: interfaces this package depends on
405 @type requires: [L{Dependency}]
406 @ivar main: the default file to execute when running as a program
407 @ivar metadata: extra metadata from the feed
408 @type metadata: {"[URI ]localName": str}
409 @ivar id: a unique identifier for this Implementation
410 @ivar version: a parsed version number
411 @ivar released: release date
412 @ivar local_path: the directory containing this local implementation, or None if it isn't local (id isn't a path)
413 @type local_path: str | None
414 @ivar requires_root_install: whether the user will need admin rights to use this
415 @type requires_root_install: bool
418 # Note: user_stability shouldn't really be here
420 __slots__
= ['upstream_stability', 'user_stability', 'langs',
421 'requires', 'main', 'metadata', 'download_sources',
422 'id', 'feed', 'version', 'released', 'bindings', 'machine']
424 def __init__(self
, feed
, id):
429 self
.user_stability
= None
430 self
.upstream_stability
= None
431 self
.metadata
= {} # [URI + " "] + localName -> value
435 self
.download_sources
= []
440 def get_stability(self
):
441 return self
.user_stability
or self
.upstream_stability
or testing
447 return "v%s (%s)" % (self
.get_version(), self
.id)
449 def __cmp__(self
, other
):
450 """Newer versions come first"""
451 d
= cmp(other
.version
, self
.version
)
453 # If the version number is the same, just give a stable sort order, and
454 # ensure that two different implementations don't compare equal.
455 d
= cmp(other
.feed
.url
, self
.feed
.url
)
457 return cmp(other
.id, self
.id)
459 def get_version(self
):
460 """Return the version as a string.
461 @see: L{format_version}
463 return format_version(self
.version
)
465 arch
= property(lambda self
: _join_arch(self
.os
, self
.machine
))
470 requires_root_install
= False
472 class DistributionImplementation(Implementation
):
473 """An implementation provided by the distribution. Information such as the version
474 comes from the package manager.
477 def __init__(self
, feed
, id, distro
):
478 assert id.startswith('package:')
479 Implementation
.__init
__(self
, feed
, id)
483 def requires_root_install(self
):
484 return not self
.installed
488 return self
.distro
.get_installed(self
.id)
490 class ZeroInstallImplementation(Implementation
):
491 """An implementation where all the information comes from Zero Install.
492 @ivar digests: a list of "algorith=value" strings (since 0.45)
495 __slots__
= ['os', 'size', 'digests', 'local_path']
497 def __init__(self
, feed
, id, local_path
):
498 """id can be a local path (string starting with /) or a manifest hash (eg "sha1=XXX")"""
499 assert not id.startswith('package:'), id
500 Implementation
.__init
__(self
, feed
, id)
504 self
.local_path
= local_path
507 dependencies
= property(lambda self
: dict([(x
.interface
, x
) for x
in self
.requires
508 if isinstance(x
, InterfaceDependency
)]))
510 def add_download_source(self
, url
, size
, extract
, start_offset
= 0, type = None):
511 """Add a download source."""
512 self
.download_sources
.append(DownloadSource(self
, url
, size
, extract
, start_offset
, type))
514 def set_arch(self
, arch
):
515 self
.os
, self
.machine
= _split_arch(arch
)
516 arch
= property(lambda self
: _join_arch(self
.os
, self
.machine
), set_arch
)
518 class Interface(object):
519 """An Interface represents some contract of behaviour.
520 @ivar uri: the URI for this interface.
521 @ivar stability_policy: user's configured policy.
522 Implementations at this level or higher are preferred.
523 Lower levels are used only if there is no other choice.
525 __slots__
= ['uri', 'stability_policy', 'extra_feeds']
527 implementations
= property(lambda self
: self
._main
_feed
.implementations
)
528 name
= property(lambda self
: self
._main
_feed
.name
)
529 description
= property(lambda self
: self
._main
_feed
.description
)
530 summary
= property(lambda self
: self
._main
_feed
.summary
)
531 last_modified
= property(lambda self
: self
._main
_feed
.last_modified
)
532 feeds
= property(lambda self
: self
.extra_feeds
+ self
._main
_feed
.feeds
)
533 metadata
= property(lambda self
: self
._main
_feed
.metadata
)
535 last_checked
= property(lambda self
: self
._main
_feed
.last_checked
)
537 def __init__(self
, uri
):
539 if uri
.startswith('http:') or uri
.startswith('https:') or os
.path
.isabs(uri
):
542 raise SafeException(_("Interface name '%s' doesn't start "
543 "with 'http:' or 'https:'") % uri
)
546 def _get_feed_for(self
):
548 for key
in self
._main
_feed
.feed_for
:
551 feed_for
= property(_get_feed_for
) # Deprecated (used by 0publish)
554 self
.extra_feeds
= []
555 self
.stability_policy
= None
558 from zeroinstall
.injector
.iface_cache
import iface_cache
559 feed
= iface_cache
.get_feed(self
.uri
)
561 return feed
.get_name()
562 return '(' + os
.path
.basename(self
.uri
) + ')'
565 return _("<Interface %s>") % self
.uri
567 def set_stability_policy(self
, new
):
568 assert new
is None or isinstance(new
, Stability
)
569 self
.stability_policy
= new
571 def get_feed(self
, url
):
573 #warnings.warn("use iface_cache.get_feed instead", DeprecationWarning, 2)
574 for x
in self
.extra_feeds
:
577 #return self._main_feed.get_feed(url)
580 def get_metadata(self
, uri
, name
):
581 return self
._main
_feed
.get_metadata(uri
, name
)
584 def _main_feed(self
):
586 #warnings.warn("use the feed instead", DeprecationWarning, 3)
587 from zeroinstall
.injector
.iface_cache
import iface_cache
588 feed
= iface_cache
.get_feed(self
.uri
)
593 def _merge_attrs(attrs
, item
):
594 """Add each attribute of item to a copy of attrs and return the copy.
595 @type attrs: {str: str}
596 @type item: L{qdom.Element}
601 new
[str(a
)] = item
.attrs
[a
]
604 def _get_long(elem
, attr_name
):
605 val
= elem
.getAttribute(attr_name
)
609 except ValueError, ex
:
610 raise SafeException(_("Invalid value for integer attribute '%(attribute_name)s': %(value)s") % {'attribute_name': attr_name
, 'value': val
})
613 class ZeroInstallFeed(object):
614 """A feed lists available implementations of an interface.
615 @ivar url: the URL for this feed
616 @ivar implementations: Implementations in this feed, indexed by ID
617 @type implementations: {str: L{Implementation}}
618 @ivar name: human-friendly name
619 @ivar summaries: short textual description (in various languages, since 0.49)
620 @type summaries: {str: str}
621 @ivar descriptions: long textual description (in various languages, since 0.49)
622 @type descriptions: {str: str}
623 @ivar last_modified: timestamp on signature
624 @ivar last_checked: time feed was last successfully downloaded and updated
625 @ivar feeds: list of <feed> elements in this feed
626 @type feeds: [L{Feed}]
627 @ivar feed_for: interfaces for which this could be a feed
628 @type feed_for: set(str)
629 @ivar metadata: extra elements we didn't understand
631 # _main is deprecated
632 __slots__
= ['url', 'implementations', 'name', 'descriptions', 'summaries',
633 'last_checked', 'last_modified', 'feeds', 'feed_for', 'metadata']
635 def __init__(self
, feed_element
, local_path
= None, distro
= None):
636 """Create a feed object from a DOM.
637 @param feed_element: the root element of a feed file
638 @type feed_element: L{qdom.Element}
639 @param local_path: the pathname of this local feed, or None for remote feeds
640 @param distro: used to resolve distribution package references
641 @type distro: L{distro.Distribution} or None"""
643 self
.implementations
= {}
645 self
.summaries
= {} # { lang: str }
646 self
.descriptions
= {} # { lang: str }
647 self
.last_modified
= None
649 self
.feed_for
= set()
651 self
.last_checked
= None
653 assert feed_element
.name
in ('interface', 'feed'), "Root element should be <interface>, not %s" % feed_element
654 assert feed_element
.uri
== XMLNS_IFACE
, "Wrong namespace on root element: %s" % feed_element
.uri
656 main
= feed_element
.getAttribute('main')
657 #if main: warn("Setting 'main' on the root element is deprecated. Put it on a <group> instead")
660 self
.url
= local_path
661 local_dir
= os
.path
.dirname(local_path
)
663 self
.url
= feed_element
.getAttribute('uri')
665 raise InvalidInterface(_("<interface> uri attribute missing"))
666 local_dir
= None # Can't have relative paths
668 min_injector_version
= feed_element
.getAttribute('min-injector-version')
669 if min_injector_version
:
670 if parse_version(min_injector_version
) > parse_version(version
):
671 raise InvalidInterface(_("This feed requires version %(min_version)s or later of "
672 "Zero Install, but I am only version %(version)s. "
673 "You can get a newer version from http://0install.net") %
674 {'min_version': min_injector_version
, 'version': version
})
676 for x
in feed_element
.childNodes
:
677 if x
.uri
!= XMLNS_IFACE
:
678 self
.metadata
.append(x
)
681 self
.name
= x
.content
682 elif x
.name
== 'description':
683 self
.descriptions
[x
.attrs
.get("http://www.w3.org/XML/1998/namespace lang", None)] = x
.content
684 elif x
.name
== 'summary':
685 self
.summaries
[x
.attrs
.get("http://www.w3.org/XML/1998/namespace lang", None)] = x
.content
686 elif x
.name
== 'feed-for':
687 feed_iface
= x
.getAttribute('interface')
689 raise InvalidInterface(_('Missing "interface" attribute in <feed-for>'))
690 self
.feed_for
.add(feed_iface
)
691 # Bug report from a Debian/stable user that --feed gets the wrong value.
692 # Can't reproduce (even in a Debian/stable chroot), but add some logging here
693 # in case it happens again.
694 debug(_("Is feed-for %s"), feed_iface
)
695 elif x
.name
== 'feed':
696 feed_src
= x
.getAttribute('src')
698 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
699 if feed_src
.startswith('http:') or feed_src
.startswith('https:') or local_path
:
700 self
.feeds
.append(Feed(feed_src
, x
.getAttribute('arch'), False, langs
= x
.getAttribute('langs')))
702 raise InvalidInterface(_("Invalid feed URL '%s'") % feed_src
)
704 self
.metadata
.append(x
)
707 raise InvalidInterface(_("Missing <name> in feed"))
709 raise InvalidInterface(_("Missing <summary> in feed"))
711 package_impls
= [0, []] # Best score so far and packages with that score
713 def process_group(group
, group_attrs
, base_depends
, base_bindings
):
714 for item
in group
.childNodes
:
715 if item
.uri
!= XMLNS_IFACE
: continue
717 if item
.name
not in ('group', 'implementation', 'package-implementation'):
720 depends
= base_depends
[:]
721 bindings
= base_bindings
[:]
723 item_attrs
= _merge_attrs(group_attrs
, item
)
725 # We've found a group or implementation. Scan for dependencies
726 # and bindings. Doing this here means that:
727 # - We can share the code for groups and implementations here.
728 # - The order doesn't matter, because these get processed first.
729 # A side-effect is that the document root cannot contain
731 for child
in item
.childNodes
:
732 if child
.uri
!= XMLNS_IFACE
: continue
733 if child
.name
== 'requires':
734 dep
= process_depends(child
)
736 elif child
.name
in binding_names
:
737 bindings
.append(process_binding(child
))
739 if item
.name
== 'group':
740 process_group(item
, item_attrs
, depends
, bindings
)
741 elif item
.name
== 'implementation':
742 process_impl(item
, item_attrs
, depends
, bindings
)
743 elif item
.name
== 'package-implementation':
744 distro_names
= item_attrs
.get('distributions', '')
745 for distro_name
in distro_names
.split(' '):
746 score
= distro
.get_score(distro_name
)
747 if score
> package_impls
[0]:
748 package_impls
[0] = score
749 package_impls
[1] = []
750 if score
== package_impls
[0]:
751 package_impls
[1].append((item
, item_attrs
, depends
))
755 def process_impl(item
, item_attrs
, depends
, bindings
):
756 id = item
.getAttribute('id')
758 raise InvalidInterface(_("Missing 'id' attribute on %s") % item
)
759 local_path
= item_attrs
.get('local-path')
760 if local_dir
and local_path
:
761 abs_local_path
= os
.path
.abspath(os
.path
.join(local_dir
, local_path
))
762 impl
= ZeroInstallImplementation(self
, id, abs_local_path
)
763 elif local_dir
and (id.startswith('/') or id.startswith('.')):
765 id = os
.path
.abspath(os
.path
.join(local_dir
, id))
766 impl
= ZeroInstallImplementation(self
, id, id)
768 impl
= ZeroInstallImplementation(self
, id, None)
770 # In older feeds, the ID was the (single) digest
771 impl
.digests
.append(id)
772 if id in self
.implementations
:
773 warn(_("Duplicate ID '%(id)s' in feed '%(feed)s'"), {'id': id, 'feed': self
})
774 self
.implementations
[id] = impl
776 impl
.metadata
= item_attrs
778 version_mod
= item_attrs
.get('version-modifier', None)
780 item_attrs
['version'] += version_mod
781 del item_attrs
['version-modifier']
782 version
= item_attrs
['version']
784 raise InvalidInterface(_("Missing version attribute"))
785 impl
.version
= parse_version(version
)
787 item_main
= item_attrs
.get('main', None)
788 if item_main
and item_main
.startswith('/'):
789 raise InvalidInterface(_("'main' attribute must be relative, but '%s' starts with '/'!") %
791 impl
.main
= item_main
793 impl
.released
= item_attrs
.get('released', None)
794 impl
.langs
= item_attrs
.get('langs', '')
796 size
= item
.getAttribute('size')
798 impl
.size
= long(size
)
799 impl
.arch
= item_attrs
.get('arch', None)
801 stability
= stability_levels
[str(item_attrs
['stability'])]
803 stab
= str(item_attrs
['stability'])
804 if stab
!= stab
.lower():
805 raise InvalidInterface(_('Stability "%s" invalid - use lower case!') % item_attrs
.stability
)
806 raise InvalidInterface(_('Stability "%s" invalid') % item_attrs
['stability'])
807 if stability
>= preferred
:
808 raise InvalidInterface(_("Upstream can't set stability to preferred!"))
809 impl
.upstream_stability
= stability
811 impl
.bindings
= bindings
812 impl
.requires
= depends
814 for elem
in item
.childNodes
:
815 if elem
.uri
!= XMLNS_IFACE
: continue
816 if elem
.name
== 'archive':
817 url
= elem
.getAttribute('href')
819 raise InvalidInterface(_("Missing href attribute on <archive>"))
820 size
= elem
.getAttribute('size')
822 raise InvalidInterface(_("Missing size attribute on <archive>"))
823 impl
.add_download_source(url
= url
, size
= long(size
),
824 extract
= elem
.getAttribute('extract'),
825 start_offset
= _get_long(elem
, 'start-offset'),
826 type = elem
.getAttribute('type'))
827 elif elem
.name
== 'manifest-digest':
828 for aname
, avalue
in elem
.attrs
.iteritems():
830 impl
.digests
.append('%s=%s' % (aname
, avalue
))
831 elif elem
.name
== 'recipe':
833 for recipe_step
in elem
.childNodes
:
834 if recipe_step
.uri
== XMLNS_IFACE
and recipe_step
.name
== 'archive':
835 url
= recipe_step
.getAttribute('href')
837 raise InvalidInterface(_("Missing href attribute on <archive>"))
838 size
= recipe_step
.getAttribute('size')
840 raise InvalidInterface(_("Missing size attribute on <archive>"))
841 recipe
.steps
.append(DownloadSource(None, url
= url
, size
= long(size
),
842 extract
= recipe_step
.getAttribute('extract'),
843 start_offset
= _get_long(recipe_step
, 'start-offset'),
844 type = recipe_step
.getAttribute('type')))
846 info(_("Unknown step '%s' in recipe; skipping recipe"), recipe_step
.name
)
849 impl
.download_sources
.append(recipe
)
851 def process_native_impl(item
, item_attrs
, depends
):
852 package
= item_attrs
.get('package', None)
854 raise InvalidInterface(_("Missing 'package' attribute on %s") % item
)
857 assert id.startswith('package:')
858 if id in self
.implementations
:
859 warn(_("Duplicate ID '%s' for DistributionImplementation"), id)
860 impl
= DistributionImplementation(self
, id, distro
)
861 self
.implementations
[id] = impl
863 impl
.metadata
= item_attrs
865 item_main
= item_attrs
.get('main', None)
866 if item_main
and not item_main
.startswith('/'):
867 raise InvalidInterface(_("'main' attribute must be absolute, but '%s' doesn't start with '/'!") %
869 impl
.main
= item_main
870 impl
.upstream_stability
= packaged
871 impl
.requires
= depends
875 distro
.get_package_info(package
, factory
)
877 root_attrs
= {'stability': 'testing'}
879 root_attrs
['main'] = main
880 process_group(feed_element
, root_attrs
, [], [])
882 for args
in package_impls
[1]:
883 process_native_impl(*args
)
886 return self
.name
or '(' + os
.path
.basename(self
.url
) + ')'
889 return _("<Feed %s>") % self
.url
892 def _get_impl(self
, id):
893 assert id not in self
.implementations
895 if id.startswith('.') or id.startswith('/'):
896 id = os
.path
.abspath(os
.path
.join(self
.url
, id))
898 impl
= ZeroInstallImplementation(self
, id, local_path
)
900 impl
= ZeroInstallImplementation(self
, id, None)
901 impl
.digests
.append(id)
903 self
.implementations
[id] = impl
906 def set_stability_policy(self
, new
):
907 assert new
is None or isinstance(new
, Stability
)
908 self
.stability_policy
= new
910 def get_feed(self
, url
):
916 def add_metadata(self
, elem
):
917 self
.metadata
.append(elem
)
919 def get_metadata(self
, uri
, name
):
920 """Return a list of interface metadata elements with this name and namespace URI."""
921 return [m
for m
in self
.metadata
if m
.name
== name
and m
.uri
== uri
]
925 return _best_language_match(self
.summaries
)
928 def description(self
):
929 return _best_language_match(self
.descriptions
)
931 class DummyFeed(object):
932 """Temporary class used during API transition."""
935 last_checked
= property(lambda self
: None)
936 implementations
= property(lambda self
: {})
937 feeds
= property(lambda self
: [])
938 summary
= property(lambda self
: '-')
939 description
= property(lambda self
: '')
940 def get_name(self
): return self
.name
941 def get_feed(self
, url
): return None
942 def get_metadata(self
, uri
, name
): return []
943 _dummy_feed
= DummyFeed()
946 """Convert each %20 to a space, etc.
948 uri
= uri
.replace('#', '/')
949 if '%' not in uri
: return uri
950 return re
.sub('%[0-9a-fA-F][0-9a-fA-F]',
951 lambda match
: chr(int(match
.group(0)[1:], 16)),
955 """Convert each space to %20, etc
957 return re
.sub('[^-_.a-zA-Z0-9]',
958 lambda match
: '%%%02x' % ord(match
.group(0)),
961 def _pretty_escape(uri
):
962 """Convert each space to %20, etc
963 : is preserved and / becomes #. This makes for nicer strings,
964 and may replace L{escape} everywhere in future.
966 return re
.sub('[^-_.a-zA-Z0-9:/]',
967 lambda match
: '%%%02x' % ord(match
.group(0)),
968 uri
.encode('utf-8')).replace('/', '#')
970 def canonical_iface_uri(uri
):
971 """If uri is a relative path, convert to an absolute one.
972 A "file:///foo" URI is converted to "/foo".
973 Otherwise, return it unmodified.
975 @raise SafeException: if uri isn't valid
977 if uri
.startswith('http://') or uri
.startswith('https://'):
978 if uri
.count("/") < 3:
979 raise SafeException(_("Missing / after hostname in URI '%s'") % uri
)
981 elif uri
.startswith('file:///'):
984 iface_uri
= os
.path
.realpath(uri
)
985 if os
.path
.isfile(iface_uri
):
987 raise SafeException(_("Bad interface name '%(uri)s'.\n"
988 "(doesn't start with 'http:', and "
989 "doesn't exist as a local file '%(interface_uri)s' either)") %
990 {'uri': uri
, 'interface_uri': iface_uri
})
992 _version_mod_to_value
= {
1000 _version_value_to_mod
= {}
1001 for x
in _version_mod_to_value
: _version_value_to_mod
[_version_mod_to_value
[x
]] = x
1004 _version_re
= re
.compile('-([a-z]*)')
1006 def parse_version(version_string
):
1007 """Convert a version string to an internal representation.
1008 The parsed format can be compared quickly using the standard Python functions.
1009 - Version := DottedList ("-" Mod DottedList?)*
1010 - DottedList := (Integer ("." Integer)*)
1011 @rtype: tuple (opaque)
1012 @raise SafeException: if the string isn't a valid version
1013 @since: 0.24 (moved from L{reader}, from where it is still available):"""
1014 if version_string
is None: return None
1015 parts
= _version_re
.split(version_string
)
1017 del parts
[-1] # Ends with a modifier
1021 raise SafeException(_("Empty version string!"))
1024 for x
in range(0, l
, 2):
1027 parts
[x
] = map(int, parts
[x
].split('.'))
1029 parts
[x
] = [] # (because ''.split('.') == [''], not [])
1030 for x
in range(1, l
, 2):
1031 parts
[x
] = _version_mod_to_value
[parts
[x
]]
1033 except ValueError, ex
:
1034 raise SafeException(_("Invalid version format in '%(version_string)s': %(exception)s") % {'version_string': version_string
, 'exception': ex
})
1035 except KeyError, ex
:
1036 raise SafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s") % {'version_string': version_string
, 'exception': ex
})
1038 def format_version(version
):
1039 """Format a parsed version for display. Undoes the effect of L{parse_version}.
1040 @see: L{Implementation.get_version}
1043 version
= version
[:]
1045 for x
in range(0, l
, 2):
1046 version
[x
] = '.'.join(map(str, version
[x
]))
1047 for x
in range(1, l
, 2):
1048 version
[x
] = '-' + _version_value_to_mod
[version
[x
]]
1049 if version
[-1] == '-': del version
[-1]
1050 return ''.join(version
)