<s> import SCons . compat <EOL> import builtins <EOL> import os <EOL> import pickle <EOL> import shutil <EOL> import time <EOL> keep_all_files = <NUM_LIT> <EOL> ignore_corrupt_dbfiles = <NUM_LIT:0> <EOL> def corruption_warning ( filename ) : <EOL> print "<STR_LIT>" , filename <EOL> try : unicode <EOL> except NameError : <EOL> def is_string ( s ) : <EOL> return isinstance ( s , str ) <EOL> else : <EOL> def is_string ( s ) : <EOL> return type ( s ) in ( str , unicode ) <EOL> try : <EOL> unicode ( '<STR_LIT:a>' ) <EOL> except NameError : <EOL> def unicode ( s ) : return s <EOL> dblite_suffix = '<STR_LIT>' <EOL> tmp_suffix = '<STR_LIT>' <EOL> class dblite ( object ) : <EOL> _open = builtins . open <EOL> _pickle_dump = staticmethod ( pickle . dump ) <EOL> _os_chmod = os . chmod <EOL> try : <EOL> _os_chown = os . chown <EOL> except AttributeError : <EOL> _os_chown = None <EOL> _os_rename = os . rename <EOL> _os_unlink = os . unlink <EOL> _shutil_copyfile = shutil . copyfile <EOL> _time_time = time . time <EOL> def __init__ ( self , file_base_name , flag , mode ) : <EOL> assert flag in ( None , "<STR_LIT:r>" , "<STR_LIT:w>" , "<STR_LIT:c>" , "<STR_LIT:n>" ) <EOL> if ( flag is None ) : flag = "<STR_LIT:r>" <EOL> base , ext = os . path . splitext ( file_base_name ) <EOL> if ext == dblite_suffix : <EOL> self . _file_name = file_base_name <EOL> self . _tmp_name = base + tmp_suffix <EOL> else : <EOL> self . _file_name = file_base_name + dblite_suffix <EOL> self . _tmp_name = file_base_name + tmp_suffix <EOL> self . _flag = flag <EOL> self . _mode = mode <EOL> self . _dict = { } <EOL> self . _needs_sync = <NUM_LIT> <EOL> if self . _os_chown is not None and ( os . geteuid ( ) == <NUM_LIT:0> or os . getuid ( ) == <NUM_LIT:0> ) : <EOL> try : <EOL> statinfo = os . stat ( self . _file_name ) <EOL> self . _chown_to = statinfo . st_uid <EOL> self . _chgrp_to = statinfo . st_gid <EOL> except OSError , e : <EOL> self . _chown_to = int ( os . environ . get ( '<STR_LIT>' , - <NUM_LIT:1> ) ) <EOL> self . _chgrp_to = int ( os . environ . get ( '<STR_LIT>' , - <NUM_LIT:1> ) ) <EOL> else : <EOL> self . _chown_to = - <NUM_LIT:1> <EOL> self . _chgrp_to = - <NUM_LIT:1> <EOL> if ( self . _flag == "<STR_LIT:n>" ) : <EOL> self . _open ( self . _file_name , "<STR_LIT:wb>" , self . _mode ) <EOL> else : <EOL> try : <EOL> f = self . _open ( self . _file_name , "<STR_LIT:rb>" ) <EOL> except IOError , e : <EOL> if ( self . _flag != "<STR_LIT:c>" ) : <EOL> raise e <EOL> self . _open ( self . _file_name , "<STR_LIT:wb>" , self . _mode ) <EOL> else : <EOL> p = f . read ( ) <EOL> if ( len ( p ) > <NUM_LIT:0> ) : <EOL> try : <EOL> self . _dict = pickle . loads ( p ) <EOL> except ( pickle . UnpicklingError , EOFError ) : <EOL> if ( ignore_corrupt_dbfiles == <NUM_LIT:0> ) : raise <EOL> if ( ignore_corrupt_dbfiles == <NUM_LIT:1> ) : <EOL> corruption_warning ( self . _file_name ) <EOL> def close ( self ) : <EOL> if ( self . _needs_sync ) : <EOL> self . sync ( ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def sync ( self ) : <EOL> self . _check_writable ( ) <EOL> f = self . _open ( self . _tmp_name , "<STR_LIT:wb>" , self . _mode ) <EOL> self . _pickle_dump ( self . _dict , f , <NUM_LIT:1> ) <EOL> f . close ( ) <EOL> try : self . _os_chmod ( self . _file_name , <NUM_LIT:0> <NUM_LIT> ) <EOL> except OSError : pass <EOL> self . _os_unlink ( self . _file_name ) <EOL> self . _os_rename ( self . _tmp_name , self . _file_name ) <EOL> if self . _os_chown is not None and self . _chown_to > <NUM_LIT:0> : <EOL> try : <EOL> self . _os_chown ( self . _file_name , self . _chown_to , self . _chgrp_to ) <EOL> except OSError : <EOL> pass <EOL> self . _needs_sync = <NUM_LIT> <EOL> if ( keep_all_files ) : <EOL> self . _shutil_copyfile ( <EOL> self . _file_name , <EOL> self . _file_name + "<STR_LIT:_>" + str ( int ( self . _time_time ( ) ) ) ) <EOL> def _check_writable ( self ) : <EOL> if ( self . _flag == "<STR_LIT:r>" ) : <EOL> raise IOError ( "<STR_LIT>" % self . _file_name ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _dict [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _check_writable ( ) <EOL> if ( not is_string ( key ) ) : <EOL> raise TypeError ( "<STR_LIT>" % ( key , type ( key ) ) ) <EOL> if ( not is_string ( value ) ) : <EOL> raise TypeError ( "<STR_LIT>" % ( value , type ( value ) ) ) <EOL> self . _dict [ key ] = value <EOL> self . _needs_sync = <NUM_LIT> <NUM_LIT:1> <EOL> def keys ( self ) : <EOL> return list ( self . _dict . keys ( ) ) <EOL> def has_key ( self , key ) : <EOL> return key in self . _dict <EOL> def __contains__ ( self , key ) : <EOL> return key in self . _dict <EOL> def iterkeys ( self ) : <EOL> return ( self . _dict . iterkeys ) ( ) <EOL> __iter__ = iterkeys <EOL> def __len__ ( self ) : <EOL> return len ( self . _dict ) <EOL> def open ( file , flag = None , mode = <NUM_LIT:0> <NUM_LIT> ) : <EOL> return dblite ( file , flag , mode ) <EOL> def _exercise ( ) : <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:n>" ) <EOL> assert len ( db ) == <NUM_LIT:0> <EOL> db [ "<STR_LIT:foo>" ] = "<STR_LIT:bar>" <EOL> assert db [ "<STR_LIT:foo>" ] == "<STR_LIT:bar>" <EOL> db [ unicode ( "<STR_LIT>" ) ] = unicode ( "<STR_LIT>" ) <EOL> assert db [ unicode ( "<STR_LIT>" ) ] == unicode ( "<STR_LIT>" ) <EOL> db . sync ( ) <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:c>" ) <EOL> assert len ( db ) == <NUM_LIT:2> , len ( db ) <EOL> assert db [ "<STR_LIT:foo>" ] == "<STR_LIT:bar>" <EOL> db [ "<STR_LIT:bar>" ] = "<STR_LIT:foo>" <EOL> assert db [ "<STR_LIT:bar>" ] == "<STR_LIT:foo>" <EOL> db [ unicode ( "<STR_LIT>" ) ] = unicode ( "<STR_LIT>" ) <EOL> assert db [ unicode ( "<STR_LIT>" ) ] == unicode ( "<STR_LIT>" ) <EOL> db . sync ( ) <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:r>" ) <EOL> assert len ( db ) == <NUM_LIT:4> , len ( db ) <EOL> assert db [ "<STR_LIT:foo>" ] == "<STR_LIT:bar>" <EOL> assert db [ "<STR_LIT:bar>" ] == "<STR_LIT:foo>" <EOL> assert db [ unicode ( "<STR_LIT>" ) ] == unicode ( "<STR_LIT>" ) <EOL> assert db [ unicode ( "<STR_LIT>" ) ] == unicode ( "<STR_LIT>" ) <EOL> try : <EOL> db . sync ( ) <EOL> except IOError , e : <EOL> assert str ( e ) == "<STR_LIT>" <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> assert len ( db ) == <NUM_LIT:4> <EOL> db [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> db . sync ( ) <EOL> try : <EOL> db [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] = "<STR_LIT>" <EOL> except TypeError , e : <EOL> assert str ( e ) == "<STR_LIT>" , str ( e ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> try : <EOL> db [ "<STR_LIT:list>" ] = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> except TypeError , e : <EOL> assert str ( e ) == "<STR_LIT>" , str ( e ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:r>" ) <EOL> assert len ( db ) == <NUM_LIT:5> <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:n>" ) <EOL> assert len ( db ) == <NUM_LIT:0> <EOL> dblite . _open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:r>" ) <EOL> dblite . _open ( "<STR_LIT>" , "<STR_LIT:w>" ) . write ( "<STR_LIT:x>" ) <EOL> try : <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:r>" ) <EOL> except pickle . UnpicklingError : <EOL> pass <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> global ignore_corrupt_dbfiles <EOL> ignore_corrupt_dbfiles = <NUM_LIT:2> <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:r>" ) <EOL> assert len ( db ) == <NUM_LIT:0> <EOL> os . unlink ( "<STR_LIT>" ) <EOL> try : <EOL> db = open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> except IOError , e : <EOL> assert str ( e ) == "<STR_LIT>" , str ( e ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> print "<STR_LIT:OK>" <EOL> if ( __name__ == "<STR_LIT:__main__>" ) : <EOL> _exercise ( ) </s>
