Write content to disk where content is map from string => (int, string). If target is int, write int random bytes. Otherwise write contents of string.
(content_map, interp=None, seed=31337, perms=0o644)
| 116 | |
| 117 | @contextlib.contextmanager |
| 118 | def temporary_content(content_map, interp=None, seed=31337, perms=0o644): |
| 119 | # type: (Mapping[str, Union[int, str]], Optional[Dict[str, Any]], int, int) -> Iterator[str] |
| 120 | """Write content to disk where content is map from string => (int, string). |
| 121 | |
| 122 | If target is int, write int random bytes. Otherwise write contents of string. |
| 123 | """ |
| 124 | random.seed(seed) |
| 125 | interp = interp or {} |
| 126 | with temporary_dir() as td: |
| 127 | for filename, size_or_content in content_map.items(): |
| 128 | dest = os.path.join(td, filename) |
| 129 | safe_mkdir(os.path.dirname(dest)) |
| 130 | with open(dest, "wb") as fp: |
| 131 | if isinstance(size_or_content, int): |
| 132 | fp.write(random_bytes(size_or_content)) |
| 133 | else: |
| 134 | fp.write((size_or_content % interp).encode("utf-8")) |
| 135 | os.chmod(dest, perms) |
| 136 | yield td |
| 137 | |
| 138 | |
| 139 | @contextlib.contextmanager |