From b87d94f3ee6690832015079aec7a1ddc0c92ac5e Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:06:52 +0100 Subject: [PATCH 1/3] Log updates to ``state.toml`` --- build_docs.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/build_docs.py b/build_docs.py index 255f45d..67bde35 100755 --- a/build_docs.py +++ b/build_docs.py @@ -610,6 +610,7 @@ def full_build(self): def run(self, http: urllib3.PoolManager) -> bool: """Build and publish a Python doc, for a language, and a version.""" start_time = perf_counter() + start_timestamp = dt.now(tz=timezone.utc).replace(microsecond=0) logging.info("Running.") try: self.cpython_repo.switch(self.version.branch_or_tag) @@ -619,7 +620,10 @@ def run(self, http: urllib3.PoolManager) -> bool: self.build_venv() self.build() self.copy_build_to_webroot(http) - self.save_state(build_duration=perf_counter() - start_time) + self.save_state( + build_start=start_timestamp, + build_duration=perf_counter() - start_time, + ) except Exception as err: logging.exception("Badly handled exception, human, please help.") if sentry_sdk: @@ -921,7 +925,7 @@ def load_state(self) -> dict: except (KeyError, FileNotFoundError): return {} - def save_state(self, build_duration: float): + def save_state(self, build_start: dt, build_duration: float): """Save current CPython sha1 and current translation sha1. Using this we can deduce if a rebuild is needed or not. @@ -932,17 +936,23 @@ def save_state(self, build_duration: float): except FileNotFoundError: states = tomlkit.document() - state = {} - state["cpython_sha"] = self.cpython_repo.run("rev-parse", "HEAD").stdout.strip() + key = f"/{self.language.tag}/{self.version.name}/" + state = { + "last_build_start": build_start, + "last_build_duration": round(build_duration, 0), + "cpython_sha": self.cpython_repo.run("rev-parse", "HEAD").stdout.strip(), + } if self.language.tag != "en": state["translation_sha"] = self.translation_repo.run( "rev-parse", "HEAD" ).stdout.strip() - state["last_build"] = dt.now(timezone.utc) - state["last_build_duration"] = build_duration - states[f"/{self.language.tag}/{self.version.name}/"] = state + states[key] = state state_file.write_text(tomlkit.dumps(states), encoding="UTF-8") + tbl = tomlkit.inline_table() + tbl |= state + logging.info("Saved new rebuild state for %s: %s", key, tbl.as_string()) + def symlink( www_root: Path, From 33c527278f953ff60348ab8fb16e4415edaae8f1 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Wed, 25 Sep 2024 22:13:57 +0100 Subject: [PATCH 2/3] Update build_docs.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- build_docs.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build_docs.py b/build_docs.py index 67bde35..5e9eb6b 100755 --- a/build_docs.py +++ b/build_docs.py @@ -949,9 +949,9 @@ def save_state(self, build_start: dt, build_duration: float): states[key] = state state_file.write_text(tomlkit.dumps(states), encoding="UTF-8") - tbl = tomlkit.inline_table() - tbl |= state - logging.info("Saved new rebuild state for %s: %s", key, tbl.as_string()) + table = tomlkit.inline_table() + table |= state + logging.info("Saved new rebuild state for %s: %s", key, table.as_string()) def symlink( From 094e52f3cf51e7c97f84b8cf9cac738334108bca Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:40:27 +0100 Subject: [PATCH 3/3] Include trigger reason --- build_docs.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/build_docs.py b/build_docs.py index 5e9eb6b..58836e4 100755 --- a/build_docs.py +++ b/build_docs.py @@ -616,13 +616,14 @@ def run(self, http: urllib3.PoolManager) -> bool: self.cpython_repo.switch(self.version.branch_or_tag) if self.language.tag != "en": self.clone_translation() - if self.should_rebuild(): + if trigger_reason := self.should_rebuild(): self.build_venv() self.build() self.copy_build_to_webroot(http) self.save_state( build_start=start_timestamp, build_duration=perf_counter() - start_time, + trigger=trigger_reason, ) except Exception as err: logging.exception("Badly handled exception, human, please help.") @@ -889,7 +890,7 @@ def should_rebuild(self): state = self.load_state() if not state: logging.info("Should rebuild: no previous state found.") - return True + return "no previous state" cpython_sha = self.cpython_repo.run("rev-parse", "HEAD").stdout.strip() if self.language.tag != "en": translation_sha = self.translation_repo.run( @@ -901,7 +902,7 @@ def should_rebuild(self): state["translation_sha"], translation_sha, ) - return True + return "new translations" if cpython_sha != state["cpython_sha"]: diff = self.cpython_repo.run( "diff", "--name-only", state["cpython_sha"], cpython_sha @@ -912,7 +913,7 @@ def should_rebuild(self): state["cpython_sha"], cpython_sha, ) - return True + return "Doc/ has changed" logging.info("Nothing changed, no rebuild needed.") return False @@ -925,7 +926,7 @@ def load_state(self) -> dict: except (KeyError, FileNotFoundError): return {} - def save_state(self, build_start: dt, build_duration: float): + def save_state(self, build_start: dt, build_duration: float, trigger: str): """Save current CPython sha1 and current translation sha1. Using this we can deduce if a rebuild is needed or not. @@ -940,6 +941,7 @@ def save_state(self, build_start: dt, build_duration: float): state = { "last_build_start": build_start, "last_build_duration": round(build_duration, 0), + "triggered_by": trigger, "cpython_sha": self.cpython_repo.run("rev-parse", "HEAD").stdout.strip(), } if self.language.tag != "en":