Skip to content

Commit 815dc98

Browse files
chronos: simplify dispatcher and remove unneeded output redirection (google#14127)
Signed-off-by: David Korczynski <[email protected]>
1 parent 4107371 commit 815dc98

File tree

1 file changed

+73
-134
lines changed

1 file changed

+73
-134
lines changed

infra/experimental/chronos/manager.py

Lines changed: 73 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
def _get_oss_fuzz_build_status(project):
4545
"""Returns the build status of a project in OSS-Fuzz."""
46-
#global OSS_FUZZ_BUILD_HISTORY
4746
if not OSS_FUZZ_BUILD_HISTORY:
4847
# Load the build history from a file or other source.
4948
# This is a placeholder for actual implementation.
@@ -71,48 +70,19 @@ def _get_project_cached_named_local(project, sanitizer='address'):
7170
return f'{project}-origin-{sanitizer}'
7271

7372

74-
def build_project_image(project, container_output='stdout'):
73+
def build_project_image(project):
7574
"""Build OSS-Fuzz base image for a project."""
7675

77-
if container_output == 'file':
78-
out_idx = 0
79-
stdout_file = os.path.join('projects', project,
80-
f'build_image_stdout.{out_idx}.out')
81-
while os.path.isfile(stdout_file):
82-
out_idx += 1
83-
stdout_file = os.path.join('projects', project,
84-
f'build_image_stdout.{out_idx}.out')
85-
stderr_file = os.path.join('projects', project,
86-
f'build_image_stderr.{out_idx}.err')
87-
stdout_fp = open(stdout_file, 'w', encoding='utf-8')
88-
stderr_fp = open(stderr_file, 'w', encoding='utf-8')
89-
elif container_output == 'silent':
90-
stdout_fp = subprocess.DEVNULL
91-
stderr_fp = subprocess.DEVNULL
92-
else:
93-
stdout_fp = None
94-
stderr_fp = None
95-
9676
cmd = ['docker', 'build', '-t', 'gcr.io/oss-fuzz/' + project, '.']
9777
try:
9878
subprocess.check_call(' '.join(cmd),
9979
shell=True,
100-
cwd=os.path.join('projects', project),
101-
stdout=stdout_fp,
102-
stderr=stderr_fp)
103-
if container_output == 'file':
104-
stdout_fp.close()
105-
stderr_fp.close()
80+
cwd=os.path.join('projects', project))
10681
except subprocess.CalledProcessError:
107-
if container_output == 'file':
108-
stdout_fp.close()
109-
stderr_fp.close()
82+
pass
11083

11184

112-
def build_cached_project(project,
113-
cleanup=True,
114-
sanitizer='address',
115-
container_output='stdout'):
85+
def build_cached_project(project, cleanup=True, sanitizer='address'):
11686
"""Build cached image for a project."""
11787
container_name = _get_project_cached_named_local(project, sanitizer)
11888

