1""" 2Code for managing the implementation cache. 3""" 4 5# Copyright (C) 2009, Thomas Leonard 6# See the README file for details, or visit http://0install.net. 7 8from__future__importprint_function 9 10fromzeroinstallimport_,logger 11importos 12 13fromzeroinstall.supportimportbasedir 14fromzeroinstallimportSafeException,support 15
54"""Break apart an algorithm/digest into in a tuple. 55 Old algorithms use '=' as the separator, while newer ones use '_'. 56 @param src: the combined string 57 @type src: str 58 @return: the parsed values 59 @rtype: (str, str) 60 @raise BadDigest: if it can't be parsed 61 @since: 1.10""" 62_validate_pair(src) 63ifsrc.startswith('sha1=')orsrc.startswith('sha1new=')orsrc.startswith('sha256='): 64returnsrc.split('=',1) 65result=src.split('_',1) 66iflen(result)!=2: 67if'='insrc: 68raiseBadDigest("Use '_' not '=' for new algorithms, in {src}".format(src=src)) 69raiseBadDigest("Can't parse digest {src}".format(src=src)) 70returnresult
73"""The opposite of L{parse_algorithm_digest_pair}. 74 The result is suitable for use as a directory name (does not contain '/' characters). 75 @type alg: str 76 @type digest: str 77 @rtype: str 78 @raise BadDigest: if the result is invalid 79 @since: 1.10""" 80ifalgin('sha1','sha1new','sha256'): 81result=alg+'='+digest 82else: 83result=alg+'_'+digest 84_validate_pair(result) 85returnresult
112"""Create a temporary directory in the directory where we would store an implementation113 with the given digest. This is used to setup a new implementation before being renamed if114 it turns out OK.115 @type required_digest: str116 @rtype: str117 @raise NonwritableStore: if we can't create it"""118try:119ifnotos.path.isdir(self.dir):120os.makedirs(self.dir)121fromtempfileimportmkdtemp122tmp=mkdtemp(dir=self.dir,prefix='tmp-')123os.chmod(tmp,0o755)# r-x for all; needed by 0store-helper124returntmp125exceptOSErrorasex:126raiseNonwritableStore(str(ex))
159"""Copy the contents of path to the cache.160 @param required_digest: the expected digest161 @type required_digest: str162 @param path: the root of the tree to copy163 @type path: str164 @param try_helper: attempt to use privileged helper before user cache (since 0.26)165 @type try_helper: bool166 @type dry_run: bool167 @raise BadDigest: if the contents don't match the given digest."""168ifself.lookup(required_digest):169logger.info(_("Not adding %s as it already exists!"),required_digest)170return171172tmp=self.get_tmp_dir_for(required_digest)173try:174_copytree2(path,tmp)175self.check_manifest_and_rename(required_digest,tmp,try_helper=try_helper,dry_run=dry_run)176except:177logger.warning(_("Error importing directory."))178logger.warning(_("Deleting %s"),tmp)179support.ro_rmtree(tmp)180raise
183"""Use 0store-secure-add to copy 'path' to the system store.184 @param required_digest: the digest for path185 @type required_digest: str186 @param path: root of implementation directory structure187 @type path: str188 @return: True iff the directory was copied into the system cache successfully"""189ifrequired_digest.startswith('sha1='):190returnFalse# Old digest alg not supported191ifos.environ.get('ZEROINSTALL_PORTABLE_BASE'):192returnFalse# Can't use helper with portable mode193helper=support.find_in_path('0store-secure-add-helper')194ifnothelper:195logger.info(_("'0store-secure-add-helper' command not found. Not adding to system cache."))196returnFalse197ifdry_run:198print(_("[dry-run] would use {helper} to store {required_digest} in system store").format(199helper=helper,200required_digest=required_digest))201self.dry_run_names.add(required_digest)202returnTrue203importsubprocess204env=os.environ.copy()205env['ENV_NOT_CLEARED']='Unclean'# (warn about insecure configurations)206env['HOME']='Unclean'# (warn about insecure configurations)207dev_null=os.open(os.devnull,os.O_RDONLY)208try:209logger.info(_("Trying to add to system cache using %s"),helper)210child=subprocess.Popen([helper,required_digest],211stdin=dev_null,212cwd=path,213env=env)214exit_code=child.wait()215finally:216os.close(dev_null)217218ifexit_code:219logger.warning(_("0store-secure-add-helper failed."))220returnFalse221222logger.info(_("Added succcessfully."))223returnTrue
226"""Check that tmp[/extract] has the required_digest.227 On success, rename the checked directory to the digest, and228 make the whole tree read-only.229 @type required_digest: str230 @type tmp: str231 @type extract: str | None232 @param try_helper: attempt to use privileged helper to import to system cache first (since 0.26)233 @type try_helper: bool234 @param dry_run: just print what we would do to stdout (and delete tmp)235 @type dry_run: bool236 @raise BadDigest: if the input directory doesn't match the given digest"""237ifextract:238extracted=os.path.join(tmp,extract)239ifnotos.path.isdir(extracted):240raiseException(_('Directory %s not found in archive')%extract)241else:242extracted=tmp243244from.importmanifest245246manifest.fixup_permissions(extracted)247248alg,required_value=manifest.splitID(required_digest)249actual_digest=alg.getID(manifest.add_manifest_file(extracted,alg))250ifactual_digest!=required_digest:251raiseBadDigest(_('Incorrect manifest -- archive is corrupted.\n'252'Required digest: %(required_digest)s\n'253'Actual digest: %(actual_digest)s\n')%254{'required_digest':required_digest,'actual_digest':actual_digest})255256iftry_helper:257ifself._add_with_helper(required_digest,extracted,dry_run=dry_run):258support.ro_rmtree(tmp)259return260logger.info(_("Can't add to system store. Trying user store instead."))261262logger.info(_("Caching new implementation (digest %s) in %s"),required_digest,self.dir)263264final_name=os.path.join(self.dir,required_digest)265ifos.path.isdir(final_name):266logger.warning(_("Item %s already stored.")%final_name)# not really an error267return268269ifdry_run:270print(_("[dry-run] would store implementation as {path}").format(path=final_name))271self.dry_run_names.add(required_digest)272support.ro_rmtree(tmp)273return274else:275# If we just want a subdirectory then the rename will change276# extracted/.. and so we'll need write permission on 'extracted'277278os.chmod(extracted,0o755)279os.rename(extracted,final_name)280os.chmod(final_name,0o555)281282ifextract:283os.rmdir(tmp)
289"""A list of L{Store}s. All stores are searched when looking for an implementation.290 When storing, we use the first of the system caches (if writable), or the user's291 cache otherwise."""292__slots__=['stores']293
295# Always add the user cache to have a reliable fallback location for storage296user_store=os.path.join(basedir.xdg_cache_home,'0install.net','implementations')297self.stores=[Store(user_store)]298299# Add custom cache locations300dirs=[]301forimpl_dirsinbasedir.load_config_paths('0install.net','injector','implementation-dirs'):302withopen(impl_dirs,'rt')asstream:303dirs.extend(stream.readlines())304fordirectoryindirs:305directory=directory.strip()306ifdirectoryandnotdirectory.startswith('#'):307logger.debug(_("Added system store '%s'"),directory)308self.stores.append(Store(directory))309310# Add the system cache when not in portable mode311ifnotos.environ.get('ZEROINSTALL_PORTABLE_BASE'):312ifos.name=="nt":313fromwin32com.shellimportshell,shellcon314commonAppData=shell.SHGetFolderPath(0,shellcon.CSIDL_COMMON_APPDATA,0,0)315systemCachePath=os.path.join(commonAppData,"0install.net","implementations")316# Only use shared cache location on Windows if it was explicitly created317ifos.path.isdir(systemCachePath):318self.stores.append(Store(systemCachePath))319else:320self.stores.append(Store('/var/cache/0install.net/implementations'))
329"""Search for digest in all stores.330 @type digests: [str]331 @rtype: str332 @raises NotStored: if not found"""333path=self.lookup_maybe(digests)334ifpath:335returnpath336raiseNotStored(_("Item with digests '%(digests)s' not found in stores. Searched:\n- %(stores)s")%337{'digests':digests,'stores':'\n- '.join([s.dirforsinself.stores])})
387"""The first system store is the one we try writing to first.388 @rtype: L{Store}389 @since: 0.30"""390try:391returnself.stores[1]392exceptIndexError:393raiseSafeException(_("No system stores have been configured"))