Skip to content

Commit 203b107

Browse files
OSS-Fuzz Teamcopybara-github
authored andcommitted
Add a switch for generating .tgz snapshots
PiperOrigin-RevId: 777002237
1 parent e22fa3e commit 203b107

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def copy_shared_libraries(
440440
raise e
441441

442442

443-
def archive_target(target: BinaryMetadata) -> Path | None:
443+
def archive_target(target: BinaryMetadata, file_extension: str) -> Path | None:
444444
"""Archives a single target in the project using the exported rootfs."""
445445
logging.info('archive_target %s', target.name)
446446
index_dir = INDEXES_PATH / target.build_id
@@ -474,7 +474,7 @@ def archive_target(target: BinaryMetadata) -> Path | None:
474474

475475
set_interpreter(target_path, lib_mount_path)
476476
set_target_rpath(target_path, lib_mount_path)
477-
archive_path = SNAPSHOT_DIR / f'{uuid}.tar'
477+
archive_path = SNAPSHOT_DIR / f'{uuid}{file_extension}'
478478
# For `/` in $PROJECT.
479479
archive_path.parent.mkdir(parents=True, exist_ok=True)
480480

@@ -511,6 +511,7 @@ def test_and_archive(
511511
target_args: list[str],
512512
target_env: dict[str, str],
513513
targets_to_index: Sequence[str] | None,
514+
file_extension: str,
514515
):
515516
"""Test target and archive."""
516517
targets = enumerate_build_targets(target_args, target_env)
@@ -532,7 +533,7 @@ def test_and_archive(
532533
except Exception: # pylint: disable=broad-exception-caught
533534
logging.exception('Error testing target.')
534535
continue
535-
archive_target(target)
536+
archive_target(target, file_extension)
536537

537538

538539
def clear_out():
@@ -598,6 +599,11 @@ def main():
598599
action='append',
599600
help='An argument to pass to the `compile` script.',
600601
)
602+
parser.add_argument(
603+
'--compressed',
604+
action='store_true',
605+
help='Use gzipped tar (.tgz) for the output snapshot',
606+
)
601607
args = parser.parse_args()
602608

603609
# Clean up the existing OUT by default, otherwise we may run into various
@@ -639,7 +645,8 @@ def main():
639645
logging.info('No target env specified.')
640646
target_env = {}
641647

642-
test_and_archive(target_args, target_env, targets_to_index)
648+
file_extension = '.tgz' if args.compressed else '.tar'
649+
test_and_archive(target_args, target_env, targets_to_index, file_extension)
643650

644651
for snapshot in SNAPSHOT_DIR.iterdir():
645652
shutil.move(str(snapshot), OUT)

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
class IndexBuildTest(unittest.TestCase):
3737

3838
def _build_project(
39-
self, project: str, *additional_args
39+
self, project: str, *additional_args, compressed: bool
4040
) -> Sequence[pathlib.Path]:
4141
subprocess.run(
4242
('python3', 'infra/helper.py', 'build_image', '--no-pull', project),
@@ -62,11 +62,16 @@ def _build_project(
6262
if additional_args:
6363
docker_args.extend(additional_args)
6464

65+
file_suffix = '.tar'
66+
if compressed:
67+
docker_args.append('--compressed')
68+
file_suffix = '.tgz'
69+
6570
subprocess.run(docker_args, cwd=OSS_FUZZ_DIR, check=True)
6671
return [
6772
file
6873
for file in out_dir.iterdir()
69-
if file.suffix == '.tar' and file.name.startswith(project)
74+
if file.suffix == file_suffix and file.name.startswith(project)
7075
]
7176

7277
def _check_archive(self, archive_path: pathlib.Path):
@@ -113,17 +118,22 @@ def _check_archive(self, archive_path: pathlib.Path):
113118

114119
def test_basic_build(self):
115120
"""Test basic build."""
116-
archives = self._build_project('expat')
117-
self.assertGreater(len(archives), 0)
118-
for archive in archives:
119-
self._check_archive(archive)
121+
for compressed in (False, True):
122+
archives = self._build_project('expat', compressed=compressed)
123+
self.assertGreater(len(archives), 0)
124+
for archive in archives:
125+
self._check_archive(archive)
120126

121127
def test_build_with_target_allowlist(self):
122128
"""Test basic build with target allowlist."""
123-
archives = self._build_project(
124-
'expat', '--targets', 'xml_parse_fuzzer_UTF-8'
125-
)
126-
self.assertEqual(len(archives), 1)
127-
self.assertIn('xml_parse_fuzzer_UTF-8', archives[0].name)
128-
for archive in archives:
129-
self._check_archive(archive)
129+
for compressed in (False, True):
130+
archives = self._build_project(
131+
'expat',
132+
'--targets',
133+
'xml_parse_fuzzer_UTF-8',
134+
compressed=compressed,
135+
)
136+
self.assertEqual(len(archives), 1)
137+
self.assertIn('xml_parse_fuzzer_UTF-8', archives[0].name)
138+
for archive in archives:
139+
self._check_archive(archive)

0 commit comments

Comments
 (0)