<s> import functools <EOL> import os <EOL> import shutil <EOL> import tempfile <EOL> class temporary_directory ( object ) : <EOL> def __init__ ( self , prefix = '<STR_LIT>' ) : <EOL> self . prefix = prefix <EOL> def __enter__ ( self ) : <EOL> self . original_cwd = os . getcwd ( ) <EOL> self . temp_path = tempfile . mkdtemp ( prefix = self . prefix ) <EOL> os . chdir ( self . temp_path ) <EOL> return self . temp_path <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> if self . temp_path and os . path . exists ( self . temp_path ) : <EOL> shutil . rmtree ( self . temp_path ) <EOL> if self . original_cwd and os . path . exists ( self . original_cwd ) : <EOL> os . chdir ( self . original_cwd ) <EOL> def in_temporary_directory ( f ) : <EOL> @ functools . wraps ( f ) <EOL> def decorated ( * args , ** kwds ) : <EOL> with temporary_directory ( ) as directory : <EOL> from inspect import getargspec <EOL> if '<STR_LIT>' not in kwds and '<STR_LIT>' in getargspec ( f ) [ <NUM_LIT:0> ] : <EOL> kwds [ '<STR_LIT>' ] = directory <EOL> return f ( * args , ** kwds ) <EOL> decorated . __name__ = f . __name__ <EOL> return decorated </s>
