Skip to content

Commit 62226cb

Browse files
committed
Rebuild only if needed.
1 parent 04b1a7c commit 62226cb

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

build_docs.py

+37-10
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,13 @@ def run(self) -> bool:
658658
self.cpython_repo.switch(self.version.branch_or_tag)
659659
if self.language.tag != "en":
660660
self.clone_translation()
661-
self.build_venv()
662-
self.build()
663-
self.copy_build_to_webroot()
664-
self.save_state(build_duration=perf_counter() - start_time)
661+
if self.should_rebuild():
662+
self.build_venv()
663+
self.build()
664+
self.copy_build_to_webroot()
665+
self.save_state(build_duration=perf_counter() - start_time)
666+
else:
667+
logging.info("Nothing changed.")
665668
except Exception as err:
666669
logging.exception("Badly handled exception, human, please help.")
667670
if sentry_sdk:
@@ -920,7 +923,35 @@ def copy_build_to_webroot(self):
920923
purge(*[prefix + p for p in changed])
921924
logging.info("Publishing done")
922925

923-
def save_state(self, build_duration):
926+
def should_rebuild(self):
927+
state = self.load_state()
928+
if not state:
929+
return True
930+
cpython_sha = self.cpython_repo.run("rev-parse", "HEAD").stdout.strip()
931+
if self.language.tag != "en":
932+
translation_sha = self.translation_repo.run(
933+
"rev-parse", "HEAD"
934+
).stdout.strip()
935+
if translation_sha != state["translation_sha"]:
936+
return True
937+
if cpython_sha != state["cpython_sha"]:
938+
diff = self.cpython_repo.run(
939+
"diff", "--name-only", state["cpython_sha"], cpython_sha
940+
).stdout
941+
if "Doc/" in diff:
942+
return True
943+
return False
944+
945+
def load_state(self) -> dict:
946+
state_file = self.build_root / "state.toml"
947+
try:
948+
return tomlkit.loads(state_file.read_text(encoding="UTF-8"))[
949+
f"/{self.language.tag}/{self.version.name}/"
950+
]
951+
except KeyError:
952+
return {}
953+
954+
def save_state(self, build_duration: float):
924955
"""Save current cpython sha1 and current translation sha1.
925956
926957
Using this we can deduce if a rebuild is needed or not.
@@ -939,11 +970,7 @@ def save_state(self, build_duration):
939970
).stdout.strip()
940971
state["last_build"] = dt.now(timezone.utc)
941972
state["last_build_duration"] = build_duration
942-
943-
states.setdefault("build", {}).setdefault(self.language.tag, {})[
944-
self.version.name
945-
] = state
946-
973+
states[f"/{self.language.tag}/{self.version.name}/"] = state
947974
state_file.write_text(tomlkit.dumps(states), encoding="UTF-8")
948975

949976

0 commit comments

Comments
 (0)