<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import logging <EOL> import os <EOL> from contextlib import contextmanager <EOL> from pants . cache . artifact import TarballArtifact <EOL> from pants . cache . artifact_cache import ArtifactCache , UnreadableArtifact <EOL> from pants . util . contextutil import temporary_file <EOL> from pants . util . dirutil import ( safe_delete , safe_mkdir , safe_mkdir_for , <EOL> safe_rm_oldest_items_in_dir , safe_rmtree ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> class BaseLocalArtifactCache ( ArtifactCache ) : <EOL> def __init__ ( self , artifact_root , compression ) : <EOL> """<STR_LIT>""" <EOL> super ( BaseLocalArtifactCache , self ) . __init__ ( artifact_root ) <EOL> self . _compression = compression <EOL> self . _cache_root = None <EOL> def _artifact ( self , path ) : <EOL> return TarballArtifact ( self . artifact_root , path , self . _compression ) <EOL> @ contextmanager <EOL> def _tmpfile ( self , cache_key , use ) : <EOL> """<STR_LIT>""" <EOL> with temporary_file ( suffix = cache_key . id + use , root_dir = self . _cache_root ) as tmpfile : <EOL> yield tmpfile <EOL> @ contextmanager <EOL> def insert_paths ( self , cache_key , paths ) : <EOL> """<STR_LIT>""" <EOL> with self . _tmpfile ( cache_key , '<STR_LIT>' ) as tmp : <EOL> self . _artifact ( tmp . name ) . collect ( paths ) <EOL> yield self . _store_tarball ( cache_key , tmp . name ) <EOL> def store_and_use_artifact ( self , cache_key , src , results_dir = None ) : <EOL> """<STR_LIT>""" <EOL> with self . _tmpfile ( cache_key , '<STR_LIT>' ) as tmp : <EOL> for chunk in src : <EOL> tmp . write ( chunk ) <EOL> tmp . close ( ) <EOL> tarball = self . _store_tarball ( cache_key , tmp . name ) <EOL> artifact = self . _artifact ( tarball ) <EOL> if results_dir is not None : <EOL> safe_rmtree ( results_dir ) <EOL> artifact . extract ( ) <EOL> return True <EOL> def _store_tarball ( self , cache_key , src ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class LocalArtifactCache ( BaseLocalArtifactCache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , artifact_root , cache_root , compression , max_entries_per_target = None ) : <EOL> """<STR_LIT>""" <EOL> super ( LocalArtifactCache , self ) . __init__ ( artifact_root , compression ) <EOL> self . _cache_root = os . path . realpath ( os . path . expanduser ( cache_root ) ) <EOL> self . _max_entries_per_target = max_entries_per_target <EOL> safe_mkdir ( self . _cache_root ) <EOL> def prune ( self , root ) : <EOL> """<STR_LIT>""" <EOL> max_entries_per_target = self . _max_entries_per_target <EOL> if os . path . isdir ( root ) and max_entries_per_target : <EOL> safe_rm_oldest_items_in_dir ( root , max_entries_per_target ) <EOL> def has ( self , cache_key ) : <EOL> return self . _artifact_for ( cache_key ) . exists ( ) <EOL> def _artifact_for ( self , cache_key ) : <EOL> return self . _artifact ( self . _cache_file_for_key ( cache_key ) ) <EOL> def use_cached_files ( self , cache_key , results_dir = None ) : <EOL> tarfile = self . _cache_file_for_key ( cache_key ) <EOL> try : <EOL> artifact = self . _artifact_for ( cache_key ) <EOL> if artifact . exists ( ) : <EOL> if results_dir is not None : <EOL> safe_rmtree ( results_dir ) <EOL> artifact . extract ( ) <EOL> return True <EOL> except Exception as e : <EOL> logger . warn ( '<STR_LIT>' . format ( tarfile , e ) ) <EOL> safe_delete ( tarfile ) <EOL> return UnreadableArtifact ( cache_key , e ) <EOL> return False <EOL> def try_insert ( self , cache_key , paths ) : <EOL> with self . insert_paths ( cache_key , paths ) : <EOL> pass <EOL> def delete ( self , cache_key ) : <EOL> safe_delete ( self . _cache_file_for_key ( cache_key ) ) <EOL> def _store_tarball ( self , cache_key , src ) : <EOL> dest = self . _cache_file_for_key ( cache_key ) <EOL> safe_mkdir_for ( dest ) <EOL> os . rename ( src , dest ) <EOL> self . prune ( os . path . dirname ( dest ) ) <EOL> return dest <EOL> def _cache_file_for_key ( self , cache_key ) : <EOL> return os . path . join ( self . _cache_root , cache_key . id , cache_key . hash ) + '<STR_LIT>' <EOL> class TempLocalArtifactCache ( BaseLocalArtifactCache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , artifact_root , compression ) : <EOL> """<STR_LIT>""" <EOL> super ( TempLocalArtifactCache , self ) . __init__ ( artifact_root , compression = compression ) <EOL> def _store_tarball ( self , cache_key , src ) : <EOL> return src <EOL> def has ( self , cache_key ) : <EOL> return False <EOL> def use_cached_files ( self , cache_key , results_dir = None ) : <EOL> return False <EOL> def delete ( self , cache_key ) : <EOL> pass </s>
