stubs.py (1043B)
1 import os 2 import shutil 3 import tempfile 4 5 # stub file paths 6 files = [ 7 ("foo.txt",), 8 ( 9 "foo", 10 "bar.txt", 11 ), 12 ( 13 "foo", 14 "bar", 15 "fleem.txt", 16 ), 17 ( 18 "foobar", 19 "fleem.txt", 20 ), 21 ("bar.txt",), 22 ( 23 "nested_tree", 24 "bar", 25 "fleem.txt", 26 ), 27 ("readonly.txt",), 28 ] 29 30 31 def create_empty_stub(): 32 tempdir = tempfile.mkdtemp() 33 return tempdir 34 35 36 def create_stub(tempdir=None): 37 """create a stub directory""" 38 39 tempdir = tempdir or tempfile.mkdtemp() 40 try: 41 for path in files: 42 fullpath = os.path.join(tempdir, *path) 43 dirname = os.path.dirname(fullpath) 44 if not os.path.exists(dirname): 45 os.makedirs(dirname) 46 contents = path[-1] 47 f = open(fullpath, "w") 48 f.write(contents) 49 f.close() 50 return tempdir 51 except Exception: 52 try: 53 shutil.rmtree(tempdir) 54 except Exception: 55 pass 56 raise