Skip to content

Commit c06b5d3

Browse files
OSS-Fuzz Teamcopybara-github
authored andcommitted
Snapshot utilities refactoring
PiperOrigin-RevId: 839289168
1 parent c8c1b25 commit c06b5d3

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

infra/base-images/base-builder/indexer/manifest_constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
OBJ_DIR = Path("obj")
3333
# Directory for indexer data.
3434
INDEX_DIR = Path("idx")
35+
# Relative source file root in the index.
36+
INDEX_RELATIVE_SOURCES = INDEX_DIR / "relative"
37+
# Absolute source file root in the index.
38+
INDEX_ABSOLUTE_SOURCES = INDEX_DIR / "absolute"
3539
# The index database filename.
3640
INDEX_DB = Path("db.sqlite")
3741
# Library directory, where shared libraries are copied - inside obj.

infra/base-images/base-builder/indexer/utils.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
"""Utils for snapshotting shared libraries."""
1717

18+
from collections.abc import Mapping, Sequence
1819
import dataclasses
1920
import os
2021
import pathlib
2122
import re
2223
import subprocess
23-
from typing import Final, Sequence
24+
from typing import Final, Protocol
2425

2526
from absl import logging
2627

@@ -69,22 +70,40 @@ def _parse_ld_trace_output(output: str) -> Sequence[SharedLibrary]:
6970
return shared_libraries
7071

7172

72-
def get_shared_libraries(
73-
binary_path: os.PathLike[str],
74-
) -> Sequence[SharedLibrary]:
75-
"""Copies the shared libraries to the shared directory."""
76-
env = os.environ.copy()
77-
env["LD_TRACE_LOADED_OBJECTS"] = "1"
78-
env["LD_BIND_NOW"] = "1"
73+
class CommandRunner(Protocol):
74+
"""Runs `command` with environment `env` and returns its stdout."""
75+
76+
def __call__(
77+
self,
78+
command: Sequence[str | os.PathLike[str]],
79+
env: Mapping[str, str] | None = None,
80+
) -> bytes:
81+
pass
82+
7983

80-
result = subprocess.run(
81-
[_LD_BINARY_PATH, binary_path],
84+
def run_subprocess(
85+
command: Sequence[str | os.PathLike[str]],
86+
env: Mapping[str, str] | None = None,
87+
) -> bytes:
88+
return subprocess.run(
89+
command,
8290
capture_output=True,
8391
env=env,
8492
check=True,
85-
)
93+
).stdout
94+
8695

87-
return _parse_ld_trace_output(result.stdout.decode())
96+
def get_shared_libraries(
97+
binary_path: os.PathLike[str],
98+
command_runner: CommandRunner = run_subprocess,
99+
) -> Sequence[SharedLibrary]:
100+
"""Copies the shared libraries to the shared directory."""
101+
env = os.environ | {
102+
"LD_TRACE_LOADED_OBJECTS": "1",
103+
"LD_BIND_NOW": "1",
104+
}
105+
stdout_bytes = command_runner([_LD_BINARY_PATH, binary_path], env=env)
106+
return _parse_ld_trace_output(stdout_bytes.decode())
88107

89108

90109
def copy_shared_libraries(

0 commit comments

Comments
 (0)