Added ability to generate archive sizes on Feed creation
[zeroinstall/zeroinstall-limyreth.git] / zeroinstall / injector / model.py
blob2f8a7f2804850a338b372195388f227fc551eec7
1 """In-memory representation of interfaces and other data structures.
3 The objects in this module are used to build a representation of an XML interface
4 file in memory.
6 @see: L{reader} constructs these data-structures
7 @see: U{http://0install.net/interface-spec.html} description of the domain model
9 @var defaults: Default values for the 'default' attribute for <environment> bindings of
10 well-known variables.
11 """
13 # Copyright (C) 2009, Thomas Leonard
14 # See the README file for details, or visit http://0install.net.
16 from zeroinstall import _
17 import os, re, locale
18 from logging import info, debug, warn
19 from zeroinstall import SafeException, version
20 from zeroinstall.injector.namespaces import XMLNS_IFACE
21 from zeroinstall.injector import qdom
22 from zeroinstall.zerostore import unpack
23 from zeroinstall.support import tasks
25 # Element names for bindings in feed files
26 binding_names = frozenset(['environment', 'overlay'])
28 network_offline = 'off-line'
29 network_minimal = 'minimal'
30 network_full = 'full'
31 network_levels = (network_offline, network_minimal, network_full)
33 stability_levels = {} # Name -> Stability
35 defaults = {
36 'PATH': '/bin:/usr/bin',
37 'XDG_CONFIG_DIRS': '/etc/xdg',
38 'XDG_DATA_DIRS': '/usr/local/share:/usr/share',
41 class InvalidInterface(SafeException):
42 """Raised when parsing an invalid feed."""
43 feed_url = None
45 def __init__(self, message, ex = None):
46 if ex:
47 try:
48 message += "\n\n(exact error: %s)" % ex
49 except:
50 # Some Python messages have type str but contain UTF-8 sequences.
51 # (e.g. IOException). Adding these to a Unicode 'message' (e.g.
52 # after gettext translation) will cause an error.
53 import codecs
54 decoder = codecs.lookup('utf-8')
55 decex = decoder.decode(str(ex), errors = 'replace')[0]
56 message += "\n\n(exact error: %s)" % decex
58 SafeException.__init__(self, message)
60 def __unicode__(self):
61 if hasattr(SafeException, '__unicode__'):
62 # Python >= 2.6
63 if self.feed_url:
64 return _('%s [%s]') % (SafeException.__unicode__(self), self.feed_url)
65 return SafeException.__unicode__(self)
66 else:
67 return unicode(SafeException.__str__(self))
69 def _split_arch(arch):
70 """Split an arch into an (os, machine) tuple. Either or both parts may be None."""
71 if not arch:
72 return None, None
73 elif '-' not in arch:
74 raise SafeException(_("Malformed arch '%s'") % arch)
75 else:
76 osys, machine = arch.split('-', 1)
77 if osys == '*': osys = None
78 if machine == '*': machine = None
79 return osys, machine
81 def _join_arch(osys, machine):
82 if osys == machine == None: return None
83 return "%s-%s" % (osys or '*', machine or '*')
85 def _best_language_match(options):
86 (language, encoding) = locale.getlocale()
88 if language:
89 # xml:lang uses '-', while LANG uses '_'
90 language = language.replace('_', '-')
91 else:
92 language = 'en-US'
94 return (options.get(language, None) or # Exact match (language+region)
95 options.get(language.split('-', 1)[0], None) or # Matching language
96 options.get('en', None)) # English
98 class Stability(object):
99 """A stability rating. Each implementation has an upstream stability rating and,
100 optionally, a user-set rating."""
101 __slots__ = ['level', 'name', 'description']
102 def __init__(self, level, name, description):
103 self.level = level
104 self.name = name
105 self.description = description
106 assert name not in stability_levels
107 stability_levels[name] = self
109 def __cmp__(self, other):
110 return cmp(self.level, other.level)
112 def __str__(self):
113 return self.name
115 def __repr__(self):
116 return _("<Stability: %s>") % self.description
118 def process_binding(e):
119 """Internal"""
120 if e.name == 'environment':
121 mode = {
122 None: EnvironmentBinding.PREPEND,
123 'prepend': EnvironmentBinding.PREPEND,
124 'append': EnvironmentBinding.APPEND,
125 'replace': EnvironmentBinding.REPLACE,
126 }[e.getAttribute('mode')]
128 binding = EnvironmentBinding(e.getAttribute('name'),
129 insert = e.getAttribute('insert'),
130 default = e.getAttribute('default'),
131 value = e.getAttribute('value'),
132 mode = mode,
133 separator = e.getAttribute('separator'))
134 if not binding.name: raise InvalidInterface(_("Missing 'name' in binding"))
135 if binding.insert is None and binding.value is None:
136 raise InvalidInterface(_("Missing 'insert' or 'value' in binding"))
137 if binding.insert is not None and binding.value is not None:
138 raise InvalidInterface(_("Binding contains both 'insert' and 'value'"))
139 return binding
140 elif e.name == 'overlay':
141 return OverlayBinding(e.getAttribute('src'), e.getAttribute('mount-point'))
142 else:
143 raise Exception(_("Unknown binding type '%s'") % e.name)
145 def process_depends(item, local_feed_dir):
146 """Internal"""
147 # Note: also called from selections
148 attrs = item.attrs
149 dep_iface = item.getAttribute('interface')
150 if not dep_iface:
151 raise InvalidInterface(_("Missing 'interface' on <%s>") % item.name)
152 if dep_iface.startswith('./'):
153 if local_feed_dir:
154 dep_iface = os.path.abspath(os.path.join(local_feed_dir, dep_iface))
155 # (updates the element too, in case we write it out again)
156 attrs['interface'] = dep_iface
157 else:
158 raise InvalidInterface(_('Relative interface URI "%s" in non-local feed') % dep_iface)
159 dependency = InterfaceDependency(dep_iface, element = item)
161 for e in item.childNodes:
162 if e.uri != XMLNS_IFACE: continue
163 if e.name in binding_names:
164 dependency.bindings.append(process_binding(e))
165 elif e.name == 'version':
166 dependency.restrictions.append(
167 VersionRangeRestriction(not_before = parse_version(e.getAttribute('not-before')),
168 before = parse_version(e.getAttribute('before'))))
169 return dependency
171 def N_(message): return message
173 insecure = Stability(0, N_('insecure'), _('This is a security risk'))
174 buggy = Stability(5, N_('buggy'), _('Known to have serious bugs'))
175 developer = Stability(10, N_('developer'), _('Work-in-progress - bugs likely'))
176 testing = Stability(20, N_('testing'), _('Stability unknown - please test!'))
177 stable = Stability(30, N_('stable'), _('Tested - no serious problems found'))
178 packaged = Stability(35, N_('packaged'), _('Supplied by the local package manager'))
179 preferred = Stability(40, N_('preferred'), _('Best of all - must be set manually'))
181 del N_
183 class Restriction(object):
184 """A Restriction limits the allowed implementations of an Interface."""
185 __slots__ = []
187 def meets_restriction(self, impl):
188 """Called by the L{solver.Solver} to check whether a particular implementation is acceptable.
189 @return: False if this implementation is not a possibility
190 @rtype: bool
192 raise NotImplementedError(_("Abstract"))
194 class VersionRestriction(Restriction):
195 """Only select implementations with a particular version number.
196 @since: 0.40"""
198 def __init__(self, version):
199 """@param version: the required version number
200 @see: L{parse_version}; use this to pre-process the version number
202 self.version = version
204 def meets_restriction(self, impl):
205 return impl.version == self.version
207 def __str__(self):
208 return _("(restriction: version = %s)") % format_version(self.version)
210 class VersionRangeRestriction(Restriction):
211 """Only versions within the given range are acceptable"""
212 __slots__ = ['before', 'not_before']
214 def __init__(self, before, not_before):
215 """@param before: chosen versions must be earlier than this
216 @param not_before: versions must be at least this high
217 @see: L{parse_version}; use this to pre-process the versions
219 self.before = before
220 self.not_before = not_before
222 def meets_restriction(self, impl):
223 if self.not_before and impl.version < self.not_before:
224 return False
225 if self.before and impl.version >= self.before:
226 return False
227 return True
229 def __str__(self):
230 if self.not_before is not None or self.before is not None:
231 range = ''
232 if self.not_before is not None:
233 range += format_version(self.not_before) + ' <= '
234 range += 'version'
235 if self.before is not None:
236 range += ' < ' + format_version(self.before)
237 else:
238 range = 'none'
239 return _("(restriction: %s)") % range
241 class Binding(object):
242 """Information about how the choice of a Dependency is made known
243 to the application being run."""
245 class EnvironmentBinding(Binding):
246 """Indicate the chosen implementation using an environment variable."""
247 __slots__ = ['name', 'insert', 'default', 'mode', 'value']
249 PREPEND = 'prepend'
250 APPEND = 'append'
251 REPLACE = 'replace'
253 def __init__(self, name, insert, default = None, mode = PREPEND,
254 value=None, separator=None):
256 mode argument added in version 0.28
257 value argument added in version 0.52
259 self.name = name
260 self.insert = insert
261 self.default = default
262 self.mode = mode
263 self.value = value
264 if separator is None:
265 self.separator = os.pathsep
266 else:
267 self.separator = separator
270 def __str__(self):
271 return _("<environ %(name)s %(mode)s %(insert)s %(value)s>") % {'name': self.name,'mode': self.mode, 'insert': self.insert, 'value': self.value}
273 __repr__ = __str__
275 def get_value(self, path, old_value):
276 """Calculate the new value of the environment variable after applying this binding.
277 @param path: the path to the selected implementation
278 @param old_value: the current value of the environment variable
279 @return: the new value for the environment variable"""
281 if self.insert is not None:
282 extra = os.path.join(path, self.insert)
283 else:
284 assert self.value is not None
285 extra = self.value
287 if self.mode == EnvironmentBinding.REPLACE:
288 return extra
290 if old_value is None:
291 old_value = self.default or defaults.get(self.name, None)
292 if old_value is None:
293 return extra
294 if self.mode == EnvironmentBinding.PREPEND:
295 return extra + self.separator + old_value
296 else:
297 return old_value + self.separator + extra
299 def _toxml(self, doc):
300 """Create a DOM element for this binding.
301 @param doc: document to use to create the element
302 @return: the new element
304 env_elem = doc.createElementNS(XMLNS_IFACE, 'environment')
305 env_elem.setAttributeNS(None, 'name', self.name)
306 if self.insert is not None:
307 env_elem.setAttributeNS(None, 'insert', self.insert)
308 else:
309 env_elem.setAttributeNS(None, 'value', self.value)
310 if self.default:
311 env_elem.setAttributeNS(None, 'default', self.default)
312 return env_elem
314 class OverlayBinding(Binding):
315 """Make the chosen implementation available by overlaying it onto another part of the file-system.
316 This is to support legacy programs which use hard-coded paths."""
317 __slots__ = ['src', 'mount_point']
319 def __init__(self, src, mount_point):
320 self.src = src
321 self.mount_point = mount_point
323 def __str__(self):
324 return _("<overlay %(src)s on %(mount_point)s>") % {'src': self.src or '.', 'mount_point': self.mount_point or '/'}
326 __repr__ = __str__
328 def _toxml(self, doc):
329 """Create a DOM element for this binding.
330 @param doc: document to use to create the element
331 @return: the new element
333 env_elem = doc.createElementNS(XMLNS_IFACE, 'overlay')
334 if self.src is not None:
335 env_elem.setAttributeNS(None, 'src', self.src)
336 if self.mount_point is not None:
337 env_elem.setAttributeNS(None, 'mount-point', self.mount_point)
338 return env_elem
340 class Feed(object):
341 """An interface's feeds are other interfaces whose implementations can also be
342 used as implementations of this interface."""
343 __slots__ = ['uri', 'os', 'machine', 'user_override', 'langs']
344 def __init__(self, uri, arch, user_override, langs = None):
345 self.uri = uri
346 # This indicates whether the feed comes from the user's overrides
347 # file. If true, writer.py will write it when saving.
348 self.user_override = user_override
349 self.os, self.machine = _split_arch(arch)
350 self.langs = langs
352 def __str__(self):
353 return "<Feed from %s>" % self.uri
354 __repr__ = __str__
356 arch = property(lambda self: _join_arch(self.os, self.machine))
358 class Dependency(object):
359 """A Dependency indicates that an Implementation requires some additional
360 code to function. This is an abstract base class.
361 @ivar qdom: the XML element for this Dependency (since 0launch 0.51)
362 @type qdom: L{qdom.Element}
363 @ivar metadata: any extra attributes from the XML element
364 @type metadata: {str: str}
366 __slots__ = ['qdom']
368 def __init__(self, element):
369 assert isinstance(element, qdom.Element), type(element) # Use InterfaceDependency instead!
370 self.qdom = element
372 @property
373 def metadata(self):
374 return self.qdom.attrs
376 class InterfaceDependency(Dependency):
377 """A Dependency on a Zero Install interface.
378 @ivar interface: the interface required by this dependency
379 @type interface: str
380 @ivar restrictions: a list of constraints on acceptable implementations
381 @type restrictions: [L{Restriction}]
382 @ivar bindings: how to make the choice of implementation known
383 @type bindings: [L{Binding}]
384 @since: 0.28
386 __slots__ = ['interface', 'restrictions', 'bindings']
388 def __init__(self, interface, restrictions = None, element = None):
389 Dependency.__init__(self, element)
390 assert isinstance(interface, (str, unicode))
391 assert interface
392 self.interface = interface
393 if restrictions is None:
394 self.restrictions = []
395 else:
396 self.restrictions = restrictions
397 self.bindings = []
399 def __str__(self):
400 return _("<Dependency on %(interface)s; bindings: %(bindings)s%(restrictions)s>") % {'interface': self.interface, 'bindings': self.bindings, 'restrictions': self.restrictions}
402 class RetrievalMethod(object):
403 """A RetrievalMethod provides a way to fetch an implementation."""
404 __slots__ = []
406 @tasks.async
407 def retrieve(self, fetcher, destination, force = False, impl_hint = None):
408 """Retrieve implementation using method
409 @param destination: where to put the retrieved files
410 @param impl_hint: the Implementation this is for (if any) as a hint for the GUI
412 raise NotImplementedError("abstract")
414 @property
415 def archives(self):
416 '''get all archives in this retrieval method'''
417 return ()
420 class DownloadSource(RetrievalMethod):
421 """A DownloadSource provides a way to fetch an implementation."""
422 __slots__ = ['implementation', 'url', 'size', 'extract', 'start_offset', 'type']
424 def __init__(self, implementation, url, size, extract, start_offset = 0, type = None):
425 self.implementation = implementation
426 self.url = url
427 self.size = size
428 self.extract = extract
429 self.start_offset = start_offset
430 self.type = type # MIME type - see unpack.py
432 @property
433 def archives(self):
434 return self
437 def _generate_size(self, fetcher):
438 dl = self._download(fetcher)
439 tasks.wait_for_blocker(dl.downloaded)
440 self.size = dl.get_bytes_downloaded_so_far()
442 @staticmethod
443 def fromDOM(elem, impl, generate_sizes=False, fetcher=None):
444 """Make a DownloadSource from a DOM archive element."""
446 url = elem.getAttribute('href')
447 if not url:
448 raise InvalidInterface(_("Missing href attribute on <archive>"))
450 size = elem.getAttribute('size')
451 if size:
452 size = int(size)
453 elif not generate_sizes:
454 raise InvalidInterface(_("Missing size attribute on <archive>"))
456 archive = DownloadSource(impl, url = url, size = size, extract = elem.getAttribute('extract'),
457 start_offset = _get_long(elem, 'start-offset'), type = elem.getAttribute('type'))
459 if not size and generate_sizes:
460 archive._generate_size(fetcher)
462 return archive
464 def prepare(self, fetcher, force, impl_hint):
466 class StepCommand(object):
467 __slots__ = ['blocker', '_stream']
469 def __init__(s):
470 s.blocker, s._stream = self.download(fetcher, force = force, impl_hint = impl_hint)
472 def run(s, destination):
473 s._stream.seek(0)
474 unpack.unpack_archive_over(self.url, s._stream, destination,
475 extract = self.extract,
476 type = self.type,
477 start_offset = self.start_offset or 0)
478 return StepCommand()
480 def download(self, fetcher, force = False, impl_hint = None):
481 """Fetch an archive. You should normally call L{Implementation.retrieve}
482 instead, since it handles other kinds of retrieval method too."""
483 dl = self._download(fetcher, force, impl_hint)
484 return (dl.downloaded, dl.tempfile)
486 def _download(self, fetcher, force = False, impl_hint = None):
487 url = self.url
488 if not (url.startswith('http:') or url.startswith('https:') or url.startswith('ftp:')):
489 raise SafeException(_("Unknown scheme in download URL '%s'") % url)
491 mime_type = self.type
492 if not mime_type:
493 mime_type = unpack.type_from_url(self.url)
494 if not mime_type:
495 raise SafeException(_("No 'type' attribute on archive, and I can't guess from the name (%s)") % self.url)
496 unpack.check_type_ok(mime_type)
497 dl = fetcher.handler.get_download(self.url, force = force, hint = impl_hint)
498 if self.size:
499 dl.expected_size = self.size + (self.start_offset or 0)
500 return dl
502 @tasks.async
503 def retrieve(self, fetcher, destination, force = False, impl_hint = None):
504 command = self.prepare(fetcher, force, impl_hint)
505 yield command.blocker
506 tasks.check(command.blocker)
507 command.run(destination)
510 class UnpackArchive(RetrievalMethod):
511 """An UnpackArchive step provides unpacks/extracts an archive.
513 It can be used inside a Recipe."""
514 __slots__ = ['path', 'extract', 'type']
516 def __init__(self, path, extract, type):
517 self.path = path
518 self.extract = extract
519 self.type = type
521 def prepare(self, fetcher, force, impl_hint):
523 class StepCommand(object):
524 __slots__ = ['blocker']
526 def __init__(s):
527 s.blocker = None
529 def run(s, tmpdir):
530 path = os.path.join(tmpdir, self.path)
531 stream = open(path, 'rb')
532 stream.seek(0)
534 unpack.unpack_archive_over(path, stream, tmpdir,
535 extract = self.extract,
536 type = self.type,
537 start_offset = 0)
539 os.unlink(path)
541 return StepCommand()
543 class Recipe(RetrievalMethod):
544 """Get an implementation by following a series of steps.
545 @ivar size: the combined download sizes from all the steps
546 @type size: int
547 @ivar steps: the sequence of steps which must be performed
548 @type steps: [L{RetrievalMethod}]"""
549 __slots__ = ['steps']
551 def __init__(self):
552 self.steps = []
554 size = property(lambda self: sum([x.size for x in self.steps]))
556 @property
557 def archives(self):
558 '''get all archives in this retrieval method'''
559 from itertools import chain
560 return chain(*[step.archives for step in self.steps])
562 @tasks.async
563 def retrieve(self, fetcher, destination, force = False, impl_hint = None):
564 # Start preparing all steps
565 step_commands = [step.prepare(fetcher, force, impl_hint) for step in self.steps]
567 # Run steps
568 valid_blockers = [s.blocker for s in step_commands if s.blocker is not None]
569 for step_command in step_commands:
570 if step_command.blocker:
571 while not step_command.blocker.happened:
572 yield valid_blockers
573 tasks.check(valid_blockers)
574 step_command.run(destination)
576 @staticmethod
577 def fromDOM(elem, generate_sizes=False, fetcher=None):
578 """Make a Recipe from a DOM recipe element"""
579 recipe = Recipe()
580 for recipe_step in elem.childNodes:
581 if recipe_step.uri == XMLNS_IFACE and recipe_step.name == 'archive':
582 recipe.steps.append(DownloadSource.fromDOM(recipe_step, None, generate_sizes, fetcher))
583 elif recipe_step.uri == XMLNS_IFACE and recipe_step.name == 'unpack':
584 path = recipe_step.getAttribute('path')
585 if not path:
586 raise InvalidInterface(_("Missing path attribute on <unpack>"))
587 recipe.steps.append(UnpackArchive(path = path,
588 extract = recipe_step.getAttribute('extract'),
589 type = recipe_step.getAttribute('type')))
590 else:
591 info(_("Unknown step '%s' in recipe; skipping recipe"), recipe_step.name)
592 return None
593 else:
594 return recipe
596 class DistributionSource(RetrievalMethod):
597 """A package that is installed using the distribution's tools (including PackageKit).
598 @ivar install: a function to call to install this package
599 @type install: (L{handler.Handler}) -> L{tasks.Blocker}
600 @ivar package_id: the package name, in a form recognised by the distribution's tools
601 @type package_id: str
602 @ivar size: the download size in bytes
603 @type size: int
604 @ivar needs_confirmation: whether the user should be asked to confirm before calling install()
605 @type needs_confirmation: bool"""
607 __slots__ = ['package_id', 'size', 'install', 'needs_confirmation']
609 def __init__(self, package_id, size, install, needs_confirmation = True):
610 RetrievalMethod.__init__(self)
611 self.package_id = package_id
612 self.size = size
613 self.install = install
614 self.needs_confirmation = needs_confirmation
616 class Command(object):
617 """A Command is a way of running an Implementation as a program."""
619 __slots__ = ['qdom', '_depends', '_local_dir', '_runner']
621 def __init__(self, qdom, local_dir):
622 """@param qdom: the <command> element
623 @param local_dir: the directory containing the feed (for relative dependencies), or None if not local
625 assert qdom.name == 'command', 'not <command>: %s' % qdom
626 self.qdom = qdom
627 self._local_dir = local_dir
628 self._depends = None
630 path = property(lambda self: self.qdom.attrs.get("path", None))
632 def _toxml(self, doc, prefixes):
633 return self.qdom.toDOM(doc, prefixes)
635 @property
636 def requires(self):
637 if self._depends is None:
638 self._runner = None
639 depends = []
640 for child in self.qdom.childNodes:
641 if child.name == 'requires':
642 dep = process_depends(child, self._local_dir)
643 depends.append(dep)
644 elif child.name == 'runner':
645 if self._runner:
646 raise InvalidInterface(_("Multiple <runner>s in <command>!"))
647 dep = process_depends(child, self._local_dir)
648 depends.append(dep)
649 self._runner = dep
650 self._depends = depends
651 return self._depends
653 def get_runner(self):
654 self.requires # (sets _runner)
655 return self._runner
657 class Implementation(object):
658 """An Implementation is a package which implements an Interface.
659 @ivar download_sources: list of methods of getting this implementation
660 @type download_sources: [L{RetrievalMethod}]
661 @ivar feed: the feed owning this implementation (since 0.32)
662 @type feed: [L{ZeroInstallFeed}]
663 @ivar bindings: how to tell this component where it itself is located (since 0.31)
664 @type bindings: [Binding]
665 @ivar upstream_stability: the stability reported by the packager
666 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged]
667 @ivar user_stability: the stability as set by the user
668 @type upstream_stability: [insecure | buggy | developer | testing | stable | packaged | preferred]
669 @ivar langs: natural languages supported by this package
670 @type langs: str
671 @ivar requires: interfaces this package depends on
672 @type requires: [L{Dependency}]
673 @ivar commands: ways to execute as a program
674 @type commands: {str: Command}
675 @ivar metadata: extra metadata from the feed
676 @type metadata: {"[URI ]localName": str}
677 @ivar id: a unique identifier for this Implementation
678 @ivar version: a parsed version number
679 @ivar released: release date
680 @ivar local_path: the directory containing this local implementation, or None if it isn't local (id isn't a path)
681 @type local_path: str | None
682 @ivar requires_root_install: whether the user will need admin rights to use this
683 @type requires_root_install: bool
686 # Note: user_stability shouldn't really be here
688 __slots__ = ['upstream_stability', 'user_stability', 'langs',
689 'requires', 'metadata', 'download_sources', 'commands',
690 'id', 'feed', 'version', 'released', 'bindings', 'machine']
692 def __init__(self, feed, id):
693 self.feed = feed
694 self.id = id
695 self.user_stability = None
696 self.upstream_stability = None
697 self.metadata = {} # [URI + " "] + localName -> value
698 self.requires = []
699 self.version = None
700 self.released = None
701 self.download_sources = []
702 self.langs = ""
703 self.machine = None
704 self.bindings = []
705 self.commands = {}
707 def get_stability(self):
708 return self.user_stability or self.upstream_stability or testing
710 def __str__(self):
711 return self.id
713 def __repr__(self):
714 return "v%s (%s)" % (self.get_version(), self.id)
716 def __cmp__(self, other):
717 """Newer versions come first"""
718 d = cmp(other.version, self.version)
719 if d: return d
720 # If the version number is the same, just give a stable sort order, and
721 # ensure that two different implementations don't compare equal.
722 d = cmp(other.feed.url, self.feed.url)
723 if d: return d
724 return cmp(other.id, self.id)
726 def get_version(self):
727 """Return the version as a string.
728 @see: L{format_version}
730 return format_version(self.version)
732 arch = property(lambda self: _join_arch(self.os, self.machine))
734 os = None
735 local_path = None
736 digests = None
737 requires_root_install = False
739 def _get_main(self):
740 """"@deprecated: use commands["run"] instead"""
741 main = self.commands.get("run", None)
742 if main is not None:
743 return main.path
744 return None
745 def _set_main(self, path):
746 """"@deprecated: use commands["run"] instead"""
747 if path is None:
748 if "run" in self.commands:
749 del self.commands["run"]
750 else:
751 self.commands["run"] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': path}), None)
752 main = property(_get_main, _set_main)
754 def is_available(self, stores):
755 """Is this Implementation available locally?
756 (a local implementation, an installed distribution package, or a cached ZeroInstallImplementation)
757 @rtype: bool
758 @since: 0.53
760 raise NotImplementedError("abstract")
762 @property
763 def best_download_source(self):
764 """Return the best download source for this implementation.
765 @rtype: L{model.RetrievalMethod}"""
766 if self.download_sources:
767 return self.download_sources[0]
768 return None
770 def retrieve(self, fetcher, retrieval_method, stores, force = False):
771 """Retrieve an implementation.
772 @param retrieval_method: a way of getting the implementation (e.g. an Archive or a Recipe)
773 @type retrieval_method: L{model.RetrievalMethod}
774 @param stores: where to store the downloaded implementation
775 @type stores: L{zerostore.Stores}
776 @param force: whether to abort and restart an existing download
777 @rtype: L{tasks.Blocker}"""
778 raise NotImplementedError("abstract")
780 @property
781 def archives(self):
782 '''get all archives in this implementation'''
783 return ()
786 class DistributionImplementation(Implementation):
787 """An implementation provided by the distribution. Information such as the version
788 comes from the package manager.
789 @since: 0.28"""
790 __slots__ = ['distro', 'installed']
792 def __init__(self, feed, id, distro):
793 assert id.startswith('package:')
794 Implementation.__init__(self, feed, id)
795 self.distro = distro
796 self.installed = False
798 @property
799 def requires_root_install(self):
800 return not self.installed
802 def is_available(self, stores):
803 return self.installed
805 def retrieve(self, fetcher, retrieval_method, stores, force = False):
806 return retrieval_method.install(fetcher.handler)
809 class ZeroInstallImplementation(Implementation):
810 """An implementation where all the information comes from Zero Install.
811 @ivar digests: a list of "algorith=value" strings (since 0.45)
812 @type digests: [str]
813 @since: 0.28"""
814 __slots__ = ['os', 'size', 'digests', 'local_path']
816 def __init__(self, feed, id, local_path):
817 """id can be a local path (string starting with /) or a manifest hash (eg "sha1=XXX")"""
818 if id:
819 assert not id.startswith('package:'), id
820 Implementation.__init__(self, feed, id)
821 self.size = None
822 self.os = None
823 self.digests = []
824 self.local_path = local_path
826 @property
827 def archives(self):
828 '''get all archives in this implementation'''
829 from itertools import chain
830 return chain([method.archives for method in self.download_sources])
833 @staticmethod
834 def fromDOM(feed, item, item_attrs, local_dir, commands, bindings, depends,
835 generate_sizes=False, id_generation_alg=None, fetcher=None, stores=None):
836 """Make an implementation from a DOM implementation element.
837 @param generate_sizes: if True, sizes of archives with missing size attributes will be generated
838 @type generate_sizes: bool
839 @param id_generation_alg: if specified, id will be autogenerated, if id is None, with this alg
840 @type id_generation_alg: L{Algorithm}
841 @param fetcher: must be specified if id_generation_alg/generate_sizes is specified/True
842 @param stores: must be specified if id_generation_alg is specified
844 id = item.getAttribute('id')
845 local_path = item_attrs.get('local-path')
846 if local_dir and local_path:
847 abs_local_path = os.path.abspath(os.path.join(local_dir, local_path))
848 impl = ZeroInstallImplementation(feed, id, abs_local_path)
849 elif local_dir and (id and (id.startswith('/') or id.startswith('.'))):
850 # For old feeds
851 id = os.path.abspath(os.path.join(local_dir, id))
852 impl = ZeroInstallImplementation(feed, id, id)
853 else:
854 impl = ZeroInstallImplementation(feed, id, None)
855 if id and '=' in id:
856 # In older feeds, the ID was the (single) digest
857 impl.digests.append(id)
859 try:
860 version_mod = item_attrs.get('version-modifier', None)
861 if version_mod:
862 item_attrs['version'] += version_mod
863 del item_attrs['version-modifier']
864 version = item_attrs['version']
865 except KeyError:
866 raise InvalidInterface(_("Missing version attribute"))
867 impl.version = parse_version(version)
869 impl.metadata = item_attrs
870 impl.commands = commands
871 impl.bindings = bindings
872 impl.requires = depends
873 impl.released = item_attrs.get('released', None)
874 impl.langs = item_attrs.get('langs', '').replace('_', '-')
876 size = item.getAttribute('size')
877 if size:
878 impl.size = int(size)
880 impl.arch = item_attrs.get('arch', None)
882 try:
883 stability = stability_levels[str(item_attrs['stability'])]
884 except KeyError:
885 stab = str(item_attrs['stability'])
886 if stab != stab.lower():
887 raise InvalidInterface(_('Stability "%s" invalid - use lower case!') % item_attrs.stability)
888 raise InvalidInterface(_('Stability "%s" invalid') % item_attrs['stability'])
889 if stability >= preferred:
890 raise InvalidInterface(_("Upstream can't set stability to preferred!"))
891 impl.upstream_stability = stability
893 for elem in item.childNodes:
894 if elem.uri != XMLNS_IFACE: continue
895 if elem.name == 'archive':
896 impl.download_sources.append(DownloadSource.fromDOM(elem, impl, generate_sizes, fetcher))
897 elif elem.name == 'manifest-digest':
898 for aname, avalue in elem.attrs.iteritems():
899 if ' ' not in aname:
900 impl.digests.append('%s=%s' % (aname, avalue))
901 elif elem.name == 'recipe':
902 recipe = Recipe.fromDOM(elem, generate_sizes, fetcher)
903 if recipe:
904 impl.download_sources.append(recipe)
906 if id is None and id_generation_alg:
907 assert fetcher
908 assert stores
909 impl.id = impl._generate_digest(fetcher, stores, id_generation_alg)
910 feed.changed_implementations.append(impl)
911 if impl.id is None:
912 raise InvalidInterface(_("Missing 'id' attribute on %s") % item)
914 return impl
917 # Deprecated
918 dependencies = property(lambda self: dict([(x.interface, x) for x in self.requires
919 if isinstance(x, InterfaceDependency)]))
921 def add_download_source(self, url, size, extract, start_offset = 0, type = None):
922 """Add a download source."""
923 # TODO should deprecate?
924 self.download_sources.append(DownloadSource(self, url, size, extract, start_offset, type))
926 def set_arch(self, arch):
927 self.os, self.machine = _split_arch(arch)
928 arch = property(lambda self: _join_arch(self.os, self.machine), set_arch)
930 def is_available(self, stores):
931 if self.local_path is not None:
932 return os.path.exists(self.local_path)
933 if self.digests:
934 path = stores.lookup_maybe(self.digests)
935 return path is not None
936 return False # (0compile creates fake entries with no digests)
938 @property
939 def best_digest(self):
940 """Return the best digest for this implementation
941 @return: tuple (alg, digest) or None"""
942 from zeroinstall.zerostore import manifest
943 best_alg = None
944 for digest in self.digests:
945 alg_name = digest.split('=', 1)[0]
946 alg = manifest.algorithms.get(alg_name, None)
947 if alg and (best_alg is None or best_alg.rating < alg.rating):
948 best_alg = alg
949 best_digest = digest
950 if best_alg:
951 return (best_alg, best_digest)
952 else:
953 return None
955 def _generate_digest(self, fetcher, stores, alg):
956 digest = None
958 # Create an empty directory for the new implementation
959 store = stores.stores[0]
960 tmpdir = store.get_tmp_dir_for('missing')
962 try:
963 blocker = self.best_download_source.retrieve(fetcher, tmpdir, force=False, impl_hint = self)
964 tasks.wait_for_blocker(blocker)
966 from zeroinstall.zerostore import manifest
967 manifest.fixup_permissions(tmpdir)
968 digest = alg.getID(manifest.add_manifest_file(tmpdir, alg))
969 finally:
970 # If unpacking fails, remove the temporary directory
971 if tmpdir is not None:
972 from zeroinstall import support
973 support.ro_rmtree(tmpdir)
975 return digest
978 def retrieve(self, fetcher, retrieval_method, stores, force = False):
979 best = self.best_digest
981 if best is None:
982 if not self.digests:
983 raise SafeException(_("No <manifest-digest> given for '%(implementation)s' version %(version)s") %
984 {'implementation': self.feed.get_name(), 'version': self.get_version()})
985 raise SafeException(_("Unknown digest algorithms '%(algorithms)s' for '%(implementation)s' version %(version)s") %
986 {'algorithms': self.digests, 'implementation': self.feed.get_name(), 'version': self.get_version()})
987 else:
988 alg, required_digest = best
990 @tasks.async
991 def retrieve():
992 # Create an empty directory for the new implementation
993 store = stores.stores[0]
994 tmpdir = store.get_tmp_dir_for(required_digest)
996 try:
997 blocker = retrieval_method.retrieve(fetcher, tmpdir, force, impl_hint = self)
998 yield blocker
999 tasks.check(blocker)
1001 # Check that the result is correct and store it in the cache
1002 store.check_manifest_and_rename(required_digest, tmpdir)
1004 tmpdir = None
1005 finally:
1006 # If unpacking fails, remove the temporary directory
1007 if tmpdir is not None:
1008 from zeroinstall import support
1009 support.ro_rmtree(tmpdir)
1011 fetcher.handler.impl_added_to_store(self)
1013 return retrieve()
1016 class Interface(object):
1017 """An Interface represents some contract of behaviour.
1018 @ivar uri: the URI for this interface.
1019 @ivar stability_policy: user's configured policy.
1020 Implementations at this level or higher are preferred.
1021 Lower levels are used only if there is no other choice.
1023 __slots__ = ['uri', 'stability_policy', 'extra_feeds']
1025 implementations = property(lambda self: self._main_feed.implementations)
1026 name = property(lambda self: self._main_feed.name)
1027 description = property(lambda self: self._main_feed.description)
1028 summary = property(lambda self: self._main_feed.summary)
1029 last_modified = property(lambda self: self._main_feed.last_modified)
1030 feeds = property(lambda self: self.extra_feeds + self._main_feed.feeds)
1031 metadata = property(lambda self: self._main_feed.metadata)
1033 last_checked = property(lambda self: self._main_feed.last_checked)
1035 def __init__(self, uri):
1036 assert uri
1037 if uri.startswith('http:') or uri.startswith('https:') or os.path.isabs(uri):
1038 self.uri = uri
1039 else:
1040 raise SafeException(_("Interface name '%s' doesn't start "
1041 "with 'http:' or 'https:'") % uri)
1042 self.reset()
1044 def _get_feed_for(self):
1045 retval = {}
1046 for key in self._main_feed.feed_for:
1047 retval[key] = True
1048 return retval
1049 feed_for = property(_get_feed_for) # Deprecated (used by 0publish)
1051 def reset(self):
1052 self.extra_feeds = []
1053 self.stability_policy = None
1055 def get_name(self):
1056 from zeroinstall.injector.iface_cache import iface_cache
1057 feed = iface_cache.get_feed(self.uri)
1058 if feed:
1059 return feed.get_name()
1060 return '(' + os.path.basename(self.uri) + ')'
1062 def __repr__(self):
1063 return _("<Interface %s>") % self.uri
1065 def set_stability_policy(self, new):
1066 assert new is None or isinstance(new, Stability)
1067 self.stability_policy = new
1069 def get_feed(self, url):
1070 #import warnings
1071 #warnings.warn("use iface_cache.get_feed instead", DeprecationWarning, 2)
1072 for x in self.extra_feeds:
1073 if x.uri == url:
1074 return x
1075 #return self._main_feed.get_feed(url)
1076 return None
1078 def get_metadata(self, uri, name):
1079 return self._main_feed.get_metadata(uri, name)
1081 @property
1082 def _main_feed(self):
1083 #import warnings
1084 #warnings.warn("use the feed instead", DeprecationWarning, 3)
1085 from zeroinstall.injector import policy
1086 iface_cache = policy.get_deprecated_singleton_config().iface_cache
1087 feed = iface_cache.get_feed(self.uri)
1088 if feed is None:
1089 return _dummy_feed
1090 return feed
1092 def _merge_attrs(attrs, item):
1093 """Add each attribute of item to a copy of attrs and return the copy.
1094 @type attrs: {str: str}
1095 @type item: L{qdom.Element}
1096 @rtype: {str: str}
1098 new = attrs.copy()
1099 for a in item.attrs:
1100 new[str(a)] = item.attrs[a]
1101 return new
1103 def _get_long(elem, attr_name):
1104 val = elem.getAttribute(attr_name)
1105 if val is not None:
1106 try:
1107 val = int(val)
1108 except ValueError:
1109 raise SafeException(_("Invalid value for integer attribute '%(attribute_name)s': %(value)s") % {'attribute_name': attr_name, 'value': val})
1110 return val
1112 class ZeroInstallFeed(object):
1113 """A feed lists available implementations of an interface.
1114 @ivar url: the URL for this feed
1115 @ivar implementations: Implementations in this feed, indexed by ID
1116 @type implementations: {str: L{Implementation}}
1117 @ivar changed_implementations: Ordered list of implementations that had their ID changed (i.e. they had their id changed)
1118 @ivar name: human-friendly name
1119 @ivar summaries: short textual description (in various languages, since 0.49)
1120 @type summaries: {str: str}
1121 @ivar descriptions: long textual description (in various languages, since 0.49)
1122 @type descriptions: {str: str}
1123 @ivar last_modified: timestamp on signature
1124 @ivar last_checked: time feed was last successfully downloaded and updated
1125 @ivar feeds: list of <feed> elements in this feed
1126 @type feeds: [L{Feed}]
1127 @ivar feed_for: interfaces for which this could be a feed
1128 @type feed_for: set(str)
1129 @ivar metadata: extra elements we didn't understand
1131 # _main is deprecated
1132 __slots__ = ['url', 'implementations', 'changed_implementations', 'name', 'descriptions', 'first_description', 'summaries', 'first_summary', '_package_implementations',
1133 'last_checked', 'last_modified', 'feeds', 'feed_for', 'metadata']
1135 def __init__(self, feed_element, local_path = None, distro = None, generate_sizes = False,
1136 implementation_id_alg=None, fetcher=None, stores=None):
1137 """Create a feed object from a DOM.
1138 @param feed_element: the root element of a feed file
1139 @type feed_element: L{qdom.Element}
1140 @param local_path: the pathname of this local feed, or None for remote feeds
1141 @param generate_sizes: if True, sizes of archives with missing size attributes will be generated
1142 @type generate_sizes: bool
1143 @param implementation_id_alg: if specified, missing impl ids will be generated with this alg
1144 @type implementation_id_alg: L{Algorithm}
1145 @param fetcher: cannot be None if implementation_id_alg is specified
1146 @param stores: cannot be None if implementation_id_alg is specified"""
1147 self.implementations = {}
1148 self.changed_implementations = []
1149 self.name = None
1150 self.summaries = {} # { lang: str }
1151 self.first_summary = None
1152 self.descriptions = {} # { lang: str }
1153 self.first_description = None
1154 self.last_modified = None
1155 self.feeds = []
1156 self.feed_for = set()
1157 self.metadata = []
1158 self.last_checked = None
1159 self._package_implementations = []
1161 if distro is not None:
1162 import warnings
1163 warnings.warn("distro argument is now ignored", DeprecationWarning, 2)
1165 if feed_element is None:
1166 return # XXX subclass?
1168 assert feed_element.name in ('interface', 'feed'), "Root element should be <interface>, not %s" % feed_element
1169 assert feed_element.uri == XMLNS_IFACE, "Wrong namespace on root element: %s" % feed_element.uri
1171 main = feed_element.getAttribute('main')
1172 #if main: warn("Setting 'main' on the root element is deprecated. Put it on a <group> instead")
1174 if local_path:
1175 self.url = local_path
1176 local_dir = os.path.dirname(local_path)
1177 else:
1178 self.url = feed_element.getAttribute('uri')
1179 if not self.url:
1180 raise InvalidInterface(_("<interface> uri attribute missing"))
1181 local_dir = None # Can't have relative paths
1183 min_injector_version = feed_element.getAttribute('min-injector-version')
1184 if min_injector_version:
1185 if parse_version(min_injector_version) > parse_version(version):
1186 raise InvalidInterface(_("This feed requires version %(min_version)s or later of "
1187 "Zero Install, but I am only version %(version)s. "
1188 "You can get a newer version from http://0install.net") %
1189 {'min_version': min_injector_version, 'version': version})
1191 for x in feed_element.childNodes:
1192 if x.uri != XMLNS_IFACE:
1193 self.metadata.append(x)
1194 continue
1195 if x.name == 'name':
1196 self.name = x.content
1197 elif x.name == 'description':
1198 if self.first_description == None:
1199 self.first_description = x.content
1200 self.descriptions[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", 'en')] = x.content
1201 elif x.name == 'summary':
1202 if self.first_summary == None:
1203 self.first_summary = x.content
1204 self.summaries[x.attrs.get("http://www.w3.org/XML/1998/namespace lang", 'en')] = x.content
1205 elif x.name == 'feed-for':
1206 feed_iface = x.getAttribute('interface')
1207 if not feed_iface:
1208 raise InvalidInterface(_('Missing "interface" attribute in <feed-for>'))
1209 self.feed_for.add(feed_iface)
1210 # Bug report from a Debian/stable user that --feed gets the wrong value.
1211 # Can't reproduce (even in a Debian/stable chroot), but add some logging here
1212 # in case it happens again.
1213 debug(_("Is feed-for %s"), feed_iface)
1214 elif x.name == 'feed':
1215 feed_src = x.getAttribute('src')
1216 if not feed_src:
1217 raise InvalidInterface(_('Missing "src" attribute in <feed>'))
1218 if feed_src.startswith('http:') or feed_src.startswith('https:') or local_path:
1219 langs = x.getAttribute('langs')
1220 if langs: langs = langs.replace('_', '-')
1221 self.feeds.append(Feed(feed_src, x.getAttribute('arch'), False, langs = langs))
1222 else:
1223 raise InvalidInterface(_("Invalid feed URL '%s'") % feed_src)
1224 else:
1225 self.metadata.append(x)
1227 if not self.name:
1228 raise InvalidInterface(_("Missing <name> in feed"))
1229 if not self.summary:
1230 raise InvalidInterface(_("Missing <summary> in feed"))
1232 def process_group(group, group_attrs, base_depends, base_bindings, base_commands):
1233 for item in group.childNodes:
1234 if item.uri != XMLNS_IFACE: continue
1236 if item.name not in ('group', 'implementation', 'package-implementation'):
1237 continue
1239 # We've found a group or implementation. Scan for dependencies,
1240 # bindings and commands. Doing this here means that:
1241 # - We can share the code for groups and implementations here.
1242 # - The order doesn't matter, because these get processed first.
1243 # A side-effect is that the document root cannot contain
1244 # these.
1246 depends = base_depends[:]
1247 bindings = base_bindings[:]
1248 commands = base_commands.copy()
1250 for attr, command in [('main', 'run'),
1251 ('self-test', 'test')]:
1252 value = item.attrs.get(attr, None)
1253 if value is not None:
1254 commands[command] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': value}), None)
1256 for child in item.childNodes:
1257 if child.uri != XMLNS_IFACE: continue
1258 if child.name == 'requires':
1259 dep = process_depends(child, local_dir)
1260 depends.append(dep)
1261 elif child.name == 'command':
1262 command_name = child.attrs.get('name', None)
1263 if not command_name:
1264 raise InvalidInterface('Missing name for <command>')
1265 commands[command_name] = Command(child, local_dir)
1266 elif child.name in binding_names:
1267 bindings.append(process_binding(child))
1269 compile_command = item.attrs.get('http://zero-install.sourceforge.net/2006/namespaces/0compile command')
1270 if compile_command is not None:
1271 commands['compile'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'shell-command': compile_command}), None)
1273 item_attrs = _merge_attrs(group_attrs, item)
1275 if item.name == 'group':
1276 process_group(item, item_attrs, depends, bindings, commands)
1277 elif item.name == 'implementation':
1278 impl = ZeroInstallImplementation.fromDOM(self, item, item_attrs, local_dir, commands, bindings, depends,
1279 generate_sizes, implementation_id_alg, fetcher, stores)
1280 if impl.id in self.implementations:
1281 warn(_("Duplicate ID '%(id)s' in feed '%(feed)s'"), {'id': id, 'feed': self})
1282 self.implementations[impl.id] = impl
1283 elif item.name == 'package-implementation':
1284 if depends:
1285 warn("A <package-implementation> with dependencies in %s!", self.url)
1286 self._package_implementations.append((item, item_attrs))
1287 else:
1288 assert 0
1290 root_attrs = {'stability': 'testing'}
1291 root_commands = {}
1292 if main:
1293 info("Note: @main on document element is deprecated in %s", self)
1294 root_commands['run'] = Command(qdom.Element(XMLNS_IFACE, 'command', {'path': main}), None)
1295 process_group(feed_element, root_attrs, [], [], root_commands)
1297 @property
1298 def archives(self):
1299 '''get all archives in this feed'''
1300 from itertools import chain
1301 return chain(*[impl.archives for impl in self.implementations.itervalues()])
1303 def get_distro_feed(self):
1304 """Does this feed contain any <pacakge-implementation> elements?
1305 i.e. is it worth asking the package manager for more information?
1306 @return: the URL of the virtual feed, or None
1307 @since: 0.49"""
1308 if self._package_implementations:
1309 return "distribution:" + self.url
1310 return None
1312 def get_package_impls(self, distro):
1313 """Find the best <pacakge-implementation> element(s) for the given distribution.
1314 @param distro: the distribution to use to rate them
1315 @type distro: L{distro.Distribution}
1316 @return: a list of tuples for the best ranked elements
1317 @rtype: [str]
1318 @since: 0.49"""
1319 best_score = 0
1320 best_impls = []
1322 for item, item_attrs in self._package_implementations:
1323 distro_names = item_attrs.get('distributions', '')
1324 for distro_name in distro_names.split(' '):
1325 score = distro.get_score(distro_name)
1326 if score > best_score:
1327 best_score = score
1328 best_impls = []
1329 if score == best_score:
1330 best_impls.append((item, item_attrs))
1331 return best_impls
1333 def get_name(self):
1334 return self.name or '(' + os.path.basename(self.url) + ')'
1336 def __repr__(self):
1337 return _("<Feed %s>") % self.url
1339 def set_stability_policy(self, new):
1340 assert new is None or isinstance(new, Stability)
1341 self.stability_policy = new
1343 def get_feed(self, url):
1344 for x in self.feeds:
1345 if x.uri == url:
1346 return x
1347 return None
1349 def add_metadata(self, elem):
1350 self.metadata.append(elem)
1352 def get_metadata(self, uri, name):
1353 """Return a list of interface metadata elements with this name and namespace URI."""
1354 return [m for m in self.metadata if m.name == name and m.uri == uri]
1356 @property
1357 def summary(self):
1358 return _best_language_match(self.summaries) or self.first_summary
1360 @property
1361 def description(self):
1362 return _best_language_match(self.descriptions) or self.first_description
1364 class DummyFeed(object):
1365 """Temporary class used during API transition."""
1366 last_modified = None
1367 name = '-'
1368 last_checked = property(lambda self: None)
1369 implementations = property(lambda self: {})
1370 feeds = property(lambda self: [])
1371 summary = property(lambda self: '-')
1372 description = property(lambda self: '')
1373 def get_name(self): return self.name
1374 def get_feed(self, url): return None
1375 def get_metadata(self, uri, name): return []
1376 _dummy_feed = DummyFeed()
1378 def unescape(uri):
1379 """Convert each %20 to a space, etc.
1380 @rtype: str"""
1381 uri = uri.replace('#', '/')
1382 if '%' not in uri: return uri
1383 return re.sub('%[0-9a-fA-F][0-9a-fA-F]',
1384 lambda match: chr(int(match.group(0)[1:], 16)),
1385 uri).decode('utf-8')
1387 def escape(uri):
1388 """Convert each space to %20, etc
1389 @rtype: str"""
1390 return re.sub('[^-_.a-zA-Z0-9]',
1391 lambda match: '%%%02x' % ord(match.group(0)),
1392 uri.encode('utf-8'))
1394 def _pretty_escape(uri):
1395 """Convert each space to %20, etc
1396 : is preserved and / becomes #. This makes for nicer strings,
1397 and may replace L{escape} everywhere in future.
1398 @rtype: str"""
1399 if os.name == "posix":
1400 # Only preserve : on Posix systems
1401 preserveRegex = '[^-_.a-zA-Z0-9:/]'
1402 else:
1403 # Other OSes may not allow the : character in file names
1404 preserveRegex = '[^-_.a-zA-Z0-9/]'
1405 return re.sub(preserveRegex,
1406 lambda match: '%%%02x' % ord(match.group(0)),
1407 uri.encode('utf-8')).replace('/', '#')
1409 def canonical_iface_uri(uri):
1410 """If uri is a relative path, convert to an absolute one.
1411 A "file:///foo" URI is converted to "/foo".
1412 An "alias:prog" URI expands to the URI in the 0alias script
1413 Otherwise, return it unmodified.
1414 @rtype: str
1415 @raise SafeException: if uri isn't valid
1417 if uri.startswith('http://') or uri.startswith('https://'):
1418 if uri.count("/") < 3:
1419 raise SafeException(_("Missing / after hostname in URI '%s'") % uri)
1420 return uri
1421 elif uri.startswith('file:///'):
1422 return uri[7:]
1423 elif uri.startswith('alias:'):
1424 from zeroinstall import alias, support
1425 alias_prog = uri[6:]
1426 if not os.path.isabs(alias_prog):
1427 full_path = support.find_in_path(alias_prog)
1428 if not full_path:
1429 raise alias.NotAnAliasScript("Not found in $PATH: " + alias_prog)
1430 else:
1431 full_path = alias_prog
1432 interface_uri, main = alias.parse_script(full_path)
1433 return interface_uri
1434 else:
1435 iface_uri = os.path.realpath(uri)
1436 if os.path.isfile(iface_uri):
1437 return iface_uri
1438 raise SafeException(_("Bad interface name '%(uri)s'.\n"
1439 "(doesn't start with 'http:', and "
1440 "doesn't exist as a local file '%(interface_uri)s' either)") %
1441 {'uri': uri, 'interface_uri': iface_uri})
1443 _version_mod_to_value = {
1444 'pre': -2,
1445 'rc': -1,
1446 '': 0,
1447 'post': 1,
1450 # Reverse mapping
1451 _version_value_to_mod = {}
1452 for x in _version_mod_to_value: _version_value_to_mod[_version_mod_to_value[x]] = x
1453 del x
1455 _version_re = re.compile('-([a-z]*)')
1457 def parse_version(version_string):
1458 """Convert a version string to an internal representation.
1459 The parsed format can be compared quickly using the standard Python functions.
1460 - Version := DottedList ("-" Mod DottedList?)*
1461 - DottedList := (Integer ("." Integer)*)
1462 @rtype: tuple (opaque)
1463 @raise SafeException: if the string isn't a valid version
1464 @since: 0.24 (moved from L{reader}, from where it is still available):"""
1465 if version_string is None: return None
1466 parts = _version_re.split(version_string)
1467 if parts[-1] == '':
1468 del parts[-1] # Ends with a modifier
1469 else:
1470 parts.append('')
1471 if not parts:
1472 raise SafeException(_("Empty version string!"))
1473 l = len(parts)
1474 try:
1475 for x in range(0, l, 2):
1476 part = parts[x]
1477 if part:
1478 parts[x] = map(int, parts[x].split('.'))
1479 else:
1480 parts[x] = [] # (because ''.split('.') == [''], not [])
1481 for x in range(1, l, 2):
1482 parts[x] = _version_mod_to_value[parts[x]]
1483 return parts
1484 except ValueError, ex:
1485 raise SafeException(_("Invalid version format in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1486 except KeyError, ex:
1487 raise SafeException(_("Invalid version modifier in '%(version_string)s': %(exception)s") % {'version_string': version_string, 'exception': ex})
1489 def format_version(version):
1490 """Format a parsed version for display. Undoes the effect of L{parse_version}.
1491 @see: L{Implementation.get_version}
1492 @rtype: str
1493 @since: 0.24"""
1494 version = version[:]
1495 l = len(version)
1496 for x in range(0, l, 2):
1497 version[x] = '.'.join(map(str, version[x]))
1498 for x in range(1, l, 2):
1499 version[x] = '-' + _version_value_to_mod[version[x]]
1500 if version[-1] == '-': del version[-1]
1501 return ''.join(version)