@@ -125,25 +95,6 @@ def build_cached_project(project,
12595
except subprocess.CalledProcessError:
12696
pass
12797

128-
if container_output == 'file':
129-
out_idx = 0
130-
stdout_file = os.path.join('projects', project,
131-
f'build_cache_stdout.{out_idx}.out')
132-
while os.path.isfile(stdout_file):
133-
out_idx += 1
134-
stdout_file = os.path.join('projects', project,
135-
f'build_cache_stdout.{out_idx}.out')
136-
stderr_file = os.path.join('projects', project,
137-
f'build_cache_stderr.{out_idx}.err')
138-
stdout_fp = open(stdout_file, 'w', encoding='utf-8')
139-
stderr_fp = open(stderr_file, 'w', encoding='utf-8')
140-
elif container_output == 'silent':
141-
stdout_fp = subprocess.DEVNULL
142-
stderr_fp = subprocess.DEVNULL
143-
else:
144-
stdout_fp = None
145-
stderr_fp = None
146-
14798
project_language = 'c++'
14899
cwd = os.getcwd()
149100
# Build the cached image.
@@ -160,20 +111,11 @@ def build_cached_project(project,
160111

161112
start = time.time()
162113
try:
163-
subprocess.check_call(' '.join(cmd),
164-
shell=True,
165-
stdout=stdout_fp,
166-
stderr=stderr_fp)
114+
subprocess.check_call(' '.join(cmd), shell=True)
167115
end = time.time()
168116
logger.info('%s vanilla build Succeeded: Duration: %.2f seconds', project,
169117
end - start)
170-
if container_output == 'file':
171-
stdout_fp.close()
172-
stderr_fp.close()
173118
except subprocess.CalledProcessError:
174-
if container_output == 'file':
175-
stdout_fp.close()
176-
stderr_fp.close()
177119
end = time.time()
178120
logger.info('%s vanilla build Failed: Duration: %.2f seconds', project,
179121
end - start)
@@ -217,16 +159,10 @@ def build_cached_project(project,
217159
return True
218160

219161

220-
def check_cached_replay(project,
221-
sanitizer='address',
222-
container_output='stdout',
223-
silent_replays=False,
224-
integrity_test=False):
162+
def check_cached_replay(project, sanitizer='address', integrity_test=False):
225163
"""Checks if a cache build succeeds and times is."""
226-
build_project_image(project, container_output=container_output)
227-
build_cached_project(project,
228-
sanitizer=sanitizer,
229-
container_output=container_output)
164+
build_project_image(project)
165+
build_cached_project(project, sanitizer=sanitizer)
230166

231167
start = time.time()
232168
base_cmd = 'export PATH=/ccache/bin:$PATH && rm -rf /out/* && compile'
@@ -245,14 +181,6 @@ def check_cached_replay(project,
245181
'-c',
246182
]
247183

248-
# Configure output
249-
if silent_replays:
250-
stdout_fp = subprocess.DEVNULL
251-
stderr_fp = subprocess.DEVNULL
252-
else:
253-
stdout_fp = None
254-
stderr_fp = None
255-
256184
if integrity_test:
257185
# Use different bad patches to test the cached replay build
258186
failed = []
@@ -267,11 +195,7 @@ def check_cached_replay(project,
267195
f'"set -euo pipefail && {bad_patch_command} && {base_cmd}"')
268196

269197
# Run the cached replay script with bad patches
270-
result = subprocess.run(' '.join(cmd_to_run),
271-
shell=True,
272-
stdout=stdout_fp,
273-
stderr=stderr_fp,
274-
check=False)
198+
result = subprocess.run(' '.join(cmd_to_run), shell=True, check=False)
275199

276200
if result.returncode not in expected_rc:
277201
failed.append(bad_patch_name)
@@ -289,11 +213,7 @@ def check_cached_replay(project,
289213
else:
290214
# Normal run with no integrity check
291215
cmd.append(f'"{base_cmd}"')
292-
subprocess.run(' '.join(cmd),
293-
shell=True,
294-
stdout=stdout_fp,
295-
stderr=stderr_fp,
296-
check=False)
216+
subprocess.run(' '.join(cmd), shell=True, check=False)
297217

298218
end = time.time()
299219
logger.info('%s check cached replay completion time: %.2f seconds', project,
@@ -302,7 +222,6 @@ def check_cached_replay(project,
302222

303223
def check_test(project,
304224
sanitizer='address',
305-
container_output='stdout',
306225
run_full_cache_replay=False,
307226
integrity_test=False):
308227
"""Run the `run_tests.sh` script for a specific project. Will
@@ -318,16 +237,11 @@ def check_test(project,
318237
logger.info('Building image for project for use in check-tests: %s', project)
319238
# Build an OSS-Fuzz image of the project
320239
if run_full_cache_replay:
321-
check_cached_replay(
322-
project,
323-
sanitizer,
324-
container_output,
325-
silent_replays=(True if container_output == 'silent' else False))
240+
check_cached_replay(project, sanitizer)
326241
else:
327-
build_project_image(project, container_output)
242+
build_project_image(project)
328243
# build a cached version of the project
329-
if not build_cached_project(
330-
project, sanitizer=sanitizer, container_output=container_output):
244+
if not build_cached_project(project, sanitizer=sanitizer):
331245
return False
332246

333247
# Run the test script
@@ -422,14 +336,11 @@ def check_test(project,
422336

423337
def check_run_tests_script(project,
424338
sanitizer='address',
425-
ignore_new_files=False,
426-
container_output='stdout'):
339+
ignore_new_files=False):
427340
"""Checks if the run_tests.sh changes the source files in the current directory."""
428341

429-
build_project_image(project, container_output=container_output)
430-
build_cached_project(project,
431-
sanitizer=sanitizer,
432-
container_output=container_output)
342+
build_project_image(project)
343+
build_cached_project(project, sanitizer=sanitizer)
433344

434345
ignore = ''
435346
if ignore_new_files:
@@ -584,8 +495,8 @@ def autogen_projects(apply_filtering=False,
584495

585496
def extract_test_coverage(project):
586497
"""Extract code coverage report from run_tests.sh script."""
587-
build_project_image(project, container_output='')
588-
build_cached_project(project, sanitizer='coverage', container_output='stdout')
498+
build_project_image(project)
499+
build_cached_project(project, sanitizer='coverage')
589500

590501
os.makedirs(os.path.join('build', 'out', project), exist_ok=True)
591502

@@ -620,6 +531,44 @@ def extract_test_coverage(project):
620531
return True
621532

622533

534+
def _cmd_dispatcher_check_test(args):
535+
check_test(args.project, args.sanitizer, args.run_full_cache_replay,
536+
args.check_patch_integrity)
537+
538+
539+
def _cmd_dispatcher_check_replay(args):
540+
check_cached_replay(args.project, args.sanitizer)
541+
542+
543+
def _cmd_dispatcher_check_replay_integrity(args):
544+
check_cached_replay(args.project, args.sanitizer, integrity_test=True)
545+
546+
547+
def _cmd_dispatcher_build_cached_image(args):
548+
build_cached_project(args.project, sanitizer=args.sanitizer)
549+
550+
551+
def _cmd_dispatcher_autogen_tests(args):
552+
autogen_projects(args.apply_filtering, args.max_projects_to_try,
553+
args.projects)
554+
555+
556+
def _cmd_dispatcher_build_many_caches(args):
557+
for project in args.projects:
558+
logger.info('Building cached project: %s', project)
559+
check_cached_replay(project, sanitizer=args.sanitizer)
560+
561+
562+
def _cmd_dispatcher_extract_coverage(args):
563+
extract_test_coverage(args.project)
564+
565+
566+
def _cmd_check_run_tests_script(args):
567+
check_run_tests_script(args.project,
568+
sanitizer=args.sanitizer,
569+
ignore_new_files=args.ignore_new_files)
570+
571+
623572
def parse_args():
624573
"""Parses command line arguments for the manager script."""
625574
parser = argparse.ArgumentParser(
@@ -771,33 +720,23 @@ def main():
771720

772721
args = parse_args()
773722

774-
if args.command == 'check-test':
775-
logger.info('Executing check-test command on %s to check run_tests',
776-
args.project)
777-
check_test(args.project, args.sanitizer, args.container_output,
778-
args.run_full_cache_replay, args.check_patch_integrity)
779-
if args.command == 'check-replay-script':
780-
check_cached_replay(args.project, args.sanitizer)
781-
if args.command == 'check-replay-script-integrity':
782-
check_cached_replay(args.project, args.sanitizer, integrity_test=True)
783-
if args.command == 'build-cached-image':
784-
build_cached_project(args.project,
785-
sanitizer=args.sanitizer,
786-
container_output=args.container_output)
787-
if args.command == 'autogen-tests':
788-
autogen_projects(args.apply_filtering, args.max_projects_to_try,
789-
args.container_output, args.projects)
790-
if args.command == 'build-many-caches':
791-
for project in args.projects:
792-
logger.info('Building cached project: %s', project)
793-
check_cached_replay(project,
794-
sanitizer=args.sanitizer,
795-
container_output=args.container_output,
796-
silent_replays=args.silent_replays)
797-
if args.command == 'extract-test-coverage':
798-
extract_test_coverage(args.project)
799-
if args.command == 'check-run-tests-script':
800-
check_run_tests_script(args.project, args.sanitizer, args.ignore_new_files)
723+
dispatch_map = {
724+
'check-test': _cmd_dispatcher_check_test,
725+
'check-replay-script': _cmd_dispatcher_check_replay,
726+
'check-replay-script-integrity': _cmd_dispatcher_check_replay_integrity,
727+
'build-cached-image': _cmd_dispatcher_build_cached_image,
728+
'autogen-tests': _cmd_dispatcher_autogen_tests,
729+
'build-many-caches': _cmd_dispatcher_build_many_caches,
730+
'extract-test-coverage': _cmd_dispatcher_extract_coverage,
731+
'check-run-tests-script': _cmd_check_run_tests_script
732+
}
733+
734+
dispatch_cmd = dispatch_map.get(args.command, None)
735+
if not dispatch_cmd:
736+
logger.error('Unknown command: %s', args.command)
737+
sys.exit(1)
738+
logger.info('Dispatching command: %s', args.command)
739+
dispatch_cmd(args)
801740

802741

803742
if __name__ == '__main__':

0 commit comments

Comments
 (0)