Skip to content

Commit fe8bbfd

Browse files
fix: avoid running out of disk space (#558)
by cleaning up after each inner synth run
1 parent cb3433f commit fe8bbfd

File tree

1 file changed

+79
-68
lines changed

1 file changed

+79
-68
lines changed

autosynth/synth.py

Lines changed: 79 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -519,82 +519,93 @@ def _inner_main(temp_dir: str) -> int:
519519

520520
working_repo_path = synthtool_git.clone(f"https://github.com/{args.repository}.git")
521521

522-
os.chdir(working_repo_path)
522+
try:
523+
os.chdir(working_repo_path)
523524

524-
git.configure_git(args.github_user, args.github_email)
525+
git.configure_git(args.github_user, args.github_email)
525526

526-
git.setup_branch(branch)
527+
git.setup_branch(branch)
527528

528-
if args.synth_path:
529-
os.chdir(args.synth_path)
530-
531-
metadata_path = os.path.join(args.metadata_path or "", "synth.metadata")
532-
533-
flags = autosynth.flags.parse_flags()
534-
# Override flags specified in synth.py with flags specified in environment vars.
535-
for key in flags.keys():
536-
env_value = os.environ.get(key, "")
537-
if env_value:
538-
flags[key] = False if env_value.lower() == "false" else env_value
539-
540-
metadata = load_metadata(metadata_path)
541-
multiple_commits = flags[autosynth.flags.AUTOSYNTH_MULTIPLE_COMMITS]
542-
multiple_prs = flags[autosynth.flags.AUTOSYNTH_MULTIPLE_PRS]
543-
if (not multiple_commits and not multiple_prs) or not metadata:
544-
if change_pusher.check_if_pr_already_exists(branch):
545-
return 0
546-
547-
synth_log = Synthesizer(
548-
metadata_path,
549-
args.extra_args,
550-
deprecated_execution=args.deprecated_execution,
551-
).synthesize(base_synth_log_path)
552-
553-
if not has_changes():
554-
logger.info("No changes. :)")
555-
sys.exit(EXIT_CODE_SKIPPED)
556-
557-
git.commit_all_changes(pr_title)
558-
change_pusher.push_changes(1, branch, pr_title, synth_log)
559-
return 1
529+
if args.synth_path:
530+
os.chdir(args.synth_path)
560531

561-
else:
562-
if not multiple_prs and change_pusher.check_if_pr_already_exists(branch):
563-
return 0 # There's already an existing PR
564-
565-
# Enumerate the versions to loop over.
566-
sources = metadata.get("sources", [])
567-
source_versions = [
568-
git_source.enumerate_versions_for_working_repo(metadata_path, sources)
569-
]
570-
# Add supported source version types below:
571-
source_versions.extend(
572-
git_source.enumerate_versions(sources, pathlib.Path(temp_dir))
573-
)
532+
metadata_path = os.path.join(args.metadata_path or "", "synth.metadata")
574533

575-
# Prepare to call synthesize loop.
576-
synthesizer = Synthesizer(
577-
metadata_path, args.extra_args, args.deprecated_execution, "synth.py",
578-
)
579-
x = SynthesizeLoopToolbox(
580-
source_versions,
581-
branch,
582-
temp_dir,
583-
metadata_path,
584-
args.synth_path,
585-
base_synth_log_path,
586-
)
587-
if not multiple_commits:
588-
change_pusher = SquashingChangePusher(change_pusher)
534+
flags = autosynth.flags.parse_flags()
535+
# Override flags specified in synth.py with flags specified in environment vars.
536+
for key in flags.keys():
537+
env_value = os.environ.get(key, "")
538+
if env_value:
539+
flags[key] = False if env_value.lower() == "false" else env_value
540+
541+
metadata = load_metadata(metadata_path)
542+
multiple_commits = flags[autosynth.flags.AUTOSYNTH_MULTIPLE_COMMITS]
543+
multiple_prs = flags[autosynth.flags.AUTOSYNTH_MULTIPLE_PRS]
544+
if (not multiple_commits and not multiple_prs) or not metadata:
545+
if change_pusher.check_if_pr_already_exists(branch):
546+
return 0
547+
548+
synth_log = Synthesizer(
549+
metadata_path,
550+
args.extra_args,
551+
deprecated_execution=args.deprecated_execution,
552+
).synthesize(base_synth_log_path)
553+
554+
if not has_changes():
555+
logger.info("No changes. :)")
556+
sys.exit(EXIT_CODE_SKIPPED)
589557

590-
# Call the loop.
591-
commit_count = synthesize_loop(x, multiple_prs, change_pusher, synthesizer)
558+
git.commit_all_changes(pr_title)
559+
change_pusher.push_changes(1, branch, pr_title, synth_log)
560+
return 1
592561

593-
if commit_count == 0:
594-
logger.info("No changes. :)")
595-
sys.exit(EXIT_CODE_SKIPPED)
562+
else:
563+
if not multiple_prs and change_pusher.check_if_pr_already_exists(branch):
564+
return 0 # There's already an existing PR
565+
566+
# Enumerate the versions to loop over.
567+
sources = metadata.get("sources", [])
568+
source_versions = [
569+
git_source.enumerate_versions_for_working_repo(metadata_path, sources)
570+
]
571+
# Add supported source version types below:
572+
source_versions.extend(
573+
git_source.enumerate_versions(sources, pathlib.Path(temp_dir))
574+
)
575+
576+
# Prepare to call synthesize loop.
577+
synthesizer = Synthesizer(
578+
metadata_path, args.extra_args, args.deprecated_execution, "synth.py",
579+
)
580+
x = SynthesizeLoopToolbox(
581+
source_versions,
582+
branch,
583+
temp_dir,
584+
metadata_path,
585+
args.synth_path,
586+
base_synth_log_path,
587+
)
588+
if not multiple_commits:
589+
change_pusher = SquashingChangePusher(change_pusher)
596590

597-
return commit_count
591+
# Call the loop.
592+
commit_count = synthesize_loop(x, multiple_prs, change_pusher, synthesizer)
593+
594+
if commit_count == 0:
595+
logger.info("No changes. :)")
596+
sys.exit(EXIT_CODE_SKIPPED)
597+
598+
return commit_count
599+
finally:
600+
if args.synth_path:
601+
# We're generating code in a mono repo. The state left behind will
602+
# probably be useful for generating the next API.
603+
pass
604+
else:
605+
# We're generating a single API in a single repo, and using a different
606+
# repo to generate the next API. So the next synth will not be able to
607+
# use any of this state. Clean it up to avoid running out of disk space.
608+
executor.check_call(["git", "clean", "-fdx"], cwd=working_repo_path)
598609

599610

600611
if __name__ == "__main__":

0 commit comments

Comments
 (0)