<s> """<STR_LIT>""" <EOL> from os import path , listdir <EOL> import sys <EOL> import marshal <EOL> import tempfile <EOL> import cPickle as pickle <EOL> import fnmatch <EOL> try : <EOL> from hashlib import sha1 <EOL> except ImportError : <EOL> from sha import new as sha1 <EOL> from jinja2 . utils import open_if_exists <EOL> if sys . version_info > ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> from io import BytesIO <EOL> marshal_dump = marshal . dump <EOL> marshal_load = marshal . load <EOL> else : <EOL> from cStringIO import StringIO as BytesIO <EOL> def marshal_dump ( code , f ) : <EOL> if isinstance ( f , file ) : <EOL> marshal . dump ( code , f ) <EOL> else : <EOL> f . write ( marshal . dumps ( code ) ) <EOL> def marshal_load ( f ) : <EOL> if isinstance ( f , file ) : <EOL> return marshal . load ( f ) <EOL> return marshal . loads ( f . read ( ) ) <EOL> bc_version = <NUM_LIT:2> <EOL> bc_magic = '<STR_LIT>' . encode ( '<STR_LIT:ascii>' ) + pickle . dumps ( bc_version , <NUM_LIT:2> ) + pickle . dumps ( ( sys . version_info [ <NUM_LIT:0> ] << <NUM_LIT> ) | sys . version_info [ <NUM_LIT:1> ] ) <EOL> class Bucket ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , environment , key , checksum ) : <EOL> self . environment = environment <EOL> self . key = key <EOL> self . checksum = checksum <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . code = None <EOL> def load_bytecode ( self , f ) : <EOL> """<STR_LIT>""" <EOL> magic = f . read ( len ( bc_magic ) ) <EOL> if magic != bc_magic : <EOL> self . reset ( ) <EOL> return <EOL> checksum = pickle . load ( f ) <EOL> if self . checksum != checksum : <EOL> self . reset ( ) <EOL> return <EOL> self . code = marshal_load ( f ) <EOL> def write_bytecode ( self , f ) : <EOL> """<STR_LIT>""" <EOL> if self . code is None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> f . write ( bc_magic ) <EOL> pickle . dump ( self . checksum , f , <NUM_LIT:2> ) <EOL> marshal_dump ( self . code , f ) <EOL> def bytecode_from_string ( self , string ) : <EOL> """<STR_LIT>""" <EOL> self . load_bytecode ( BytesIO ( string ) ) <EOL> def bytecode_to_string ( self ) : <EOL> """<STR_LIT>""" <EOL> out = BytesIO ( ) <EOL> self . write_bytecode ( out ) <EOL> return out . getvalue ( ) <EOL> class BytecodeCache ( object ) : <EOL> """<STR_LIT>""" <EOL> def load_bytecode ( self , bucket ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def dump_bytecode ( self , bucket ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> def get_cache_key ( self , name , filename = None ) : <EOL> """<STR_LIT>""" <EOL> hash = sha1 ( name . encode ( '<STR_LIT:utf-8>' ) ) <EOL> if filename is not None : <EOL> filename = '<STR_LIT:|>' + filename <EOL> if isinstance ( filename , unicode ) : <EOL> filename = filename . encode ( '<STR_LIT:utf-8>' ) <EOL> hash . update ( filename ) <EOL> return hash . hexdigest ( ) <EOL> def get_source_checksum ( self , source ) : <EOL> """<STR_LIT>""" <EOL> return sha1 ( source . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> def get_bucket ( self , environment , name , filename , source ) : <EOL> """<STR_LIT>""" <EOL> key = self . get_cache_key ( name , filename ) <EOL> checksum = self . get_source_checksum ( source ) <EOL> bucket = Bucket ( environment , key , checksum ) <EOL> self . load_bytecode ( bucket ) <EOL> return bucket <EOL> def set_bucket ( self , bucket ) : <EOL> """<STR_LIT>""" <EOL> self . dump_bytecode ( bucket ) <EOL> class FileSystemBytecodeCache ( BytecodeCache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , directory = None , pattern = '<STR_LIT>' ) : <EOL> if directory is None : <EOL> directory = tempfile . gettempdir ( ) <EOL> self . directory = directory <EOL> self . pattern = pattern <EOL> def _get_cache_filename ( self , bucket ) : <EOL> return path . join ( self . directory , self . pattern % bucket . key ) <EOL> def load_bytecode ( self , bucket ) : <EOL> f = open_if_exists ( self . _get_cache_filename ( bucket ) , '<STR_LIT:rb>' ) <EOL> if f is not None : <EOL> try : <EOL> bucket . load_bytecode ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> def dump_bytecode ( self , bucket ) : <EOL> f = open ( self . _get_cache_filename ( bucket ) , '<STR_LIT:wb>' ) <EOL> try : <EOL> bucket . write_bytecode ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> def clear ( self ) : <EOL> from os import remove <EOL> files = fnmatch . filter ( listdir ( self . directory ) , self . pattern % '<STR_LIT:*>' ) <EOL> for filename in files : <EOL> try : <EOL> remove ( path . join ( self . directory , filename ) ) <EOL> except OSError : <EOL> pass <EOL> class MemcachedBytecodeCache ( BytecodeCache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , prefix = '<STR_LIT>' , timeout = None ) : <EOL> self . client = client <EOL> self . prefix = prefix <EOL> self . timeout = timeout <EOL> def load_bytecode ( self , bucket ) : <EOL> code = self . client . get ( self . prefix + bucket . key ) <EOL> if code is not None : <EOL> bucket . bytecode_from_string ( code ) <EOL> def dump_bytecode ( self , bucket ) : <EOL> args = ( self . prefix + bucket . key , bucket . bytecode_to_string ( ) ) <EOL> if self . timeout is not None : <EOL> args += ( self . timeout , ) <EOL> self . client . set ( * args ) </s>
