19"""A single selected implementation in a L{Selections} set. 20 @ivar dependencies: list of dependencies 21 @type dependencies: [L{model.Dependency}] 22 @ivar attrs: XML attributes map (name is in the format "{namespace} {localName}") 23 @type attrs: {str: str} 24 @ivar version: the implementation's version number 25 @type version: str""" 26 27interface=property(lambdaself:self.attrs['interface']) 28id=property(lambdaself:self.attrs['id']) 29version=property(lambdaself:self.attrs['version']) 30feed=property(lambdaself:self.attrs.get('from-feed',self.interface)) 31main=property(lambdaself:self.attrs.get('main',None)) 32 33@property
47"""Is this implementation available locally? 48 (a local implementation or a cached ZeroInstallImplementation) 49 @rtype: bool 50 @since: 0.53""" 51path=self.local_path 52ifpathisnotNone: 53returnos.path.exists(path) 54path=stores.lookup_maybe(self.digests) 55returnpathisnotNone
58"""Return the root directory of this implementation. 59 For local implementations, this is L{local_path}. 60 For cached implementations, this is the directory in the cache. 61 @param stores: stores to search 62 @type stores: L{zerostore.Stores} 63 @param missing_ok: return None for uncached implementations 64 @type missing_ok: bool 65 @return: the path of the directory 66 @rtype: str | None 67 @since: 1.8""" 68ifself.local_pathisnotNone: 69returnself.local_path 70ifnotself.digests: 71# (for now, we assume this is always an error, even for missing_ok) 72raisemodel.SafeException("No digests for {feed} {version}".format(feed=self.feed,version=self.version)) 73ifmissing_ok: 74returnstores.lookup_maybe(self.digests) 75else: 76returnstores.lookup_any(self.digests)
115"""A Selection created by reading an XML selections document.116 @ivar digests: a list of manifest digests117 @type digests: [str]118 """119__slots__=['bindings','dependencies','attrs','digests','commands']120
152"""153 A selected set of components which will make up a complete program.154 @ivar interface: the interface of the program155 @type interface: str156 @ivar command: the command to run on 'interface'157 @type command: str158 @ivar selections: the selected implementations159 @type selections: {str: L{Selection}}160 """161__slots__=['interface','selections','command']162
164"""Constructor.165 @param source: a map of implementations, policy or selections document166 @type source: L{Element}"""167self.selections={}168self.command=None169170ifsourceisNone:171# (Solver will fill everything in)172pass173elifisinstance(source,Element):174self._init_from_qdom(source)175else:176raiseException(_("Source not a qdom.Element!"))
179"""Parse and load a selections document.180 @param root: a saved set of selections.181 @type root: L{Element}"""182self.interface=root.getAttribute('interface')183self.command=root.getAttribute('command')184ifself.interfaceisNone:185raisemodel.SafeException(_("Not a selections document (no 'interface' attribute on root)"))186old_commands=[]187188forselectioninroot.childNodes:189ifselection.uri!=XMLNS_IFACE:190continue191ifselection.name!='selection':192ifselection.name=='command':193old_commands.append(Command(selection,None))194continue195196requires=[]197bindings=[]198digests=[]199commands={}200foreleminselection.childNodes:201ifelem.uri!=XMLNS_IFACE:202continue203ifelem.nameinbinding_names:204bindings.append(process_binding(elem))205elifelem.name=='requires':206dep=process_depends(elem,None)207requires.append(dep)208elifelem.name=='manifest-digest':209foraname,avalueinelem.attrs.items():210digests.append(zerostore.format_algorithm_digest_pair(aname,avalue))211elifelem.name=='command':212name=elem.getAttribute('name')213assertname,"Missing name attribute on <command>"214commands[name]=Command(elem,None)215216# For backwards compatibility, allow getting the digest from the ID217sel_id=selection.attrs['id']218local_path=selection.attrs.get("local-path",None)219if(notdigestsandnotlocal_path)and'='insel_id:220alg=sel_id.split('=',1)[0]221ifalgin('sha1','sha1new','sha256'):222digests.append(sel_id)223224iface_uri=selection.attrs['interface']225226s=XMLSelection(requires,bindings,selection.attrs,digests,commands)227self.selections[iface_uri]=s228229ifself.commandisNone:230# Old style selections document231ifold_commands:232# 0launch 0.52 to 1.1233self.command='run'234iface=self.interface235236forcommandinold_commands:237command.qdom.attrs['name']='run'238self.selections[iface].commands['run']=command239runner=command.get_runner()240ifrunner:241iface=runner.interface242else:243iface=None244else:245# 0launch < 0.51246root_sel=self.selections[self.interface]247main=root_sel.attrs.get('main',None)248ifmainisnotNone:249root_sel.commands['run']=Command(Element(XMLNS_IFACE,'command',{'path':main,'name':'run'}),None)250self.command='run'251252elifself.command=='':253# New style, but no command requested254self.command=None255assertnotold_commands,"<command> list in new-style selections document!"
258"""Create a DOM document for the selected implementations.259 The document gives the URI of the root, plus each selected implementation.260 For each selected implementation, we record the ID, the version, the URI and261 (if different) the feed URL. We also record all the bindings needed.262 @return: a new DOM Document"""263fromxml.domimportminidom,XMLNS_NAMESPACE264265assertself.interface266267impl=minidom.getDOMImplementation()268269doc=impl.createDocument(XMLNS_IFACE,"selections",None)270271root=doc.documentElement272root.setAttributeNS(XMLNS_NAMESPACE,'xmlns',XMLNS_IFACE)273274root.setAttributeNS(None,'interface',self.interface)275276root.setAttributeNS(None,'command',self.commandor"")277278prefixes=Prefixes(XMLNS_IFACE)279280foriface,selectioninsorted(self.selections.items()):281selection_elem=doc.createElementNS(XMLNS_IFACE,'selection')282selection_elem.setAttributeNS(None,'interface',selection.interface)283root.appendChild(selection_elem)284285forname,valueinselection.attrs.items():286if' 'inname:287ns,localName=name.split(' ',1)288prefixes.setAttributeNS(selection_elem,ns,localName,value)289elifname=='stability':290pass291elifname=='from-feed':292# Don't bother writing from-feed attr if it's the same as the interface293ifvalue!=selection.attrs['interface']:294selection_elem.setAttributeNS(None,name,value)295elifnamenotin('main','self-test'):# (replaced by <command>)296selection_elem.setAttributeNS(None,name,value)297298ifselection.digests:299manifest_digest=doc.createElementNS(XMLNS_IFACE,'manifest-digest')300fordigestinselection.digests:301aname,avalue=zerostore.parse_algorithm_digest_pair(digest)302assert':'notinaname303manifest_digest.setAttribute(aname,avalue)304selection_elem.appendChild(manifest_digest)305306forbinselection.bindings:307selection_elem.appendChild(b._toxml(doc,prefixes))308309fordepinselection.dependencies:310ifnotisinstance(dep,model.InterfaceDependency):continue311312dep_elem=doc.createElementNS(XMLNS_IFACE,'requires')313dep_elem.setAttributeNS(None,'interface',dep.interface)314selection_elem.appendChild(dep_elem)315316formindep.metadata:317parts=m.split(' ',1)318iflen(parts)==1:319ns=None320localName=parts[0]321dep_elem.setAttributeNS(None,localName,dep.metadata[m])322else:323ns,localName=parts324prefixes.setAttributeNS(dep_elem,ns,localName,dep.metadata[m])325326forbindep.bindings:327dep_elem.appendChild(b._toxml(doc,prefixes))328329forcommandinselection.get_commands().values():330selection_elem.appendChild(command._toxml(doc,prefixes))331332forns,prefixinprefixes.prefixes.items():333root.setAttributeNS(XMLNS_NAMESPACE,'xmlns:'+prefix,ns)334335returndoc
341"""Find those selections which are not present.342 Local implementations are available if their directory exists.343 Other 0install implementations are available if they are in the cache.344 Package implementations are available if the Distribution says so.345 @param include_packages: whether to include <package-implementation>s346 @type include_packages: bool347 @rtype: [Selection]348 @since: 1.16"""349iface_cache=config.iface_cache350stores=config.stores351352# Check that every required selection is cached353defneeds_download(sel):354ifsel.id.startswith('package:'):355ifnotinclude_packages:returnFalse356feed=iface_cache.get_feed(sel.feed)357ifnotfeed:returnFalse358impl=feed.implementations.get(sel.id,None)359returnimplisNoneornotimpl.installed360elifsel.local_path:361returnFalse362else:363returnsel.get_path(stores,missing_ok=True)isNone
368"""Check all selected implementations are available.369 Download any that are not present. Since native distribution packages are usually370 only available in a single version, which is unlikely to be the one in the371 selections document, we ignore them by default.372 Note: package implementations (distribution packages) are ignored.373 @param config: used to get iface_cache, stores and fetcher374 @param include_packages: also try to install native packages (since 1.5)375 @type include_packages: bool376 @rtype: L{zeroinstall.support.tasks.Blocker} | None"""377if_old:378config=get_deprecated_singleton_config()379380iface_cache=config.iface_cache381stores=config.stores382383needed_downloads=self.get_unavailable_selections(config,include_packages)384ifnotneeded_downloads:385return386387ifconfig.network_use==model.network_offline:388fromzeroinstallimportNeedDownload389raiseNeedDownload(', '.join([str(x)forxinneeded_downloads]))390391@tasks.async392defdownload():393# We're missing some. For each one, get the feed it came from394# and find the corresponding <implementation> in that. This will395# tell us where to get it from.396# Note: we look for an implementation with the same ID. Maybe we397# should check it has the same digest(s) too?398needed_impls=[]399forselinneeded_downloads:400feed_url=sel.attrs.get('from-feed',None)orsel.attrs['interface']401feed=iface_cache.get_feed(feed_url)402iffeedisNoneorsel.idnotinfeed.implementations:403fetch_feed=config.fetcher.download_and_import_feed(feed_url,iface_cache)404yieldfetch_feed405tasks.check(fetch_feed)406407feed=iface_cache.get_feed(feed_url)408assertfeed,"Failed to get feed for %s"%feed_url409impl=feed.implementations[sel.id]410needed_impls.append(impl)411412fetch_impls=config.fetcher.download_impls(needed_impls,stores)413yieldfetch_impls414tasks.check(fetch_impls)
415returndownload()416417# These (deprecated) methods are to make a Selections object look like the old Policy.implementation map...418