in antlir/compiler/procfs_serde.py [0:0]
def deserialize_untyped(path: Path, path_with_ext: str) -> Any:
# `isdir` and `isfile` follow symbolic links so use `normalized_subpath`
# to prevent the use of symlinks that take us outside the base path.
if os.path.isdir(path.normalized_subpath(path_with_ext)):
return {
k: deserialize_untyped(path, os.path.join(path_with_ext, k))
for k in os.listdir(path.normalized_subpath(path_with_ext).decode())
}
elif os.path.isfile(path.normalized_subpath(path_with_ext)):
with open(path.normalized_subpath(path_with_ext), "rb") as f:
s = f.read()
_, ext = os.path.splitext(path_with_ext)
if ext == ".bin":
return s
# All other extensions had a trailing newline appended.
if not s.endswith(b"\n"):
raise AssertionError(
f"{path_with_ext} must have had a trailing newline, got {s}"
)
s = s[:-1]
if ext == ".image_path" or ext == ".host_path":
return s
elif ext == "":
return s.decode()
else:
raise AssertionError(f"Unsupported extension {path_with_ext}")
else:
raise AssertionError(f"{path_with_ext} is neither a file nor a dir")