#NEWLINE# def create_test_directory( tree ) : #NEWLINE# #INDENT# '\n    Creates a temporary directory with the given file tree in it.\n\n    :param tree: A list of strings and tuples where each string is a path to\n        a directory that should exist within the temporary directory (relative\n        to the root of the directory) and each tuple is a file that should\n        exist within the temporary directory represented as ``(path, size)``,\n        the file will be created and filled with ``size`` bytes of data.\n    :returns: A path to the temporary directory.\n\n    .. note::\n\n        The files and directories are created in the order they appear in the\n        list. If a file is specfied in a directory that has not previously been\n        created an exception will be thrown.\n\n    >>> create_test_directory(["a", ("a/bla", 12)])\n    "/tmp/asdf"\n    >>> print list(os.walk("/tmp/asdf"))\n\n    ' #NEWLINE# temp_dir = tempfile . mkdtemp ( ) #NEWLINE# resolve_path = ( lambda x : os . path . join ( temp_dir , x ) ) #NEWLINE# for i in tree : #NEWLINE# #INDENT# if ( isinstance ( i , str ) or isinstance ( i , unicode ) ) : #NEWLINE# #INDENT# os . mkdir ( resolve_path ( i ) , 448 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( isinstance ( i , tuple ) and ( len ( i ) == 2 ) ) : #NEWLINE# #INDENT# ( path , size ) = i #NEWLINE# with open ( resolve_path ( path ) , 'w' ) as f : #NEWLINE# #INDENT# symbols = ( string . ascii_letters + string . digits ) #NEWLINE# for i in xrange ( size ) : #NEWLINE# #INDENT# f . write ( random . choice ( symbols ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( 'All items in list must be string or two-tuple.' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return temp_dir