1""" 2Integration with native distribution package managers. 3@since: 0.28 4""" 5 6# Copyright (C) 2009, Thomas Leonard 7# See the README file for details, or visit http://0install.net. 8 9fromzeroinstallimport_,logger,gobject 10importos,platform,re,subprocess,sys 11fromzeroinstall.injectorimportnamespaces,model,arch,qdom 12fromzeroinstall.supportimportbasedir,portable_rename,intern 13 14_dotted_ints='[0-9]+(?:\.[0-9]+)*' 15 16# This matches a version number that would be a valid Zero Install version without modification 17_zeroinstall_regexp='(?:%s)(?:-(?:pre|rc|post|)(?:%s))*'%(_dotted_ints,_dotted_ints) 18 19# This matches the interesting bits of distribution version numbers 20# (first matching group is for Java-style 6b17 or 7u9 syntax, or "major") 21_version_regexp='(?:[a-z])?({ints}\.?[bu])?({zero})(-r{ints})?'.format(zero=_zeroinstall_regexp,ints=_dotted_ints) 22 23_PYTHON_URI='http://repo.roscidus.com/python/python'
24 25# We try to do updates atomically without locking, but we don't worry too much about 26# duplicate entries or being a little out of sync with the on-disk copy. 27-classCache(object):
29"""Maintain a cache file (e.g. ~/.cache/0install.net/injector/$name). 30 If the size or mtime of $source has changed, or the cache 31 format version if different, reset the cache first. 32 @type cache_leaf: str 33 @type source: str 34 @type format: int""" 35self.cache_leaf=cache_leaf 36self.source=source 37self.format=format 38self.cache_dir=basedir.save_cache_path(namespaces.config_site, 39namespaces.config_prog) 40self.cached_for={}# Attributes of source when cache was created 41try: 42self._load_cache() 43exceptExceptionasex: 44logger.info(_("Failed to load cache (%s). Flushing..."),ex) 45self.flush()
86info=os.stat(self.source) 87ifself.cached_for['mtime']!=int(info.st_mtime): 88raiseException("Modification time of %s has changed"%self.source) 89ifself.cached_for['size']!=info.st_size: 90raiseException("Size of %s has changed"%self.source) 91ifself.cached_for.get('format',None)!=self.format: 92raiseException("Format of cache has changed")
118"""Try to turn a distribution version string into one readable by Zero Install. 119 We do this by stripping off anything we can't parse. 120 @type version: str 121 @return: the part we understood, or None if we couldn't parse anything 122 @rtype: str""" 123if':'inversion: 124version=version.split(':')[1]# Skip 'epoch' 125version=version.replace('_','-') 126if'~'inversion: 127version,suffix=version.split('~',1) 128ifsuffix.startswith('pre'): 129suffix=suffix[3:] 130suffix='-pre'+(try_cleanup_distro_version(suffix)or'') 131else: 132suffix='' 133match=re.match(_version_regexp,version) 134ifmatch: 135major,version,revision=match.groups() 136ifmajorisnotNone: 137version=major[:-1].rstrip('.')+'.'+version 138ifrevisionisnotNone: 139version='%s-%s'%(version,revision[2:]) 140returnversion+suffix 141returnNone
144"""Represents a distribution with which we can integrate. 145 Sub-classes should specialise this to integrate with the package managers of 146 particular distributions. This base class ignores the native package manager. 147 @since: 0.28 148 @ivar name: the default value for Implementation.distro_name for our implementations 149 @type name: str 150 @ivar system_paths: list of paths to search for binaries (we MUST NOT find 0install launchers, so only include directories where system packages install binaries - e.g. /usr/bin but not /usr/local/bin) 151 @type system_paths: [str] 152 """ 153 154name="fallback" 155 156_packagekit=None 157 158system_paths=['/usr/bin','/bin','/usr/sbin','/sbin'] 159
161"""Get information about the given package. 162 Add zero or more implementations using the factory (typically at most two 163 will be added; the currently installed version and the latest available). 164 @param package: package name (e.g. "gimp") 165 @type package: str 166 @param factory: function for creating new DistributionImplementation objects from IDs 167 @type factory: str -> L{model.DistributionImplementation}""" 168return
171"""Indicate how closely the host distribution matches this one. 172 The <package-implementation> with the highest score is passed 173 to L{Distribution.get_package_info}. If several elements get 174 the same score, get_package_info is called for all of them. 175 @param distribution: a distribution name 176 @type distribution: str 177 @return: an integer, or -1 if there is no match at all 178 @rtype: int""" 179return0
182"""Generate a feed containing information about distribution packages. 183 This should immediately return a feed containing an implementation for the 184 package if it's already installed. Information about versions that could be 185 installed using the distribution's package manager can be added asynchronously 186 later (see L{fetch_candidates}). 187 @param master_feed: feed containing the <package-implementation> elements 188 @type master_feed: L{model.ZeroInstallFeed} 189 @rtype: L{model.ZeroInstallFeed}""" 190 191feed=model.ZeroInstallFeed(None) 192feed.url='distribution:'+master_feed.url 193 194foritem,item_attrs,dependsinmaster_feed.get_package_impls(self): 195package=item_attrs.get('package',None) 196ifpackageisNone: 197raisemodel.InvalidInterface(_("Missing 'package' attribute on %s")%item) 198 199new_impls=[] 200 201deffactory(id,only_if_missing=False,installed=True): 202assertid.startswith('package:') 203ifidinfeed.implementations: 204ifonly_if_missing: 205returnNone 206logger.warning(_("Duplicate ID '%s' for DistributionImplementation"),id) 207impl=model.DistributionImplementation(feed,id,self,item) 208feed.implementations[id]=impl 209new_impls.append(impl) 210 211impl.installed=installed 212impl.metadata=item_attrs 213impl.requires=depends 214 215if'run'notinimpl.commands: 216item_main=item_attrs.get('main',None) 217ifitem_main: 218impl.main=item_main 219impl.upstream_stability=model.packaged 220 221returnimpl
222 223self.get_package_info(package,factory) 224 225forimplinnew_impls: 226self.fixup(package,impl) 227ifimpl.installed: 228self.installed_fixup(impl) 229 230ifmaster_feed.url==_PYTHON_URIandos.name!="nt": 231# Hack: we can support Python on platforms with unsupported package managers 232# by adding the implementation of Python running us now to the list. 233python_version='.'.join([str(v)forvinsys.version_infoifisinstance(v,int)]) 234impl_id='package:host:python:'+python_version 235assertimpl_idnotinfeed.implementations 236impl=model.DistributionImplementation(feed,impl_id,self,distro_name='host') 237impl.installed=True 238impl.version=model.parse_version(python_version) 239impl.main=sys.executableor'/usr/bin/python' 240impl.upstream_stability=model.packaged 241impl.machine=host_machine# (hopefully) 242feed.implementations[impl_id]=impl 243elifmaster_feed.url=='http://repo.roscidus.com/python/python-gobject'andos.name!="nt"andgobject: 244# Likewise, we know that there is a native python-gobject available for our Python 245impl_id='package:host:python-gobject:'+'.'.join(str(x)forxingobject.pygobject_version) 246assertimpl_idnotinfeed.implementations 247impl=model.DistributionImplementation(feed,impl_id,self,distro_name='host') 248impl.installed=True 249impl.version=[list(gobject.pygobject_version)] 250impl.upstream_stability=model.packaged 251impl.machine=host_machine# (hopefully) 252 253# Requires our version of Python too 254restriction_element=qdom.Element(namespaces.XMLNS_IFACE,'restricts',{'interface':_PYTHON_URI,'distribution':'host'}) 255impl.requires.append(model.process_depends(restriction_element,None)) 256 257feed.implementations[impl_id]=impl 258 259returnfeed
262"""Collect information about versions we could install using 263 the distribution's package manager. On success, the distribution 264 feed in iface_cache is updated. 265 @return: a L{tasks.Blocker} if the task is in progress, or None if not""" 266ifself.packagekit.available: 267package_names=[item.getAttribute("package")foritem,item_attrs,dependsinmaster_feed.get_package_impls(self)] 268returnself.packagekit.fetch_candidates(package_names)
280"""Some packages require special handling (e.g. Java). This is called for each 281 package that was added by L{get_package_info} after it returns. The default 282 method does nothing. 283 @param package: the name of the package 284 @param impl: the constructed implementation""" 285pass
288"""Called when an installed package is added (after L{fixup}), or when installation 289 completes. This is useful to fix up the main value. 290 The default implementation checks that main exists, and searches L{Distribution.system_paths} for 291 it if not. 292 @type impl: L{DistributionImplementation} 293 @since: 1.11""" 294 295run=impl.commands.get('run',None) 296ifnotrun:return 297 298path=run.path 299 300ifnotpath:return 301 302ifos.path.isabs(path)andos.path.exists(path): 303return 304 305basename=os.path.basename(path) 306ifos.name=="nt"andnotbasename.endswith('.exe'): 307basename+='.exe' 308 309fordinself.system_paths: 310path=os.path.join(d,basename) 311ifos.path.isfile(path): 312logger.info("Found %s by searching system paths",path) 313run.qdom.attrs["path"]=path 314return 315else: 316logger.info("Binary '%s' not found in any system path (checked %s)",basename,self.system_paths)
329def_is_64bit_windows(): 330p=sys.platform 331fromwin32processimportIsWow64Process 332ifp=='win64'or(p=='win32'andIsWow64Process()):returnTrue 333elifp=='win32':returnFalse 334else:raiseException(_("WindowsDistribution may only be used on the Windows platform"))
363 364deffind_java(part,win_version,zero_version): 365reg_path=r"SOFTWARE\JavaSoft\{part}\{win_version}".format(part=part,win_version=win_version) 366(java32_home,java64_home)=_read_hklm_reg(reg_path,"JavaHome") 367 368for(home,arch)in[(java32_home,'i486'),(java64_home,'x86_64')]: 369ifos.path.isfile(home+r"\bin\java.exe"): 370impl=factory('package:windows:%s:%s:%s'%(package,zero_version,arch)) 371impl.machine=arch 372impl.version=model.parse_version(zero_version) 373impl.upstream_stability=model.packaged 374impl.main=home+r"\bin\java.exe" 375 376deffind_netfx(win_version,zero_version): 377reg_path=r"SOFTWARE\Microsoft\NET Framework Setup\NDP\{win_version}".format(win_version=win_version) 378(netfx32_install,netfx64_install)=_read_hklm_reg(reg_path,"Install") 379 380for(install,arch)in[(netfx32_install,'i486'),(netfx64_install,'x86_64')]: 381impl=factory('package:windows:%s:%s:%s'%(package,zero_version,arch)) 382impl.installed=(install==1) 383impl.machine=arch 384impl.version=model.parse_version(zero_version) 385impl.upstream_stability=model.packaged 386impl.main=""# .NET executables do not need a runner on Windows but they need one elsewhere 387 388deffind_netfx_release(win_version,release_version,zero_version): 389reg_path=r"SOFTWARE\Microsoft\NET Framework Setup\NDP\{win_version}".format(win_version=win_version) 390(netfx32_install,netfx64_install)=_read_hklm_reg(reg_path,"Install") 391(netfx32_release,netfx64_release)=_read_hklm_reg(reg_path,"Release") 392 393for(install,release,arch)in[(netfx32_install,netfx32_release,'i486'),(netfx64_install,netfx64_release,'x86_64')]: 394impl=factory('package:windows:%s:%s:%s'%(package,zero_version,arch)) 395impl.installed=(install==1andrelease!=''andrelease>=release_version) 396impl.machine=arch 397impl.version=model.parse_version(zero_version) 398impl.upstream_stability=model.packaged 399impl.main=""# .NET executables do not need a runner on Windows but they need one elsewhere 400 401ifpackage=='openjdk-6-jre': 402find_java("Java Runtime Environment","1.6",'6') 403elifpackage=='openjdk-6-jdk': 404find_java("Java Development Kit","1.6",'6') 405elifpackage=='openjdk-7-jre': 406find_java("Java Runtime Environment","1.7",'7') 407elifpackage=='openjdk-7-jdk': 408find_java("Java Development Kit","1.7",'7') 409elifpackage=='netfx': 410find_netfx("v2.0.50727",'2.0') 411find_netfx("v3.0",'3.0') 412find_netfx("v3.5",'3.5') 413find_netfx("v4\\Full",'4.0') 414find_netfx_release("v4\\Full",378389,'4.5') 415find_netfx("v5",'5.0') 416elifpackage=='netfx-client': 417find_netfx("v4\\Client",'4.0') 418find_netfx_release("v4\\Client",378389,'4.5') 419
480"""For distributions where querying the package database is slow (e.g. requires running 481 an external command), we cache the results. 482 @since: 0.39 483 @deprecated: use Cache instead 484 """ 485
487"""@param db_status_file: update the cache when the timestamp of this file changes 488 @type db_status_file: str""" 489self._status_details=os.stat(db_status_file) 490 491self.versions={} 492self.cache_dir=basedir.save_cache_path(namespaces.config_site, 493namespaces.config_prog) 494 495try: 496self._load_cache() 497exceptExceptionasex: 498logger.info(_("Failed to load distribution database cache (%s). Regenerating..."),ex) 499try: 500self.generate_cache() 501self._load_cache() 502exceptExceptionasex: 503logger.warning(_("Failed to regenerate distribution database cache: %s"),ex)
506"""Load {cache_leaf} cache file into self.versions if it is available and up-to-date. 507 Throws an exception if the cache should be (re)created.""" 508withopen(os.path.join(self.cache_dir,self.cache_leaf),'rt')asstream: 509cache_version=None 510forlineinstream: 511ifline=='\n': 512break 513name,value=line.split(': ') 514ifname=='mtime'andint(value)!=int(self._status_details.st_mtime): 515raiseException(_("Modification time of package database file has changed")) 516ifname=='size'andint(value)!=self._status_details.st_size: 517raiseException(_("Size of package database file has changed")) 518ifname=='version': 519cache_version=int(value) 520else: 521raiseException(_('Invalid cache format (bad header)')) 522 523ifcache_versionisNone: 524raiseException(_('Old cache format')) 525 526versions=self.versions 527forlineinstream: 528package,version,zi_arch=line[:-1].split('\t') 529versionarch=(version,intern(zi_arch)) 530ifpackagenotinversions: 531versions[package]=[versionarch] 532else: 533versions[package].append(versionarch)
536#cache.sort() # Might be useful later; currently we don't care 537"""@type cache: [str]""" 538importtempfile 539fd,tmpname=tempfile.mkstemp(prefix='zeroinstall-cache-tmp', 540dir=self.cache_dir) 541try: 542stream=os.fdopen(fd,'wt') 543stream.write('version: 2\n') 544stream.write('mtime: %d\n'%int(self._status_details.st_mtime)) 545stream.write('size: %d\n'%self._status_details.st_size) 546stream.write('\n') 547forlineincache: 548stream.write(line+'\n') 549stream.close() 550 551portable_rename(tmpname, 552os.path.join(self.cache_dir, 553self.cache_leaf)) 554except: 555os.unlink(tmpname) 556raise
557 558# Maps machine type names used in packages to their Zero Install versions 559# (updates to this might require changing the reverse Java mapping) 560_canonical_machine={ 561'all':'*', 562'any':'*', 563'noarch':'*', 564'(none)':'*', 565'x86_64':'x86_64', 566'amd64':'x86_64', 567'i386':'i386', 568'i486':'i486', 569'i586':'i586', 570'i686':'i686', 571'ppc64':'ppc64', 572'ppc':'ppc', 573} 574 575host_machine=arch.canonicalize_machine(platform.uname()[4])
622# Add any already-installed package... 623"""@type package: str""" 624installed_cached_info=self._get_dpkg_info(package) 625 626ifinstalled_cached_info!='-': 627installed_version,machine=installed_cached_info.split('\t') 628impl=factory('package:deb:%s:%s:%s'%(package,installed_version,machine)) 629impl.version=model.parse_version(installed_version) 630ifmachine!='*': 631impl.machine=machine 632else: 633installed_version=None 634 635# Add any uninstalled candidates (note: only one of these two methods will add anything) 636 637# From PackageKit... 638self.packagekit.get_candidates(package,factory,'package:deb') 639 640# From apt-cache... 641cached=self.apt_cache.get(package,None) 642ifcached: 643candidate_version=cached['version'] 644candidate_arch=cached['arch'] 645ifcandidate_versionandcandidate_version!=installed_version: 646impl=factory('package:deb:%s:%s:%s'%(package,candidate_version,candidate_arch),installed=False) 647impl.version=model.parse_version(candidate_version) 648ifcandidate_arch!='*': 649impl.machine=candidate_arch 650definstall(handler): 651raisemodel.SafeException(_("This program depends on '%s', which is a package that is available through your distribution. " 652"Please install it manually using your distribution's tools and try again. Or, install 'packagekit' and I can " 653"use that to install it.")%package)
1038# We support both sources of packages.1039# In theory, we should route 'Darwin' package names to DarwinDistribution, and1040# Mac Ports names to MacPortsDistribution. But since we only use Darwin for Java,1041# having one object handle both is OK.1042returnint(distro_namein('Darwin','MacPorts'))
1052cache=[]10531054zi_arch=canonical_machine(arch)1055forlineinos.popen("cygcheck -c -d"):1056ifline=="Cygwin Package Information\r\n":1057continue1058ifline=="\n":1059continue1060package,version=line.split()1061ifpackage=="Package"andversion=="Version":1062continue1063clean_version=try_cleanup_distro_version(version)1064ifclean_version:1065cache.append('%s\t%s\t%s'%(package,clean_version,zi_arch))1066else:1067logger.warning(_("Can't parse distribution version '%(version)s' for package '%(package)s'"),{'version':version,'package':package})10681069self._write_cache(cache)