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 _
17 import os
, re
, locale
, sys
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
22 from zeroinstall
import support
, zerostore
24 # Element names for bindings in feed files
25 binding_names
= frozenset(['environment', 'overlay', 'executable-in-path', 'executable-in-var'])
27 _dependency_names
= frozenset(['requires', 'restricts'])
29 network_offline
= 'off-line'
30 network_minimal
= 'minimal'
32 network_levels
= (network_offline
, network_minimal
, network_full
)
34 stability_levels
= {} # Name -> Stability
37 'PATH': '/bin:/usr/bin',
38 'XDG_CONFIG_DIRS': '/etc/xdg',
39 'XDG_DATA_DIRS': '/usr/local/share:/usr/share',
42 class InvalidInterface(SafeException
):
43 """Raised when parsing an invalid feed."""
46 def __init__(self
, message
, ex
= None):
49 message
+= "\n\n(exact error: %s)" % ex
51 # Some Python messages have type str but contain UTF-8 sequences.
52 # (e.g. IOException). Adding these to a Unicode 'message' (e.g.
53 # after gettext translation) will cause an error.
55 decoder
= codecs
.lookup('utf-8')
56 decex
= decoder
.decode(str(ex
), errors
= 'replace')[0]
57 message
+= "\n\n(exact error: %s)" % decex
59 SafeException
.__init
__(self
, message
)
61 def __unicode__(self
):
62 if hasattr(SafeException
, '__unicode__'):
65 return _('%s [%s]') % (SafeException
.__unicode
__(self
), self
.feed_url
)
66 return SafeException
.__unicode
__(self
)
68 return support
.unicode(SafeException
.__str
__(self
))
70 def _split_arch(arch
):
71 """Split an arch into an (os, machine) tuple. Either or both parts may be None."""
75 raise SafeException(_("Malformed arch '%s'") % arch
)
77 osys
, machine
= arch
.split('-', 1)
78 if osys
== '*': osys
= None
79 if machine
== '*': machine
= None
82 def _join_arch(osys
, machine
):
83 if osys
== machine
== None: return None
84 return "%s-%s" % (osys
or '*', machine
or '*')
86 def _best_language_match(options
):
87 (language
, encoding
) = locale
.getlocale()
90 # xml:lang uses '-', while LANG uses '_'
91 language
= language
.replace('_', '-')
95 return (options
.get(language
, None) or # Exact match (language+region)
96 options
.get(language
.split('-', 1)[0], None) or # Matching language
97 options
.get('en', None)) # English
99 class Stability(object):
100 """A stability rating. Each implementation has an upstream stability rating and,
101 optionally, a user-set rating."""
102 __slots__
= ['level', 'name', 'description']
103 def __init__(self
, level
, name
, description
):
106 self
.description
= description
107 assert name
not in stability_levels
108 stability_levels
[name
] = self
110 def __cmp__(self
, other
):
111 return cmp(self
.level
, other
.level
)
113 def __lt__(self
, other
):
114 if isinstance(other
, Stability
):
115 return self
.level
< other
.level
117 return NotImplemented
119 def __eq__(self
, other
):
120 if isinstance(other
, Stability
):
121 return self
.level
== other
.level
123 return NotImplemented
129 return _("<Stability: %s>") % self
.description
131 def process_binding(e
):
133 if e
.name
== 'environment':
135 None: EnvironmentBinding
.PREPEND
,
136 'prepend': EnvironmentBinding
.PREPEND
,
137 'append': EnvironmentBinding
.APPEND
,
138 'replace': EnvironmentBinding
.REPLACE
,
139 }[e
.getAttribute('mode')]
141 binding
= EnvironmentBinding(e
.getAttribute('name'),
142 insert
= e
.getAttribute('insert'),
143 default
= e
.getAttribute('default'),
144 value
= e
.getAttribute('value'),
146 separator
= e
.getAttribute('separator'))
147 if not binding
.name
: raise InvalidInterface(_("Missing 'name' in binding"))
148 if binding
.insert
is None and binding
.value
is None:
149 raise InvalidInterface(_("Missing 'insert' or 'value' in binding"))
150 if binding
.insert
is not None and binding
.value
is not None:
151 raise InvalidInterface(_("Binding contains both 'insert' and 'value'"))
153 elif e
.name
== 'executable-in-path':
154 return ExecutableBinding(e
, in_path
= True)
155 elif e
.name
== 'executable-in-var':
156 return ExecutableBinding(e
, in_path
= False)
157 elif e
.name
== 'overlay':
158 return OverlayBinding(e
.getAttribute('src'), e
.getAttribute('mount-point'))
160 raise Exception(_("Unknown binding type '%s'") % e
.name
)
162 def process_depends(item
, local_feed_dir
):
164 # Note: also called from selections
166 dep_iface
= item
.getAttribute('interface')
168 raise InvalidInterface(_("Missing 'interface' on <%s>") % item
.name
)
169 if dep_iface
.startswith('.'):
171 dep_iface
= os
.path
.abspath(os
.path
.join(local_feed_dir
, dep_iface
))
172 # (updates the element too, in case we write it out again)
173 attrs
['interface'] = dep_iface
175 raise InvalidInterface(_('Relative interface URI "%s" in non-local feed') % dep_iface
)
177 if item
.name
== 'restricts':
178 dependency
= InterfaceRestriction(dep_iface
, element
= item
)
180 dependency
= InterfaceDependency(dep_iface
, element
= item
)
182 for e
in item
.childNodes
:
183 if e
.uri
!= XMLNS_IFACE
: continue
184 if e
.name
in binding_names
:
185 dependency
.bindings
.append(process_binding(e
))
186 elif e
.name
== 'version':
187 dependency
.restrictions
.append(
188 VersionRangeRestriction(not_before
= parse_version(e
.getAttribute('not-before')),
189 before
= parse_version(e
.getAttribute('before'))))
192 def N_(message
): return message
194 insecure
= Stability(0, N_('insecure'), _('This is a security risk'))
195 buggy
= Stability(5, N_('buggy'), _('Known to have serious bugs'))
196 developer
= Stability(10, N_('developer'), _('Work-in-progress - bugs likely'))
197 testing
= Stability(20, N_('testing'), _('Stability unknown - please test!'))
198 stable
= Stability(30, N_('stable'), _('Tested - no serious problems found'))
199 packaged
= Stability(35, N_('packaged'), _('Supplied by the local package manager'))
200 preferred
= Stability(40, N_('preferred'), _('Best of all - must be set manually'))
204 class Restriction(object):
205 """A Restriction limits the allowed implementations of an Interface."""
208 def meets_restriction(self
, impl
):
209 """Called by the L{solver.Solver} to check whether a particular implementation is acceptable.
210 @return: False if this implementation is not a possibility
213 raise NotImplementedError(_("Abstract"))
215 class VersionRestriction(Restriction
):
216 """Only select implementations with a particular version number.
219 def __init__(self
, version
):
220 """@param version: the required version number
221 @see: L{parse_version}; use this to pre-process the version number
223 self
.version
= version
225 def meets_restriction(self
, impl
):
226 return impl
.version
== self
.version
229 return _("(restriction: version = %s)") % format_version(self
.version
)
231 class VersionRangeRestriction(Restriction
):
232 """Only versions within the given range are acceptable"""
233 __slots__
= ['before', 'not_before']
235 def __init__(self
, before
, not_before
):
236 """@param before: chosen versions must be earlier than this
237 @param not_before: versions must be at least this high
238 @see: L{parse_version}; use this to pre-process the versions
241 self
.not_before
= not_before
243 def meets_restriction(self
, impl
):
244 if self
.not_before
and impl
.version
< self
.not_before
:
246 if self
.before
and impl
.version
>= self
.before
:
251 if self
.not_before
is not None or self
.before
is not None:
253 if self
.not_before
is not None:
254 range += format_version(self
.not_before
) + ' <= '
256 if self
.before
is not None:
257 range += ' < ' + format_version(self
.before
)
260 return _("(restriction: %s)") % range
262 class Binding(object):
263 """Information about how the choice of a Dependency is made known
264 to the application being run."""
268 """"Returns the name of the specific command needed by this binding, if any.
272 class EnvironmentBinding(Binding
):
273 """Indicate the chosen implementation using an environment variable."""
274 __slots__
= ['name', 'insert', 'default', 'mode', 'value']
280 def __init__(self
, name
, insert
, default
= None, mode
= PREPEND
, value
=None, separator
=None):
282 mode argument added in version 0.28
283 value argument added in version 0.52
287 self
.default
= default
290 if separator
is None:
291 self
.separator
= os
.pathsep
293 self
.separator
= separator
297 return _("<environ %(name)s %(mode)s %(insert)s %(value)s>") % \
298 {'name': self
.name
, 'mode': self
.mode
, 'insert': self
.insert
, 'value': self
.value
}
302 def get_value(self
, path
, old_value
):
303 """Calculate the new value of the environment variable after applying this binding.
304 @param path: the path to the selected implementation
305 @param old_value: the current value of the environment variable
306 @return: the new value for the environment variable"""
308 if self
.insert
is not None:
309 extra
= os
.path
.join(path
, self
.insert
)
311 assert self
.value
is not None
314 if self
.mode
== EnvironmentBinding
.REPLACE
:
317 if old_value
is None:
318 old_value
= self
.default
or defaults
.get(self
.name
, None)
319 if old_value
is None:
321 if self
.mode
== EnvironmentBinding
.PREPEND
:
322 return extra
+ self
.separator
+ old_value
324 return old_value
+ self
.separator
+ extra
326 def _toxml(self
, doc
, prefixes
):
327 """Create a DOM element for this binding.
328 @param doc: document to use to create the element
329 @return: the new element
331 env_elem
= doc
.createElementNS(XMLNS_IFACE
, 'environment')
332 env_elem
.setAttributeNS(None, 'name', self
.name
)
333 if self
.mode
is not None:
334 env_elem
.setAttributeNS(None, 'mode', self
.mode
)
335 if self
.insert
is not None:
336 env_elem
.setAttributeNS(None, 'insert', self
.insert
)
338 env_elem
.setAttributeNS(None, 'value', self
.value
)
340 env_elem
.setAttributeNS(None, 'default', self
.default
)
342 env_elem
.setAttributeNS(None, 'separator', self
.separator
)
345 class ExecutableBinding(Binding
):
346 """Make the chosen command available in $PATH.
347 @ivar in_path: True to add the named command to $PATH, False to store in named variable
352 def __init__(self
, qdom
, in_path
):
354 self
.in_path
= in_path
357 return str(self
.qdom
)
361 def _toxml(self
, doc
, prefixes
):
362 return self
.qdom
.toDOM(doc
, prefixes
)
366 return self
.qdom
.getAttribute('name')
370 return self
.qdom
.getAttribute("command") or 'run'
372 class OverlayBinding(Binding
):
373 """Make the chosen implementation available by overlaying it onto another part of the file-system.
374 This is to support legacy programs which use hard-coded paths."""
375 __slots__
= ['src', 'mount_point']
377 def __init__(self
, src
, mount_point
):
379 self
.mount_point
= mount_point
382 return _("<overlay %(src)s on %(mount_point)s>") % {'src': self
.src
or '.', 'mount_point': self
.mount_point
or '/'}
386 def _toxml(self
, doc
, prefixes
):
387 """Create a DOM element for this binding.
388 @param doc: document to use to create the element
389 @return: the new element
391 env_elem
= doc
.createElementNS(XMLNS_IFACE
, 'overlay')
392 if self
.src
is not None:
393 env_elem
.setAttributeNS(None, 'src', self
.src
)
394 if self
.mount_point
is not None:
395 env_elem
.setAttributeNS(None, 'mount-point', self
.mount_point
)
399 """An interface's feeds are other interfaces whose implementations can also be
400 used as implementations of this interface."""
401 __slots__
= ['uri', 'os', 'machine', 'user_override', 'langs', 'site_package']
402 def __init__(self
, uri
, arch
, user_override
, langs
= None, site_package
= False):
404 # This indicates whether the feed comes from the user's overrides
405 # file. If true, writer.py will write it when saving.
406 self
.user_override
= user_override
407 self
.os
, self
.machine
= _split_arch(arch
)
409 self
.site_package
= site_package
412 return "<Feed from %s>" % self
.uri
415 arch
= property(lambda self
: _join_arch(self
.os
, self
.machine
))
417 class Dependency(object):
418 """A Dependency indicates that an Implementation requires some additional
419 code to function. This is an abstract base class.
420 @ivar qdom: the XML element for this Dependency (since 0launch 0.51)
421 @type qdom: L{qdom.Element}
422 @ivar metadata: any extra attributes from the XML element
423 @type metadata: {str: str}
427 Essential
= "essential" # Must select a version of the dependency
428 Recommended
= "recommended" # Prefer to select a version
429 Restricts
= "restricts" # Just adds restrictions without expressing any opinion
431 def __init__(self
, element
):
432 assert isinstance(element
, qdom
.Element
), type(element
) # Use InterfaceDependency instead!
437 return self
.qdom
.attrs
439 def get_required_commands(self
):
440 """Return a list of command names needed by this dependency"""
443 class InterfaceRestriction(Dependency
):
444 """A Dependency that restricts the possible choices of a Zero Install interface.
445 @ivar interface: the interface required by this dependency
447 @ivar restrictions: a list of constraints on acceptable implementations
448 @type restrictions: [L{Restriction}]
451 __slots__
= ['interface', 'restrictions']
453 def __init__(self
, interface
, restrictions
= None, element
= None):
454 Dependency
.__init
__(self
, element
)
455 assert isinstance(interface
, (str, support
.unicode))
457 self
.interface
= interface
458 if restrictions
is None:
459 self
.restrictions
= []
461 self
.restrictions
= restrictions
463 importance
= Dependency
.Restricts
467 return _("<Restriction on %(interface)s; %(restrictions)s>") % {'interface': self
.interface
, 'restrictions': self
.restrictions
}
469 class InterfaceDependency(InterfaceRestriction
):
470 """A Dependency on a Zero Install interface.
471 @ivar interface: the interface required by this dependency
473 @ivar restrictions: a list of constraints on acceptable implementations
474 @type restrictions: [L{Restriction}]
475 @ivar bindings: how to make the choice of implementation known
476 @type bindings: [L{Binding}]
479 __slots__
= ['bindings']
481 def __init__(self
, interface
, restrictions
= None, element
= None):
482 InterfaceRestriction
.__init
__(self
, interface
, restrictions
, element
)
486 return _("<Dependency on %(interface)s; bindings: %(bindings)s%(restrictions)s>") % {'interface': self
.interface
, 'bindings': self
.bindings
, 'restrictions': self
.restrictions
}
489 def importance(self
):
490 return self
.qdom
.getAttribute("importance") or Dependency
.Essential
492 def get_required_commands(self
):
493 """Return a list of command names needed by this dependency"""
494 if self
.qdom
.name
== 'runner':
495 commands
= [self
.qdom
.getAttribute('command') or 'run']
498 for b
in self
.bindings
:
506 if self
.qdom
.name
== 'runner':
507 return self
.qdom
.getAttribute('command') or 'run'
510 class RetrievalMethod(object):
511 """A RetrievalMethod provides a way to fetch an implementation."""
514 class DownloadSource(RetrievalMethod
):
515 """A DownloadSource provides a way to fetch an implementation."""
516 __slots__
= ['implementation', 'url', 'size', 'extract', 'start_offset', 'type']
518 def __init__(self
, implementation
, url
, size
, extract
, start_offset
= 0, type = None):
519 self
.implementation
= implementation
522 self
.extract
= extract
523 self
.start_offset
= start_offset
524 self
.type = type # MIME type - see unpack.py
526 class RenameStep(RetrievalMethod
):
527 """A Rename provides a way to rename / move a file within an implementation."""
528 __slots__
= ['source', 'dest']
530 def __init__(self
, source
, dest
):
534 class Recipe(RetrievalMethod
):
535 """Get an implementation by following a series of steps.
536 @ivar size: the combined download sizes from all the steps
538 @ivar steps: the sequence of steps which must be performed
539 @type steps: [L{RetrievalMethod}]"""
540 __slots__
= ['steps']
545 size
= property(lambda self
: sum([x
.size
for x
in self
.steps
if isinstance(x
, DownloadSource
)]))
547 class DistributionSource(RetrievalMethod
):
548 """A package that is installed using the distribution's tools (including PackageKit).
549 @ivar install: a function to call to install this package
550 @type install: (L{handler.Handler}) -> L{tasks.Blocker}
551 @ivar package_id: the package name, in a form recognised by the distribution's tools
552 @type package_id: str
553 @ivar size: the download size in bytes
555 @ivar needs_confirmation: whether the user should be asked to confirm before calling install()
556 @type needs_confirmation: bool"""
558 __slots__
= ['package_id', 'size', 'install', 'needs_confirmation']
560 def __init__(self
, package_id
, size
, install
, needs_confirmation
= True):
561 RetrievalMethod
.__init
__(self
)
562 self
.package_id
= package_id
564 self
.install
= install
565 self
.needs_confirmation
= needs_confirmation
567 class Command(object):
568 """A Command is a way of running an Implementation as a program."""
570 __slots__
= ['qdom', '_depends', '_local_dir', '_runner', '_bindings']
572 def __init__(self
, qdom
, local_dir
):
573 """@param qdom: the <command> element
574 @param local_dir: the directory containing the feed (for relative dependencies), or None if not local
576 assert qdom
.name
== 'command', 'not <command>: %s' % qdom
578 self
._local
_dir
= local_dir
580 self
._bindings
= None
582 path
= property(lambda self
: self
.qdom
.attrs
.get("path", None))
584 def _toxml(self
, doc
, prefixes
):
585 return self
.qdom
.toDOM(doc
, prefixes
)
589 if self
._depends
is None:
592 for child
in self
.qdom
.childNodes
:
593 if child
.name
in _dependency_names
:
594 dep
= process_depends(child
, self
._local
_dir
)
596 elif child
.name
== 'runner':
598 raise InvalidInterface(_("Multiple <runner>s in <command>!"))
599 dep
= process_depends(child
, self
._local
_dir
)
602 self
._depends
= depends
605 def get_runner(self
):
606 self
.requires
# (sets _runner)
610 return str(self
.qdom
)
615 if self
._bindings
is None:
617 for e
in self
.qdom
.childNodes
:
618 if e
.uri
!= XMLNS_IFACE
: continue
619 if e
.name
in binding_names
:
620 bindings
.append(process_binding(e
))
621 self
._bindings
= bindings
622 return self
._bindings
624 class Implementation(object):
625 """An Implementation is a package which implements an Interface.
626 @ivar download_sources: list of methods of getting this implementation
627 @type download_sources: [L{RetrievalMethod}]
628 @ivar feed: the feed owning this implementation (since 0.32)
629 @type feed: [L{ZeroInstallFeed}]
630 @ivar bindings: how to tell this component where it itself is located (since 0.31)
631 @type bindings: [Binding]
632 @ivar upstream_stability: the stability reported by the packager
633 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged]
634 @ivar user_stability: the stability as set by the user
635 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged | preferred]
636 @ivar langs: natural languages supported by this package
638 @ivar requires: interfaces this package depends on
639 @type requires: [L{Dependency}]
640 @ivar commands: ways to execute as a program
641 @type commands: {str: Command}
642 @ivar metadata: extra metadata from the feed
643 @type metadata: {"[URI ]localName": str}
644 @ivar id: a unique identifier for this Implementation
645 @ivar version: a parsed version number
646 @ivar released: release date
647 @ivar local_path: the directory containing this local implementation, or None if it isn't local (id isn't a path)
648 @type local_path: str | None
649 @ivar requires_root_install: whether the user will need admin rights to use this
650 @type requires_root_install: bool
653 # Note: user_stability shouldn't really be here
655 __slots__
= ['upstream_stability', 'user_stability', 'langs',
656 'requires', 'metadata', 'download_sources', 'commands',
657 'id', 'feed', 'version', 'released', 'bindings', 'machine']
659 def __init__(self
, feed
, id):
663 self
.user_stability
= None
664 self
.upstream_stability
= None
665 self
.metadata
= {} # [URI + " "] + localName -> value
669 self
.download_sources
= []
675 def get_stability(self
):
676 return self
.user_stability
or self
.upstream_stability
or testing
682 return "v%s (%s)" % (self
.get_version(), self
.id)
684 def __cmp__(self
, other
):
685 """Newer versions come first"""
686 d
= cmp(other
.version
, self
.version
)
688 # If the version number is the same, just give a stable sort order, and
689 # ensure that two different implementations don't compare equal.
690 d
= cmp(other
.feed
.url
, self
.feed
.url
)
692 return cmp(other
.id, self
.id)
695 return self
.id.__hash
__()
697 def __eq__(self
, other
):
700 def __le__(self
, other
):
701 if isinstance(other
, Implementation
):
702 if other
.version
< self
.version
: return True
703 elif other
.version
> self
.version
: return False
705 if other
.feed
.url
< self
.feed
.url
: return True
706 elif other
.feed
.url
> self
.feed
.url
: return False
708 return other
.id <= self
.id
710 return NotImplemented
712 def get_version(self
):
713 """Return the version as a string.
714 @see: L{format_version}
716 return format_version(self
.version
)
718 arch
= property(lambda self
: _join_arch(self
.os
, self
.machine
))
723 requires_root_install
= False
726 """"@deprecated: use commands["run"] instead"""
727 main
= self
.commands
.get("run", None)
731 def _set_main(self
, path
):
732 """"@deprecated: use commands["run"] instead"""
734 if "run" in self
.commands
:
735 del self
.commands
["run"]
737 self
.commands
["run"] = Command(qdom
.Element(XMLNS_IFACE
, 'command', {'path': path
, 'name': 'run'}), None)
738 main
= property(_get_main
, _set_main
)
740 def is_available(self
, stores
):
741 """Is this Implementation available locally?
742 (a local implementation, an installed distribution package, or a cached ZeroInstallImplementation)
746 raise NotImplementedError("abstract")
748 class DistributionImplementation(Implementation
):
749 """An implementation provided by the distribution. Information such as the version
750 comes from the package manager.
751 @ivar package_implementation: the <package-implementation> element that generated this impl (since 1.7)
752 @type package_implementation: L{qdom.Element}
754 __slots__
= ['distro', 'installed', 'package_implementation']
756 def __init__(self
, feed
, id, distro
, package_implementation
= None):
757 assert id.startswith('package:')
758 Implementation
.__init
__(self
, feed
, id)
760 self
.installed
= False
761 self
.package_implementation
= package_implementation
763 if package_implementation
:
764 for child
in package_implementation
.childNodes
:
765 if child
.uri
!= XMLNS_IFACE
: continue
766 if child
.name
== 'command':
767 command_name
= child
.attrs
.get('name', None)
769 raise InvalidInterface('Missing name for <command>')
770 self
.commands
[command_name
] = Command(child
, local_dir
= None)
773 def requires_root_install(self
):
774 return not self
.installed
776 def is_available(self
, stores
):
777 return self
.installed
779 class ZeroInstallImplementation(Implementation
):
780 """An implementation where all the information comes from Zero Install.
781 @ivar digests: a list of "algorith=value" or "algorith_value" strings (since 0.45)
784 __slots__
= ['os', 'size', 'digests', 'local_path']
786 def __init__(self
, feed
, id, local_path
):
787 """id can be a local path (string starting with /) or a manifest hash (eg "sha1=XXX")"""
788 assert not id.startswith('package:'), id
789 Implementation
.__init
__(self
, feed
, id)
793 self
.local_path
= local_path
796 dependencies
= property(lambda self
: dict([(x
.interface
, x
) for x
in self
.requires
797 if isinstance(x
, InterfaceRestriction
)]))
799 def add_download_source(self
, url
, size
, extract
, start_offset
= 0, type = None):
800 """Add a download source."""
801 self
.download_sources
.append(DownloadSource(self
, url
, size
, extract
, start_offset
, type))
803 def set_arch(self
, arch
):
804 self
.os
, self
.machine
= _split_arch(arch
)
805 arch
= property(lambda self
: _join_arch(self
.os
, self
.machine
), set_arch
)
807 def is_available(self
, stores
):
808 if self
.local_path
is not None:
809 return os
.path
.exists(self
.local_path
)
811 path
= stores
.lookup_maybe(self
.digests
)
812 return path
is not None
813 return False # (0compile creates fake entries with no digests)
815 class Interface(object):
816 """An Interface represents some contract of behaviour.
817 @ivar uri: the URI for this interface.
818 @ivar stability_policy: user's configured policy.
819 Implementations at this level or higher are preferred.
820 Lower levels are used only if there is no other choice.
822 __slots__
= ['uri', 'stability_policy', 'extra_feeds']
824 implementations
= property(lambda self
: self
._main
_feed
.implementations
)
825 name
= property(lambda self
: self
._main
_feed
.name
)
826 description
= property(lambda self
: self
._main
_feed
.description
)
827 summary
= property(lambda self
: self
._main
_feed
.summary
)
828 last_modified
= property(lambda self
: self
._main
_feed
.last_modified
)
829 feeds
= property(lambda self
: self
.extra_feeds
+ self
._main
_feed
.feeds
)
830 metadata
= property(lambda self
: self
._main
_feed
.metadata
)
832 last_checked
= property(lambda self
: self
._main
_feed
.last_checked
)
834 def __init__(self
, uri
):
836 if uri
.startswith('http:') or uri
.startswith('https:') or os
.path
.isabs(uri
):
839 raise SafeException(_("Interface name '%s' doesn't start "
840 "with 'http:' or 'https:'") % uri
)
843 def _get_feed_for(self
):
845 for key
in self
._main
_feed
.feed_for
:
848 feed_for
= property(_get_feed_for
) # Deprecated (used by 0publish)
851 self
.extra_feeds
= []
852 self
.stability_policy
= None
855 from zeroinstall
.injector
.iface_cache
import iface_cache
856 feed
= iface_cache
.get_feed(self
.uri
)
858 return feed
.get_name()
859 return '(' + os
.path
.basename(self
.uri
) + ')'
862 return _("<Interface %s>") % self
.uri
864 def set_stability_policy(self
, new
):
865 assert new
is None or isinstance(new
, Stability
)
866 self
.stability_policy
= new
868 def get_feed(self
, url
):
870 #warnings.warn("use iface_cache.get_feed instead", DeprecationWarning, 2)
871 for x
in self
.extra_feeds
:
874 #return self._main_feed.get_feed(url)
877 def get_metadata(self
, uri
, name
):
878 return self
._main
_feed
.get_metadata(uri
, name
)
881 def _main_feed(self
):
883 #warnings.warn("use the feed instead", DeprecationWarning, 3)
884 from zeroinstall
.injector
import policy
885 iface_cache
= policy
.get_deprecated_singleton_config().iface_cache
886 feed
= iface_cache
.get_feed(self
.uri
)
891 def _merge_attrs(attrs
, item
):
892 """Add each attribute of item to a copy of attrs and return the copy.
893 @type attrs: {str: str}
894 @type item: L{qdom.Element}
899 new
[str(a
)] = item
.attrs
[a
]
902 def _get_long(elem
, attr_name
):
903 val
= elem
.getAttribute(attr_name
)
908 raise SafeException(_("Invalid value for integer attribute '%(attribute_name)s': %(value)s") % {'attribute_name': attr_name
, 'value': val
})
911 class ZeroInstallFeed(object):
912 """A feed lists available implementations of an interface.
913 @ivar url: the URL for this feed
914 @ivar implementations: Implementations in this feed, indexed by ID
915 @type implementations: {str: L{Implementation}}
916 @ivar name: human-friendly name
917 @ivar summaries: short textual description (in various languages, since 0.49)
918 @type summaries: {str: str}
919 @ivar descriptions: long textual description (in various languages, since 0.49)
920 @type descriptions: {str: str}
921 @ivar last_modified: timestamp on signature
922 @ivar last_checked: time feed was last successfully downloaded and updated
923 @ivar local_path: the path of this local feed, or None if remote (since 1.7)
924 @type local_path: str | None
925 @ivar feeds: list of <feed> elements in this feed
926 @type feeds: [L{Feed}]
927 @ivar feed_for: interfaces for which this could be a feed
928 @type feed_for: set(str)
929 @ivar metadata: extra elements we didn't understand
931 # _main is deprecated
932 __slots__
= ['url', 'implementations', 'name', 'descriptions', 'first_description', 'summaries', 'first_summary', '_package_implementations',
933 'last_checked', 'last_modified', 'feeds', 'feed_for', 'metadata', 'local_path']
935 def __init__(self
, feed_element
, local_path
= None, distro
= None):
936 """Create a feed object from a DOM.
937 @param feed_element: the root element of a feed file
938 @type feed_element: L{qdom.Element}
939 @param local_path: the pathname of this local feed, or None for remote feeds"""
940 self
.local_path
= local_path
941 self
.implementations
= {}
943 self
.summaries
= {} # { lang: str }
944 self
.first_summary
= None
945 self
.descriptions
= {} # { lang: str }
946 self
.first_description
= None
947 self
.last_modified
= None
949 self
.feed_for
= set()
951 self
.last_checked
= None
952 self
._package
_implementations
= []
954 if distro
is not None:
956 warnings
.warn("distro argument is now ignored", DeprecationWarning, 2)
958 if feed_element
is None:
959 return # XXX subclass?
961 assert feed_element
.name
in ('interface', 'feed'), "Root element should be <interface>, not %s" % feed_element
962 assert feed_element
.uri
== XMLNS_IFACE
, "Wrong namespace on root element: %s" % feed_element
.uri
964 main
= feed_element
.getAttribute('main')
965 #if main: warn("Setting 'main' on the root element is deprecated. Put it on a <group> instead")
968 self
.url
= local_path
969 local_dir
= os
.path
.dirname(local_path
)
971 assert local_path
is None
972 self
.url
= feed_element
.getAttribute('uri')
974 raise InvalidInterface(_("<interface> uri attribute missing"))
975 local_dir
= None # Can't have relative paths
977 min_injector_version
= feed_element
.getAttribute('min-injector-version')
978 if min_injector_version
:
979 if parse_version(min_injector_version
) > parse_version(version
):
980 raise InvalidInterface(_("This feed requires version %(min_version)s or later of "
981 "Zero Install, but I am only version %(version)s. "
982 "You can get a newer version from http://0install.net") %
983 {'min_version': min_injector_version
, 'version': version
})
985 for x
in feed_element
.childNodes
:
986 if x
.uri
!= XMLNS_IFACE
:
987 self
.metadata
.append(x
)
990 self
.name
= x
.content
991 elif x
.name
== 'description':
992 if self
.first_description
== None:
993 self
.first_description
= x
.content
994 self
.descriptions
[x
.attrs
.get("http://www.w3.org/XML/1998/namespace lang", 'en')] = x
.content
995 elif x
.name
== 'summary':
996 if self
.first_summary
== None:
997 self
.first_summary
= x
.content
998 self
.summaries
[x
.attrs
.get("http://www.w3.org/XML/1998/namespace lang", 'en')] = x
.content
999 elif x
.name
== 'feed-for':
1000 feed_iface
= x
.getAttribute('interface')
1002 raise InvalidInterface(_('Missing "interface" attribute in <feed-for>'))
1003 self
.feed_for
.add(feed_iface
)
1004 # Bug report from a Debian/stable user that --feed gets the wrong value.
1005 # Can't reproduce (even in a Debian/stable chroot), but add some logging here
1006 # in case it happens again.
1007 debug(_("Is feed-for %s"), feed_iface
)
1008 elif x
.name
== 'feed':
1009 feed_src
= x
.getAttribute('src')
1011 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
1012 if feed_src
.startswith('http:') or feed_src
.startswith('https:') or local_path
:
1013 if feed_src
.startswith('.'):
1014 feed_src
= os
.path
.abspath(os
.path
.join(local_dir
, feed_src
))
1016 langs
= x
.getAttribute('langs')
1017 if langs
: langs
= langs
.replace('_', '-')
1018 self
.feeds
.append(Feed(feed_src
, x
.getAttribute('arch'), False, langs
= langs
))
1020 raise InvalidInterface(_("Invalid feed URL '%s'") % feed_src
)
1022 self
.metadata
.append(x
)
1025 raise InvalidInterface(_("Missing <name> in feed"))
1026 if not self
.summary
:
1027 raise InvalidInterface(_("Missing <summary> in feed"))
1029 def process_group(group
, group_attrs
, base_depends
, base_bindings
, base_commands
):
1030 for item
in group
.childNodes
:
1031 if item
.uri
!= XMLNS_IFACE
: continue
1033 if item
.name
not in ('group', 'implementation', 'package-implementation'):
1036 # We've found a group or implementation. Scan for dependencies,
1037 # bindings and commands. Doing this here means that:
1038 # - We can share the code for groups and implementations here.
1039 # - The order doesn't matter, because these get processed first.
1040 # A side-effect is that the document root cannot contain
1043 depends
= base_depends
[:]
1044 bindings
= base_bindings
[:]
1045 commands
= base_commands
.copy()
1047 for attr
, command
in [('main', 'run'),
1048 ('self-test', 'test')]:
1049 value
= item
.attrs
.get(attr
, None)
1050 if value
is not None:
1051 commands
[command
] = Command(qdom
.Element(XMLNS_IFACE
, 'command', {'name': command
, 'path': value
}), None)
1053 for child
in item
.childNodes
:
1054 if child
.uri
!= XMLNS_IFACE
: continue
1055 if child
.name
in _dependency_names
:
1056 dep
= process_depends(child
, local_dir
)
1058 elif child
.name
== 'command':
1059 command_name
= child
.attrs
.get('name', None)
1060 if not command_name
:
1061 raise InvalidInterface('Missing name for <command>')
1062 commands
[command_name
] = Command(child
, local_dir
)
1063 elif child
.name
in binding_names
:
1064 bindings
.append(process_binding(child
))
1066 compile_command
= item
.attrs
.get('http://zero-install.sourceforge.net/2006/namespaces/0compile command')
1067 if compile_command
is not None:
1068 commands
['compile'] = Command(qdom
.Element(XMLNS_IFACE
, 'command', {'name': 'compile', 'shell-command': compile_command
}), None)
1070 item_attrs
= _merge_attrs(group_attrs
, item
)
1072 if item
.name
== 'group':
1073 process_group(item
, item_attrs
, depends
, bindings
, commands
)
1074 elif item
.name
== 'implementation':
1075 process_impl(item
, item_attrs
, depends
, bindings
, commands
)
1076 elif item
.name
== 'package-implementation':
1078 warn("A <package-implementation> with dependencies in %s!", self
.url
)
1079 self
._package
_implementations
.append((item
, item_attrs
))
1083 def process_impl(item
, item_attrs
, depends
, bindings
, commands
):
1084 id = item
.getAttribute('id')
1086 raise InvalidInterface(_("Missing 'id' attribute on %s") % item
)
1087 local_path
= item_attrs
.get('local-path')
1088 if local_dir
and local_path
:
1089 abs_local_path
= os
.path
.abspath(os
.path
.join(local_dir
, local_path
))
1090 impl
= ZeroInstallImplementation(self
, id, abs_local_path
)
1091 elif local_dir
and (id.startswith('/') or id.startswith('.')):
1093 id = os
.path
.abspath(os
.path
.join(local_dir
, id))
1094 impl
= ZeroInstallImplementation(self
, id, id)
1096 impl
= ZeroInstallImplementation(self
, id, None)
1098 # In older feeds, the ID was the (single) digest
1099 impl
.digests
.append(id)
1100 if id in self
.implementations
:
1101 warn(_("Duplicate ID '%(id)s' in feed '%(feed)s'"), {'id': id, 'feed': self
})
1102 self
.implementations
[id] = impl
1104 impl
.metadata
= item_attrs
1106 version_mod
= item_attrs
.get('version-modifier', None)
1108 item_attrs
['version'] += version_mod
1109 del item_attrs
['version-modifier']
1110 version
= item_attrs
['version']
1112 raise InvalidInterface(_("Missing version attribute"))
1113 impl
.version
= parse_version(version
)
1115 impl
.commands
= commands
1117 impl
.released
= item_attrs
.get('released', None)
1118 impl
.langs
= item_attrs
.get('langs', '').replace('_', '-')
1120 size
= item
.getAttribute('size')
1122 impl
.size
= int(size
)
1123 impl
.arch
= item_attrs
.get('arch', None)
1125 stability
= stability_levels
[str(item_attrs
['stability'])]
1127 stab
= str(item_attrs
['stability'])
1128 if stab
!= stab
.lower():
1129 raise InvalidInterface(_('Stability "%s" invalid - use lower case!') % item_attrs
.stability
)
1130 raise InvalidInterface(_('Stability "%s" invalid') % item_attrs
['stability'])
1131 if stability
>= preferred
:
1132 raise InvalidInterface(_("Upstream can't set stability to preferred!"))
1133 impl
.upstream_stability
= stability
1135 impl
.bindings
= bindings
1136 impl
.requires
= depends
1138 for elem
in item
.childNodes
:
1139 if elem
.uri
!= XMLNS_IFACE
: continue
1140 if elem
.name
== 'archive':
1141 url
= elem
.getAttribute('href')
1143 raise InvalidInterface(_("Missing href attribute on <archive>"))
1144 size
= elem
.getAttribute('size')
1146 raise InvalidInterface(_("Missing size attribute on <archive>"))
1147 impl
.add_download_source(url
= url
, size
= int(size
),
1148 extract
= elem
.getAttribute('extract'),
1149 start_offset
= _get_long(elem
, 'start-offset'),
1150 type = elem
.getAttribute('type'))
1151 elif elem
.name
== 'manifest-digest':
1152 for aname
, avalue
in elem
.attrs
.items():
1153 if ' ' not in aname
:
1154 impl
.digests
.append(zerostore
.format_algorithm_digest_pair(aname
, avalue
))
1155 elif elem
.name
== 'recipe':
1157 for recipe_step
in elem
.childNodes
:
1158 if recipe_step
.uri
== XMLNS_IFACE
and recipe_step
.name
== 'archive':
1159 url
= recipe_step
.getAttribute('href')
1161 raise InvalidInterface(_("Missing href attribute on <archive>"))
1162 size
= recipe_step
.getAttribute('size')
1164 raise InvalidInterface(_("Missing size attribute on <archive>"))
1165 recipe
.steps
.append(DownloadSource(None, url
= url
, size
= int(size
),
1166 extract
= recipe_step
.getAttribute('extract'),
1167 start_offset
= _get_long(recipe_step
, 'start-offset'),
1168 type = recipe_step
.getAttribute('type')))
1169 elif recipe_step
.uri
== XMLNS_IFACE
and recipe_step
.name
== 'rename':
1170 source
= recipe_step
.getAttribute('source')
1172 raise InvalidInterface(_("Missing source attribute on <rename>"))
1173 dest
= recipe_step
.getAttribute('dest')
1175 raise InvalidInterface(_("Missing dest attribute on <rename>"))
1176 recipe
.steps
.append(RenameStep(source
=source
, dest
=dest
))
1178 info(_("Unknown step '%s' in recipe; skipping recipe"), recipe_step
.name
)
1181 impl
.download_sources
.append(recipe
)
1183 root_attrs
= {'stability': 'testing'}
1186 info("Note: @main on document element is deprecated in %s", self
)
1187 root_commands
['run'] = Command(qdom
.Element(XMLNS_IFACE
, 'command', {'path': main
, 'name': 'run'}), None)
1188 process_group(feed_element
, root_attrs
, [], [], root_commands
)
1190 def get_distro_feed(self
):
1191 """Does this feed contain any <pacakge-implementation> elements?
1192 i.e. is it worth asking the package manager for more information?
1193 @return: the URL of the virtual feed, or None
1195 if self
._package
_implementations
:
1196 return "distribution:" + self
.url
1199 def get_package_impls(self
, distro
):
1200 """Find the best <pacakge-implementation> element(s) for the given distribution.
1201 @param distro: the distribution to use to rate them
1202 @type distro: L{distro.Distribution}
1203 @return: a list of tuples for the best ranked elements
1209 for item
, item_attrs
in self
._package
_implementations
:
1210 distro_names
= item_attrs
.get('distributions', '')
1212 for distro_name
in distro_names
.split(' '):
1213 score
= distro
.get_score(distro_name
) if distro_name
else 0.5
1214 if score
> best_score
:
1217 if score
== best_score
and not added_this
:
1218 best_impls
.append((item
, item_attrs
))
1223 return self
.name
or '(' + os
.path
.basename(self
.url
) + ')'
1226 return _("<Feed %s>") % self
.url
1228 def set_stability_policy(self
, new
):
1229 assert new
is None or isinstance(new
, Stability
)
1230 self
.stability_policy
= new
1232 def get_feed(self
, url
):
1233 for x
in self
.feeds
:
1238 def add_metadata(self
, elem
):
1239 self
.metadata
.append(elem
)
1241 def get_metadata(self
, uri
, name
):
1242 """Return a list of interface metadata elements with this name and namespace URI."""
1243 return [m
for m
in self
.metadata
if m
.name
== name
and m
.uri
== uri
]
1247 return _best_language_match(self
.summaries
) or self
.first_summary
1250 def description(self
):
1251 return _best_language_match(self
.descriptions
) or self
.first_description
1253 def get_replaced_by(self
):
1254 """Return the URI of the interface that replaced the one with the URI of this feed's URL.
1255 This is the value of the feed's <replaced-by interface'...'/> element.
1256 @return: the new URI, or None if it hasn't been replaced
1258 for child
in self
.metadata
:
1259 if child
.uri
== XMLNS_IFACE
and child
.name
== 'replaced-by':
1260 new_uri
= child
.getAttribute('interface')
1261 if new_uri
and (new_uri
.startswith('http:') or new_uri
.startswith('https:') or self
.local_path
):
1265 class DummyFeed(object):
1266 """Temporary class used during API transition."""
1267 last_modified
= None
1269 last_checked
= property(lambda self
: None)
1270 implementations
= property(lambda self
: {})
1271 feeds
= property(lambda self
: [])
1272 summary
= property(lambda self
: '-')
1273 description
= property(lambda self
: '')
1274 def get_name(self
): return self
.name
1275 def get_feed(self
, url
): return None
1276 def get_metadata(self
, uri
, name
): return []
1277 _dummy_feed
= DummyFeed()
1279 if sys
.version_info
[0] > 2:
1282 from functools
import total_ordering
1283 Stability
= total_ordering(Stability
)
1284 Implementation
= total_ordering(Implementation
)
1286 # These could be replaced by urllib.parse.quote, except that
1287 # it uses upper-case escapes and we use lower-case ones...
1289 """Convert each %20 to a space, etc.
1291 uri
= uri
.replace('#', '/')
1292 if '%' not in uri
: return uri
1293 return re
.sub(b
'%[0-9a-fA-F][0-9a-fA-F]',
1294 lambda match
: bytes([int(match
.group(0)[1:], 16)]),
1295 uri
.encode('ascii')).decode('utf-8')
1298 """Convert each space to %20, etc
1300 return re
.sub(b
'[^-_.a-zA-Z0-9]',
1301 lambda match
: ('%%%02x' % ord(match
.group(0))).encode('ascii'),
1302 uri
.encode('utf-8')).decode('ascii')
1304 def _pretty_escape(uri
):
1305 """Convert each space to %20, etc
1306 : is preserved and / becomes #. This makes for nicer strings,
1307 and may replace L{escape} everywhere in future.
1309 if os
.name
== "posix":
1310 # Only preserve : on Posix systems
1311 preserveRegex
= b
'[^-_.a-zA-Z0-9:/]'
1313 # Other OSes may not allow the : character in file names
1314 preserveRegex
= b
'[^-_.a-zA-Z0-9/]'
1315 return re
.sub(preserveRegex
,
1316 lambda match
: ('%%%02x' % ord(match
.group(0))).encode('ascii'),
1317 uri
.encode('utf-8')).decode('ascii').replace('/', '#')
1322 """Convert each %20 to a space, etc.
1324 uri
= uri
.replace('#', '/')
1325 if '%' not in uri
: return uri
1326 return re
.sub('%[0-9a-fA-F][0-9a-fA-F]',
1327 lambda match
: chr(int(match
.group(0)[1:], 16)),
1328 uri
).decode('utf-8')
1331 """Convert each space to %20, etc
1333 return re
.sub('[^-_.a-zA-Z0-9]',
1334 lambda match
: '%%%02x' % ord(match
.group(0)),
1335 uri
.encode('utf-8'))
1337 def _pretty_escape(uri
):
1338 """Convert each space to %20, etc
1339 : is preserved and / becomes #. This makes for nicer strings,
1340 and may replace L{escape} everywhere in future.
1342 if os
.name
== "posix":
1343 # Only preserve : on Posix systems
1344 preserveRegex
= '[^-_.a-zA-Z0-9:/]'
1346 # Other OSes may not allow the : character in file names
1347 preserveRegex
= '[^-_.a-zA-Z0-9/]'
1348 return re
.sub(preserveRegex
,
1349 lambda match
: '%%%02x' % ord(match
.group(0)),
1350 uri
.encode('utf-8')).replace('/', '#')
1352 def canonical_iface_uri(uri
):
1353 """If uri is a relative path, convert to an absolute one.
1354 A "file:///foo" URI is converted to "/foo".
1355 An "alias:prog" URI expands to the URI in the 0alias script
1356 Otherwise, return it unmodified.
1358 @raise SafeException: if uri isn't valid
1360 if uri
.startswith('http://') or uri
.startswith('https://'):
1361 if uri
.count("/") < 3:
1362 raise SafeException(_("Missing / after hostname in URI '%s'") % uri
)
1364 elif uri
.startswith('file:///'):
1366 elif uri
.startswith('file:'):
1368 raise SafeException(_('Use file:///path for absolute paths, not {uri}').format(uri
= uri
))
1369 path
= os
.path
.abspath(uri
[5:])
1370 elif uri
.startswith('alias:'):
1371 from zeroinstall
import alias
1372 alias_prog
= uri
[6:]
1373 if not os
.path
.isabs(alias_prog
):
1374 full_path
= support
.find_in_path(alias_prog
)
1376 raise alias
.NotAnAliasScript("Not found in $PATH: " + alias_prog
)
1378 full_path
= alias_prog
1379 return alias
.parse_script(full_path
).uri
1381 path
= os
.path
.realpath(uri
)
1383 if os
.path
.isfile(path
):
1385 raise SafeException(_("Bad interface name '%(uri)s'.\n"
1386 "(doesn't start with 'http:', and "
1387 "doesn't exist as a local file '%(interface_uri)s' either)") %
1388 {'uri': uri
, 'interface_uri': path
})
1390 _version_mod_to_value
= {
1398 _version_value_to_mod
= {}
1399 for x
in _version_mod_to_value
: _version_value_to_mod
[_version_mod_to_value
[x
]] = x
1402 _version_re
= re
.compile('-([a-z]*)')
1404 def parse_version(version_string
):
1405 """Convert a version string to an internal representation.
1406 The parsed format can be compared quickly using the standard Python functions.
1407 - Version := DottedList ("-" Mod DottedList?)*
1408 - DottedList := (Integer ("." Integer)*)
1409 @rtype: tuple (opaque)
1410 @raise SafeException: if the string isn't a valid version
1411 @since: 0.24 (moved from L{reader}, from where it is still available):"""
1412 if version_string
is None: return None
1413 parts
= _version_re
.split(version_string
)
1415 del parts
[-1] # Ends with a modifier
1419 raise SafeException(_("Empty version string!"))
1422 for x
in range(0, l
, 2):
1425 parts
[x
] = list(map(int, parts
[x
].split('.')))
1427 parts
[x
] = [] # (because ''.split('.') == [''], not [])
1428 for x
in range(1, l
, 2):
1429 parts
[x
] = _version_mod_to_value
[parts
[x
]]
1431 except ValueError as ex
:
1432 raise SafeException(_("Invalid version format in '%(version_string)s': %(exception)s") % {'version_string': version_string
, 'exception': ex
})
1433 except KeyError as ex
:
1434 raise SafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s") % {'version_string': version_string
, 'exception': ex
})
1436 def format_version(version
):
1437 """Format a parsed version for display. Undoes the effect of L{parse_version}.
1438 @see: L{Implementation.get_version}
1441 version
= version
[:]
1443 for x
in range(0, l
, 2):
1444 version
[x
] = '.'.join(map(str, version
[x
]))
1445 for x
in range(1, l
, 2):
1446 version
[x
] = '-' + _version_value_to_mod
[version
[x
]]
1447 if version
[-1] == '-': del version
[-1]
1448 return ''.join(version
)