MCPcopy
hub / github.com/pex-tool/pex / temporary_content

Function temporary_content

testing/__init__.py:118–136  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

116
117@contextlib.contextmanager
118def 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

Callers 10

test_pex_executableFunction · 0.90
add_wheelFunction · 0.90
add_sourcesFunction · 0.90
make_projectFunction · 0.85

Calls 6

temporary_dirFunction · 0.90
safe_mkdirFunction · 0.90
random_bytesFunction · 0.85
joinMethod · 0.45
writeMethod · 0.45
encodeMethod · 0.45

Tested by 9

test_pex_executableFunction · 0.72
add_wheelFunction · 0.72
add_sourcesFunction · 0.72