Coverage for cogapp/makefiles.py: 11.11%
22 statements
« prev ^ index » next coverage.py v7.5.0a1.dev1, created at 2024-04-15 15:50 -0400
« prev ^ index » next coverage.py v7.5.0a1.dev1, created at 2024-04-15 15:50 -0400
1""" Dictionary-to-filetree functions, to create test files for testing.
2"""
4import os.path
6from .whiteutils import reindentBlock
9def makeFiles(d, basedir='.'):
10 """ Create files from the dictionary `d`, in the directory named by `basedir`.
11 """
12 for name, contents in d.items():
13 child = os.path.join(basedir, name)
14 if isinstance(contents, (bytes, str)):
15 mode = "w"
16 if isinstance(contents, bytes):
17 mode += "b"
18 with open(child, mode) as f:
19 f.write(reindentBlock(contents))
20 else:
21 if not os.path.exists(child):
22 os.mkdir(child)
23 makeFiles(contents, child)
25def removeFiles(d, basedir='.'):
26 """ Remove the files created by makeFiles.
27 Directories are removed if they are empty.
28 """
29 for name, contents in d.items():
30 child = os.path.join(basedir, name)
31 if isinstance(contents, (bytes, str)):
32 os.remove(child)
33 else:
34 removeFiles(contents, child)
35 if not os.listdir(child):
36 os.rmdir(child)