<s> import os <EOL> import random <EOL> import string <EOL> import lockfile <EOL> class FilePermissionError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def generate_key ( key_length = <NUM_LIT:64> ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( random , '<STR_LIT>' ) : <EOL> choice = random . SystemRandom ( ) . choice <EOL> else : <EOL> choice = random . choice <EOL> return '<STR_LIT>' . join ( map ( lambda x : choice ( string . digits + string . ascii_letters ) , <EOL> range ( key_length ) ) ) <EOL> def generate_or_read_from_file ( key_file = '<STR_LIT>' , key_length = <NUM_LIT:64> ) : <EOL> """<STR_LIT>""" <EOL> lock = lockfile . FileLock ( key_file ) <EOL> with lock : <EOL> if not os . path . exists ( key_file ) : <EOL> key = generate_key ( key_length ) <EOL> old_umask = os . umask ( <NUM_LIT> ) <EOL> with open ( key_file , '<STR_LIT:w>' ) as f : <EOL> f . write ( key ) <EOL> os . umask ( old_umask ) <EOL> else : <EOL> if ( os . stat ( key_file ) . st_mode & <NUM_LIT> ) != <NUM_LIT> : <EOL> raise FilePermissionError ( "<STR_LIT>" ) <EOL> with open ( key_file , '<STR_LIT:r>' ) as f : <EOL> key = f . readline ( ) <EOL> return key </s>
