1 """In-memory representation of interfaces and other data structures.
2
3 The objects in this module are used to build a representation of an XML interface
4 file in memory.
5
6 @see: L{reader} constructs these data-structures
7 @see: U{http://0install.net/interface-spec.html} description of the domain model
8
9 @var defaults: Default values for the 'default' attribute for <environment> bindings of
10 well-known variables.
11 """
12
13
14
15
16 from zeroinstall import _
17 import os, re, locale
18 from logging import info, debug, warn
19 from zeroinstall import SafeException, version
20 from zeroinstall.injector.namespaces import XMLNS_IFACE
21 from zeroinstall.injector import qdom
22
23
24 binding_names = frozenset(['environment', 'overlay', 'executable-in-path', 'executable-in-var'])
25
26 network_offline = 'off-line'
27 network_minimal = 'minimal'
28 network_full = 'full'
29 network_levels = (network_offline, network_minimal, network_full)
30
31 stability_levels = {}
32
33 defaults = {
34 'PATH': '/bin:/usr/bin',
35 'XDG_CONFIG_DIRS': '/etc/xdg',
36 'XDG_DATA_DIRS': '/usr/local/share:/usr/share',
37 }
40 """Raised when parsing an invalid feed."""
41 feed_url = None
42
44 if ex:
45 try:
46 message += "\n\n(exact error: %s)" % ex
47 except:
48
49
50
51 import codecs
52 decoder = codecs.lookup('utf-8')
53 decex = decoder.decode(str(ex), errors = 'replace')[0]
54 message += "\n\n(exact error: %s)" % decex
55
56 SafeException.__init__(self, message)
57
66
68 """Split an arch into an (os, machine) tuple. Either or both parts may be None."""
69 if not arch:
70 return None, None
71 elif '-' not in arch:
72 raise SafeException(_("Malformed arch '%s'") % arch)
73 else:
74 osys, machine = arch.split('-', 1)
75 if osys == '*': osys = None
76 if machine == '*': machine = None
77 return osys, machine
78
80 if osys == machine == None: return None
81 return "%s-%s" % (osys or '*', machine or '*')
82
84 (language, encoding) = locale.getlocale()
85
86 if language:
87
88 language = language.replace('_', '-')
89 else:
90 language = 'en-US'
91
92 return (options.get(language, None) or
93 options.get(language.split('-', 1)[0], None) or
94 options.get('en', None))
95
97 """A stability rating. Each implementation has an upstream stability rating and,
98 optionally, a user-set rating."""
99 __slots__ = ['level', 'name', 'description']
100 - def __init__(self, level, name, description):
106
108 return cmp(self.level, other.level)
109
112
115
117 """Internal"""
118 if e.name == 'environment':
119 mode = {
120 None: EnvironmentBinding.PREPEND,
121 'prepend': EnvironmentBinding.PREPEND,
122 'append': EnvironmentBinding.APPEND,
123 'replace': EnvironmentBinding.REPLACE,
124 }[e.getAttribute('mode')]
125
126 binding = EnvironmentBinding(e.getAttribute('name'),
127 insert = e.getAttribute('insert'),
128 default = e.getAttribute('default'),
129 value = e.getAttribute('value'),
130 mode = mode,
131 separator = e.getAttribute('separator'))
132 if not binding.name: raise InvalidInterface(_("Missing 'name' in binding"))
133 if binding.insert is None and binding.value is None:
134 raise InvalidInterface(_("Missing 'insert' or 'value' in binding"))
135 if binding.insert is not None and binding.value is not None:
136 raise InvalidInterface(_("Binding contains both 'insert' and 'value'"))
137 return binding
138 elif e.name == 'executable-in-path':
139 return ExecutableBinding(e, in_path = True)
140 elif e.name == 'executable-in-var':
141 return ExecutableBinding(e, in_path = False)
142 elif e.name == 'overlay':
143 return OverlayBinding(e.getAttribute('src'), e.getAttribute('mount-point'))
144 else:
145 raise Exception(_("Unknown binding type '%s'") % e.name)
146
172
173 -def N_(message): return message
174
175 insecure = Stability(0, N_('insecure'), _('This is a security risk'))
176 buggy = Stability(5, N_('buggy'), _('Known to have serious bugs'))
177 developer = Stability(10, N_('developer'), _('Work-in-progress - bugs likely'))
178 testing = Stability(20, N_('testing'), _('Stability unknown - please test!'))
179 stable = Stability(30, N_('stable'), _('Tested - no serious problems found'))
180 packaged = Stability(35, N_('packaged'), _('Supplied by the local package manager'))
181 preferred = Stability(40, N_('preferred'), _('Best of all - must be set manually'))
182
183 del N_
186 """A Restriction limits the allowed implementations of an Interface."""
187 __slots__ = []
188
190 """Called by the L{solver.Solver} to check whether a particular implementation is acceptable.
191 @return: False if this implementation is not a possibility
192 @rtype: bool
193 """
194 raise NotImplementedError(_("Abstract"))
195
197 """Only select implementations with a particular version number.
198 @since: 0.40"""
199
201 """@param version: the required version number
202 @see: L{parse_version}; use this to pre-process the version number
203 """
204 self.version = version
205
208
211
213 """Only versions within the given range are acceptable"""
214 __slots__ = ['before', 'not_before']
215
216 - def __init__(self, before, not_before):
217 """@param before: chosen versions must be earlier than this
218 @param not_before: versions must be at least this high
219 @see: L{parse_version}; use this to pre-process the versions
220 """
221 self.before = before
222 self.not_before = not_before
223
225 if self.not_before and impl.version < self.not_before:
226 return False
227 if self.before and impl.version >= self.before:
228 return False
229 return True
230
232 if self.not_before is not None or self.before is not None:
233 range = ''
234 if self.not_before is not None:
235 range += format_version(self.not_before) + ' <= '
236 range += 'version'
237 if self.before is not None:
238 range += ' < ' + format_version(self.before)
239 else:
240 range = 'none'
241 return _("(restriction: %s)") % range
242
244 """Information about how the choice of a Dependency is made known
245 to the application being run."""
246
247 @property
249 """"Returns the name of the specific command needed by this binding, if any.
250 @since: 1.2"""
251 return None
252
254 """Indicate the chosen implementation using an environment variable."""
255 __slots__ = ['name', 'insert', 'default', 'mode', 'value']
256
257 PREPEND = 'prepend'
258 APPEND = 'append'
259 REPLACE = 'replace'
260
261 - def __init__(self, name, insert, default = None, mode = PREPEND, value=None, separator=None):
262 """
263 mode argument added in version 0.28
264 value argument added in version 0.52
265 """
266 self.name = name
267 self.insert = insert
268 self.default = default
269 self.mode = mode
270 self.value = value
271 if separator is None:
272 self.separator = os.pathsep
273 else:
274 self.separator = separator
275
276
278 return _("<environ %(name)s %(mode)s %(insert)s %(value)s>") % \
279 {'name': self.name, 'mode': self.mode, 'insert': self.insert, 'value': self.value}
280
281 __repr__ = __str__
282
284 """Calculate the new value of the environment variable after applying this binding.
285 @param path: the path to the selected implementation
286 @param old_value: the current value of the environment variable
287 @return: the new value for the environment variable"""
288
289 if self.insert is not None:
290 extra = os.path.join(path, self.insert)
291 else:
292 assert self.value is not None
293 extra = self.value
294
295 if self.mode == EnvironmentBinding.REPLACE:
296 return extra
297
298 if old_value is None:
299 old_value = self.default or defaults.get(self.name, None)
300 if old_value is None:
301 return extra
302 if self.mode == EnvironmentBinding.PREPEND:
303 return extra + self.separator + old_value
304 else:
305 return old_value + self.separator + extra
306
307 - def _toxml(self, doc, prefixes):
308 """Create a DOM element for this binding.
309 @param doc: document to use to create the element
310 @return: the new element
311 """
312 env_elem = doc.createElementNS(XMLNS_IFACE, 'environment')
313 env_elem.setAttributeNS(None, 'name', self.name)
314 if self.mode is not None:
315 env_elem.setAttributeNS(None, 'mode', self.mode)
316 if self.insert is not None:
317 env_elem.setAttributeNS(None, 'insert', self.insert)
318 else:
319 env_elem.setAttributeNS(None, 'value', self.value)
320 if self.default:
321 env_elem.setAttributeNS(None, 'default', self.default)
322 if self.separator:
323 env_elem.setAttributeNS(None, 'separator', self.separator)
324 return env_elem
325
327 """Make the chosen command available in $PATH.
328 @ivar in_path: True to add the named command to $PATH, False to store in named variable
329 @type in_path: bool
330 """
331 __slots__ = ['qdom']
332
334 self.qdom = qdom
335 self.in_path = in_path
336
338 return str(self.qdom)
339
340 __repr__ = __str__
341
342 - def _toxml(self, doc, prefixes):
344
345 @property
348
349 @property
352
354 """Make the chosen implementation available by overlaying it onto another part of the file-system.
355 This is to support legacy programs which use hard-coded paths."""
356 __slots__ = ['src', 'mount_point']
357
359 self.src = src
360 self.mount_point = mount_point
361
363 return _("<overlay %(src)s on %(mount_point)s>") % {'src': self.src or '.', 'mount_point': self.mount_point or '/'}
364
365 __repr__ = __str__
366
367 - def _toxml(self, doc, prefixes):
368 """Create a DOM element for this binding.
369 @param doc: document to use to create the element
370 @return: the new element
371 """
372 env_elem = doc.createElementNS(XMLNS_IFACE, 'overlay')
373 if self.src is not None:
374 env_elem.setAttributeNS(None, 'src', self.src)
375 if self.mount_point is not None:
376 env_elem.setAttributeNS(None, 'mount-point', self.mount_point)
377 return env_elem
378
380 """An interface's feeds are other interfaces whose implementations can also be
381 used as implementations of this interface."""
382 __slots__ = ['uri', 'os', 'machine', 'user_override', 'langs']
383 - def __init__(self, uri, arch, user_override, langs = None):
384 self.uri = uri
385
386
387 self.user_override = user_override
388 self.os, self.machine = _split_arch(arch)
389 self.langs = langs
390
392 return "<Feed from %s>" % self.uri
393 __repr__ = __str__
394
395 arch = property(lambda self: _join_arch(self.os, self.machine))
396
398 """A Dependency indicates that an Implementation requires some additional
399 code to function. This is an abstract base class.
400 @ivar qdom: the XML element for this Dependency (since 0launch 0.51)
401 @type qdom: L{qdom.Element}
402 @ivar metadata: any extra attributes from the XML element
403 @type metadata: {str: str}
404 """
405 __slots__ = ['qdom']
406
407 Essential = "essential"
408 Recommended = "recommended"
409
411 assert isinstance(element, qdom.Element), type(element)
412 self.qdom = element
413
414 @property
417
418 @property
421
423 """Return a list of command names needed by this dependency"""
424 return []
425
427 """A Dependency on a Zero Install interface.
428 @ivar interface: the interface required by this dependency
429 @type interface: str
430 @ivar restrictions: a list of constraints on acceptable implementations
431 @type restrictions: [L{Restriction}]
432 @ivar bindings: how to make the choice of implementation known
433 @type bindings: [L{Binding}]
434 @since: 0.28
435 """
436 __slots__ = ['interface', 'restrictions', 'bindings']
437
438 - def __init__(self, interface, restrictions = None, element = None):
448
450 return _("<Dependency on %(interface)s; bindings: %(bindings)s%(restrictions)s>") % {'interface': self.interface, 'bindings': self.bindings, 'restrictions': self.restrictions}
451
463
464 @property
469
471 """A RetrievalMethod provides a way to fetch an implementation."""
472 __slots__ = []
473
475 """A DownloadSource provides a way to fetch an implementation."""
476 __slots__ = ['implementation', 'url', 'size', 'extract', 'start_offset', 'type']
477
478 - def __init__(self, implementation, url, size, extract, start_offset = 0, type = None):
485
486 -class Recipe(RetrievalMethod):
487 """Get an implementation by following a series of steps.
488 @ivar size: the combined download sizes from all the steps
489 @type size: int
490 @ivar steps: the sequence of steps which must be performed
491 @type steps: [L{RetrievalMethod}]"""
492 __slots__ = ['steps']
493
496
497 size = property(lambda self: sum([x.size for x in self.steps]))
498
500 """A package that is installed using the distribution's tools (including PackageKit).
501 @ivar install: a function to call to install this package
502 @type install: (L{handler.Handler}) -> L{tasks.Blocker}
503 @ivar package_id: the package name, in a form recognised by the distribution's tools
504 @type package_id: str
505 @ivar size: the download size in bytes
506 @type size: int
507 @ivar needs_confirmation: whether the user should be asked to confirm before calling install()
508 @type needs_confirmation: bool"""
509
510 __slots__ = ['package_id', 'size', 'install', 'needs_confirmation']
511
512 - def __init__(self, package_id, size, install, needs_confirmation = True):
513 RetrievalMethod.__init__(self)
514 self.package_id = package_id
515 self.size = size
516 self.install = install
517 self.needs_confirmation = needs_confirmation
518
520 """A Command is a way of running an Implementation as a program."""
521
522 __slots__ = ['qdom', '_depends', '_local_dir', '_runner', '_bindings']
523
525 """@param qdom: the <command> element
526 @param local_dir: the directory containing the feed (for relative dependencies), or None if not local
527 """
528 assert qdom.name == 'command', 'not <command>: %s' % qdom
529 self.qdom = qdom
530 self._local_dir = local_dir
531 self._depends = None
532 self._bindings = None
533
534 path = property(lambda self: self.qdom.attrs.get("path", None))
535
536 - def _toxml(self, doc, prefixes):
538
539 @property
541 if self._depends is None:
542 self._runner = None
543 depends = []
544 for child in self.qdom.childNodes:
545 if child.name == 'requires':
546 dep = process_depends(child, self._local_dir)
547 depends.append(dep)
548 elif child.name == 'runner':
549 if self._runner:
550 raise InvalidInterface(_("Multiple <runner>s in <command>!"))
551 dep = process_depends(child, self._local_dir)
552 depends.append(dep)
553 self._runner = dep
554 self._depends = depends
555 return self._depends
556
558 self.requires
559 return self._runner
560
562 return str(self.qdom)
563
564 @property
575
577 """An Implementation is a package which implements an Interface.
578 @ivar download_sources: list of methods of getting this implementation
579 @type download_sources: [L{RetrievalMethod}]
580 @ivar feed: the feed owning this implementation (since 0.32)
581 @type feed: [L{ZeroInstallFeed}]
582 @ivar bindings: how to tell this component where it itself is located (since 0.31)
583 @type bindings: [Binding]
584 @ivar upstream_stability: the stability reported by the packager
585 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged]
586 @ivar user_stability: the stability as set by the user
587 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged | preferred]
588 @ivar langs: natural languages supported by this package
589 @type langs: str
590 @ivar requires: interfaces this package depends on
591 @type requires: [L{Dependency}]
592 @ivar commands: ways to execute as a program
593 @type commands: {str: Command}
594 @ivar metadata: extra metadata from the feed
595 @type metadata: {"[URI ]localName": str}
596 @ivar id: a unique identifier for this Implementation
597 @ivar version: a parsed version number
598 @ivar released: release date
599 @ivar local_path: the directory containing this local implementation, or None if it isn't local (id isn't a path)
600 @type local_path: str | None
601 @ivar requires_root_install: whether the user will need admin rights to use this
602 @type requires_root_install: bool
603 """
604
605
606
607 __slots__ = ['upstream_stability', 'user_stability', 'langs',
608 'requires', 'metadata', 'download_sources', 'commands',
609 'id', 'feed', 'version', 'released', 'bindings', 'machine']
610
612 assert id
613 self.feed = feed
614 self.id = id
615 self.user_stability = None
616 self.upstream_stability = None
617 self.metadata = {}
618 self.requires = []
619 self.version = None
620 self.released = None
621 self.download_sources = []
622 self.langs = ""
623 self.machine = None
624 self.bindings = []
625 self.commands = {}
626
628 return self.user_stability or self.upstream_stability or testing
629
632
635
637 """Newer versions come first"""
638 d = cmp(other.version, self.version)
639 if d: return d
640
641
642 d = cmp(other.feed.url, self.feed.url)
643 if d: return d
644 return cmp(other.id, self.id)
645
647 """Return the version as a string.
648 @see: L{format_version}
649 """
650 return format_version(self.version)
651
652 arch = property(lambda self: _join_arch(self.os, self.machine))
653
654 os = None
655 local_path = None
656 digests = None
657 requires_root_install = False
658
659 - def _get_main(self):
660 """"@deprecated: use commands["run"] instead"""
661 main = self.commands.get("run", None)
662 if main is not None:
663 return main.path
664 return None
665 - def _set_main(self, path):
666 """"@deprecated: use commands["run"] instead"""
667 if path is None:
668 if "run" in self.commands:
669 del self.commands["run"]
670 else:
671 self.commands["run"] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': path, 'name': 'run'}), None)
672 main = property(_get_main, _set_main)
673
675 """Is this Implementation available locally?
676 (a local implementation, an installed distribution package, or a cached ZeroInstallImplementation)
677 @rtype: bool
678 @since: 0.53
679 """
680 raise NotImplementedError("abstract")
681
683 """An implementation provided by the distribution. Information such as the version
684 comes from the package manager.
685 @ivar package_implementation: the <package-implementation> element that generated this impl (since 1.7)
686 @type package_implementation: L{qdom.Element}
687 @since: 0.28"""
688 __slots__ = ['distro', 'installed', 'package_implementation']
689
690 - def __init__(self, feed, id, distro, package_implementation = None):
691 assert id.startswith('package:')
692 Implementation.__init__(self, feed, id)
693 self.distro = distro
694 self.installed = False
695 self.package_implementation = package_implementation
696
697 if package_implementation:
698 for child in package_implementation.childNodes:
699 if child.uri != XMLNS_IFACE: continue
700 if child.name == 'command':
701 command_name = child.attrs.get('name', None)
702 if not command_name:
703 raise InvalidInterface('Missing name for <command>')
704 self.commands[command_name] = Command(child, local_dir = None)
705
706 @property
708 return not self.installed
709
711 return self.installed
712
714 """An implementation where all the information comes from Zero Install.
715 @ivar digests: a list of "algorith=value" strings (since 0.45)
716 @type digests: [str]
717 @since: 0.28"""
718 __slots__ = ['os', 'size', 'digests', 'local_path']
719
720 - def __init__(self, feed, id, local_path):
728
729
730 dependencies = property(lambda self: dict([(x.interface, x) for x in self.requires
731 if isinstance(x, InterfaceDependency)]))
732
734 """Add a download source."""
735 self.download_sources.append(DownloadSource(self, url, size, extract, start_offset, type))
736
738 self.os, self.machine = _split_arch(arch)
739 arch = property(lambda self: _join_arch(self.os, self.machine), set_arch)
740
748
750 """An Interface represents some contract of behaviour.
751 @ivar uri: the URI for this interface.
752 @ivar stability_policy: user's configured policy.
753 Implementations at this level or higher are preferred.
754 Lower levels are used only if there is no other choice.
755 """
756 __slots__ = ['uri', 'stability_policy', 'extra_feeds']
757
758 implementations = property(lambda self: self._main_feed.implementations)
759 name = property(lambda self: self._main_feed.name)
760 description = property(lambda self: self._main_feed.description)
761 summary = property(lambda self: self._main_feed.summary)
762 last_modified = property(lambda self: self._main_feed.last_modified)
763 feeds = property(lambda self: self.extra_feeds + self._main_feed.feeds)
764 metadata = property(lambda self: self._main_feed.metadata)
765
766 last_checked = property(lambda self: self._main_feed.last_checked)
767
769 assert uri
770 if uri.startswith('http:') or uri.startswith('https:') or os.path.isabs(uri):
771 self.uri = uri
772 else:
773 raise SafeException(_("Interface name '%s' doesn't start "
774 "with 'http:' or 'https:'") % uri)
775 self.reset()
776
778 retval = {}
779 for key in self._main_feed.feed_for:
780 retval[key] = True
781 return retval
782 feed_for = property(_get_feed_for)
783
785 self.extra_feeds = []
786 self.stability_policy = None
787
794
796 return _("<Interface %s>") % self.uri
797
799 assert new is None or isinstance(new, Stability)
800 self.stability_policy = new
801
803
804
805 for x in self.extra_feeds:
806 if x.uri == url:
807 return x
808
809 return None
810
813
814 @property
815 - def _main_feed(self):
824
826 """Add each attribute of item to a copy of attrs and return the copy.
827 @type attrs: {str: str}
828 @type item: L{qdom.Element}
829 @rtype: {str: str}
830 """
831 new = attrs.copy()
832 for a in item.attrs:
833 new[str(a)] = item.attrs[a]
834 return new
835
837 val = elem.getAttribute(attr_name)
838 if val is not None:
839 try:
840 val = int(val)
841 except ValueError:
842 raise SafeException(_("Invalid value for integer attribute '%(attribute_name)s': %(value)s") % {'attribute_name': attr_name, 'value': val})
843 return val
844
846 """A feed lists available implementations of an interface.
847 @ivar url: the URL for this feed
848 @ivar implementations: Implementations in this feed, indexed by ID
849 @type implementations: {str: L{Implementation}}
850 @ivar name: human-friendly name
851 @ivar summaries: short textual description (in various languages, since 0.49)
852 @type summaries: {str: str}
853 @ivar descriptions: long textual description (in various languages, since 0.49)
854 @type descriptions: {str: str}
855 @ivar last_modified: timestamp on signature
856 @ivar last_checked: time feed was last successfully downloaded and updated
857 @ivar local_path: the path of this local feed, or None if remote (since 1.7)
858 @type local_path: str | None
859 @ivar feeds: list of <feed> elements in this feed
860 @type feeds: [L{Feed}]
861 @ivar feed_for: interfaces for which this could be a feed
862 @type feed_for: set(str)
863 @ivar metadata: extra elements we didn't understand
864 """
865
866 __slots__ = ['url', 'implementations', 'name', 'descriptions', 'first_description', 'summaries', 'first_summary', '_package_implementations',
867 'last_checked', 'last_modified', 'feeds', 'feed_for', 'metadata', 'local_path']
868
869 - def __init__(self, feed_element, local_path = None, distro = None):
870 """Create a feed object from a DOM.
871 @param feed_element: the root element of a feed file
872 @type feed_element: L{qdom.Element}
873 @param local_path: the pathname of this local feed, or None for remote feeds"""
874 self.local_path = local_path
875 self.implementations = {}
876 self.name = None
877 self.summaries = {}
878 self.first_summary = None
879 self.descriptions = {}
880 self.first_description = None
881 self.last_modified = None
882 self.feeds = []
883 self.feed_for = set()
884 self.metadata = []
885 self.last_checked = None
886 self._package_implementations = []
887
888 if distro is not None:
889 import warnings
890 warnings.warn("distro argument is now ignored", DeprecationWarning, 2)
891
892 if feed_element is None:
893 return
894
895 assert feed_element.name in ('interface', 'feed'), "Root element should be <interface>, not %s" % feed_element
896 assert feed_element.uri == XMLNS_IFACE, "Wrong namespace on root element: %s" % feed_element.uri
897
898 main = feed_element.getAttribute('main')
899
900
901 if local_path:
902 self.url = local_path
903 local_dir = os.path.dirname(local_path)
904 else:
905 assert local_path is None
906 self.url = feed_element.getAttribute('uri')
907 if not self.url:
908 raise InvalidInterface(_("<interface> uri attribute missing"))
909 local_dir = None
910
911 min_injector_version = feed_element.getAttribute('min-injector-version')
912 if min_injector_version:
913 if parse_version(min_injector_version) > parse_version(version):
914 raise InvalidInterface(_("This feed requires version %(min_version)s or later of "
915 "Zero Install, but I am only version %(version)s. "
916 "You can get a newer version from http://0install.net") %
917 {'min_version': min_injector_version, 'version': version})
918
919 for x in feed_element.childNodes:
920 if x.uri != XMLNS_IFACE:
921 self.metadata.append(x)
922 continue
923 if x.name == 'name':
924 self.name = x.content
925 elif x.name == 'description':
926 if self.first_description == None:
927 self.first_description = x.content
928 self.descriptions[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", 'en')] = x.content
929 elif x.name == 'summary':
930 if self.first_summary == None:
931 self.first_summary = x.content
932 self.summaries[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", 'en')] = x.content
933 elif x.name == 'feed-for':
934 feed_iface = x.getAttribute('interface')
935 if not feed_iface:
936 raise InvalidInterface(_('Missing "interface" attribute in <feed-for>'))
937 self.feed_for.add(feed_iface)
938
939
940
941 debug(_("Is feed-for %s"), feed_iface)
942 elif x.name == 'feed':
943 feed_src = x.getAttribute('src')
944 if not feed_src:
945 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
946 if feed_src.startswith('http:') or feed_src.startswith('https:') or local_path:
947 langs = x.getAttribute('langs')
948 if langs: langs = langs.replace('_', '-')
949 self.feeds.append(Feed(feed_src, x.getAttribute('arch'), False, langs = langs))
950 else:
951 raise InvalidInterface(_("Invalid feed URL '%s'") % feed_src)
952 else:
953 self.metadata.append(x)
954
955 if not self.name:
956 raise InvalidInterface(_("Missing <name> in feed"))
957 if not self.summary:
958 raise InvalidInterface(_("Missing <summary> in feed"))
959
960 def process_group(group, group_attrs, base_depends, base_bindings, base_commands):
961 for item in group.childNodes:
962 if item.uri != XMLNS_IFACE: continue
963
964 if item.name not in ('group', 'implementation', 'package-implementation'):
965 continue
966
967
968
969
970
971
972
973
974 depends = base_depends[:]
975 bindings = base_bindings[:]
976 commands = base_commands.copy()
977
978 for attr, command in [('main', 'run'),
979 ('self-test', 'test')]:
980 value = item.attrs.get(attr, None)
981 if value is not None:
982 commands[command] = Command(qdom.Element(XMLNS_IFACE, 'command', {'name': command, 'path': value}), None)
983
984 for child in item.childNodes:
985 if child.uri != XMLNS_IFACE: continue
986 if child.name == 'requires':
987 dep = process_depends(child, local_dir)
988 depends.append(dep)
989 elif child.name == 'command':
990 command_name = child.attrs.get('name', None)
991 if not command_name:
992 raise InvalidInterface('Missing name for <command>')
993 commands[command_name] = Command(child, local_dir)
994 elif child.name in binding_names:
995 bindings.append(process_binding(child))
996
997 compile_command = item.attrs.get('http://zero-install.sourceforge.net/2006/namespaces/0compile command')
998 if compile_command is not None:
999 commands['compile'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'name': 'compile', 'shell-command': compile_command}), None)
1000
1001 item_attrs = _merge_attrs(group_attrs, item)
1002
1003 if item.name == 'group':
1004 process_group(item, item_attrs, depends, bindings, commands)
1005 elif item.name == 'implementation':
1006 process_impl(item, item_attrs, depends, bindings, commands)
1007 elif item.name == 'package-implementation':
1008 if depends:
1009 warn("A <package-implementation> with dependencies in %s!", self.url)
1010 self._package_implementations.append((item, item_attrs))
1011 else:
1012 assert 0
1013
1014 def process_impl(item, item_attrs, depends, bindings, commands):
1015 id = item.getAttribute('id')
1016 if id is None:
1017 raise InvalidInterface(_("Missing 'id' attribute on %s") % item)
1018 local_path = item_attrs.get('local-path')
1019 if local_dir and local_path:
1020 abs_local_path = os.path.abspath(os.path.join(local_dir, local_path))
1021 impl = ZeroInstallImplementation(self, id, abs_local_path)
1022 elif local_dir and (id.startswith('/') or id.startswith('.')):
1023
1024 id = os.path.abspath(os.path.join(local_dir, id))
1025 impl = ZeroInstallImplementation(self, id, id)
1026 else:
1027 impl = ZeroInstallImplementation(self, id, None)
1028 if '=' in id:
1029
1030 impl.digests.append(id)
1031 if id in self.implementations:
1032 warn(_("Duplicate ID '%(id)s' in feed '%(feed)s'"), {'id': id, 'feed': self})
1033 self.implementations[id] = impl
1034
1035 impl.metadata = item_attrs
1036 try:
1037 version_mod = item_attrs.get('version-modifier', None)
1038 if version_mod:
1039 item_attrs['version'] += version_mod
1040 del item_attrs['version-modifier']
1041 version = item_attrs['version']
1042 except KeyError:
1043 raise InvalidInterface(_("Missing version attribute"))
1044 impl.version = parse_version(version)
1045
1046 impl.commands = commands
1047
1048 impl.released = item_attrs.get('released', None)
1049 impl.langs = item_attrs.get('langs', '').replace('_', '-')
1050
1051 size = item.getAttribute('size')
1052 if size:
1053 impl.size = int(size)
1054 impl.arch = item_attrs.get('arch', None)
1055 try:
1056 stability = stability_levels[str(item_attrs['stability'])]
1057 except KeyError:
1058 stab = str(item_attrs['stability'])
1059 if stab != stab.lower():
1060 raise InvalidInterface(_('Stability "%s" invalid - use lower case!') % item_attrs.stability)
1061 raise InvalidInterface(_('Stability "%s" invalid') % item_attrs['stability'])
1062 if stability >= preferred:
1063 raise InvalidInterface(_("Upstream can't set stability to preferred!"))
1064 impl.upstream_stability = stability
1065
1066 impl.bindings = bindings
1067 impl.requires = depends
1068
1069 for elem in item.childNodes:
1070 if elem.uri != XMLNS_IFACE: continue
1071 if elem.name == 'archive':
1072 url = elem.getAttribute('href')
1073 if not url:
1074 raise InvalidInterface(_("Missing href attribute on <archive>"))
1075 size = elem.getAttribute('size')
1076 if not size:
1077 raise InvalidInterface(_("Missing size attribute on <archive>"))
1078 impl.add_download_source(url = url, size = int(size),
1079 extract = elem.getAttribute('extract'),
1080 start_offset = _get_long(elem, 'start-offset'),
1081 type = elem.getAttribute('type'))
1082 elif elem.name == 'manifest-digest':
1083 for aname, avalue in elem.attrs.iteritems():
1084 if ' ' not in aname:
1085 impl.digests.append('%s=%s' % (aname, avalue))
1086 elif elem.name == 'recipe':
1087 recipe = Recipe()
1088 for recipe_step in elem.childNodes:
1089 if recipe_step.uri == XMLNS_IFACE and recipe_step.name == 'archive':
1090 url = recipe_step.getAttribute('href')
1091 if not url:
1092 raise InvalidInterface(_("Missing href attribute on <archive>"))
1093 size = recipe_step.getAttribute('size')
1094 if not size:
1095 raise InvalidInterface(_("Missing size attribute on <archive>"))
1096 recipe.steps.append(DownloadSource(None, url = url, size = int(size),
1097 extract = recipe_step.getAttribute('extract'),
1098 start_offset = _get_long(recipe_step, 'start-offset'),
1099 type = recipe_step.getAttribute('type')))
1100 else:
1101 info(_("Unknown step '%s' in recipe; skipping recipe"), recipe_step.name)
1102 break
1103 else:
1104 impl.download_sources.append(recipe)
1105
1106 root_attrs = {'stability': 'testing'}
1107 root_commands = {}
1108 if main:
1109 info("Note: @main on document element is deprecated in %s", self)
1110 root_commands['run'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': main, 'name': 'run'}), None)
1111 process_group(feed_element, root_attrs, [], [], root_commands)
1112
1114 """Does this feed contain any <pacakge-implementation> elements?
1115 i.e. is it worth asking the package manager for more information?
1116 @return: the URL of the virtual feed, or None
1117 @since: 0.49"""
1118 if self._package_implementations:
1119 return "distribution:" + self.url
1120 return None
1121
1123 """Find the best <pacakge-implementation> element(s) for the given distribution.
1124 @param distro: the distribution to use to rate them
1125 @type distro: L{distro.Distribution}
1126 @return: a list of tuples for the best ranked elements
1127 @rtype: [str]
1128 @since: 0.49"""
1129 best_score = 0
1130 best_impls = []
1131
1132 for item, item_attrs in self._package_implementations:
1133 distro_names = item_attrs.get('distributions', '')
1134 for distro_name in distro_names.split(' '):
1135 score = distro.get_score(distro_name)
1136 if score > best_score:
1137 best_score = score
1138 best_impls = []
1139 if score == best_score:
1140 best_impls.append((item, item_attrs))
1141 return best_impls
1142
1145
1147 return _("<Feed %s>") % self.url
1148
1150 assert new is None or isinstance(new, Stability)
1151 self.stability_policy = new
1152
1154 for x in self.feeds:
1155 if x.uri == url:
1156 return x
1157 return None
1158
1161
1165
1166 @property
1168 return _best_language_match(self.summaries) or self.first_summary
1169
1170 @property
1172 return _best_language_match(self.descriptions) or self.first_description
1173
1175 """Return the URI of the interface that replaced the one with the URI of this feed's URL.
1176 This is the value of the feed's <replaced-by interface'...'/> element.
1177 @return: the new URI, or None if it hasn't been replaced
1178 @since: 1.7"""
1179 for child in self.metadata:
1180 if child.uri == XMLNS_IFACE and child.name == 'replaced-by':
1181 new_uri = child.getAttribute('interface')
1182 if new_uri and (new_uri.startswith('http:') or new_uri.startswith('https:') or self.local_path):
1183 return new_uri
1184 return None
1185
1198 _dummy_feed = DummyFeed()
1201 """Convert each %20 to a space, etc.
1202 @rtype: str"""
1203 uri = uri.replace('#', '/')
1204 if '%' not in uri: return uri
1205 return re.sub('%[0-9a-fA-F][0-9a-fA-F]',
1206 lambda match: chr(int(match.group(0)[1:], 16)),
1207 uri).decode('utf-8')
1208
1210 """Convert each space to %20, etc
1211 @rtype: str"""
1212 return re.sub('[^-_.a-zA-Z0-9]',
1213 lambda match: '%%%02x' % ord(match.group(0)),
1214 uri.encode('utf-8'))
1215
1217 """Convert each space to %20, etc
1218 : is preserved and / becomes #. This makes for nicer strings,
1219 and may replace L{escape} everywhere in future.
1220 @rtype: str"""
1221 if os.name == "posix":
1222
1223 preserveRegex = '[^-_.a-zA-Z0-9:/]'
1224 else:
1225
1226 preserveRegex = '[^-_.a-zA-Z0-9/]'
1227 return re.sub(preserveRegex,
1228 lambda match: '%%%02x' % ord(match.group(0)),
1229 uri.encode('utf-8')).replace('/', '#')
1230
1232 """If uri is a relative path, convert to an absolute one.
1233 A "file:///foo" URI is converted to "/foo".
1234 An "alias:prog" URI expands to the URI in the 0alias script
1235 Otherwise, return it unmodified.
1236 @rtype: str
1237 @raise SafeException: if uri isn't valid
1238 """
1239 if uri.startswith('http://') or uri.startswith('https://'):
1240 if uri.count("/") < 3:
1241 raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
1242 return uri
1243 elif uri.startswith('file:///'):
1244 path = uri[7:]
1245 elif uri.startswith('file:'):
1246 if uri[5] == '/':
1247 raise SafeException(_('Use file:///path for absolute paths, not {uri}').format(uri = uri))
1248 path = os.path.abspath(uri[5:])
1249 elif uri.startswith('alias:'):
1250 from zeroinstall import alias, support
1251 alias_prog = uri[6:]
1252 if not os.path.isabs(alias_prog):
1253 full_path = support.find_in_path(alias_prog)
1254 if not full_path:
1255 raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
1256 else:
1257 full_path = alias_prog
1258 return alias.parse_script(full_path).uri
1259 else:
1260 path = os.path.realpath(uri)
1261
1262 if os.path.isfile(path):
1263 return path
1264 raise SafeException(_("Bad interface name '%(uri)s'.\n"
1265 "(doesn't start with 'http:', and "
1266 "doesn't exist as a local file '%(interface_uri)s' either)") %
1267 {'uri': uri, 'interface_uri': path})
1268
1269 _version_mod_to_value = {
1270 'pre': -2,
1271 'rc': -1,
1272 '': 0,
1273 'post': 1,
1274 }
1275
1276
1277 _version_value_to_mod = {}
1278 for x in _version_mod_to_value: _version_value_to_mod[_version_mod_to_value[x]] = x
1279 del x
1280
1281 _version_re = re.compile('-([a-z]*)')
1284 """Convert a version string to an internal representation.
1285 The parsed format can be compared quickly using the standard Python functions.
1286 - Version := DottedList ("-" Mod DottedList?)*
1287 - DottedList := (Integer ("." Integer)*)
1288 @rtype: tuple (opaque)
1289 @raise SafeException: if the string isn't a valid version
1290 @since: 0.24 (moved from L{reader}, from where it is still available):"""
1291 if version_string is None: return None
1292 parts = _version_re.split(version_string)
1293 if parts[-1] == '':
1294 del parts[-1]
1295 else:
1296 parts.append('')
1297 if not parts:
1298 raise SafeException(_("Empty version string!"))
1299 l = len(parts)
1300 try:
1301 for x in range(0, l, 2):
1302 part = parts[x]
1303 if part:
1304 parts[x] = map(int, parts[x].split('.'))
1305 else:
1306 parts[x] = []
1307 for x in range(1, l, 2):
1308 parts[x] = _version_mod_to_value[parts[x]]
1309 return parts
1310 except ValueError as ex:
1311 raise SafeException(_("Invalid version format in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1312 except KeyError as ex:
1313 raise SafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1314
1328