diff --git a/build_docs.py b/build_docs.py index 255f45d..58836e4 100755 --- a/build_docs.py +++ b/build_docs.py @@ -610,16 +610,21 @@ 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) 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_duration=perf_counter() - start_time) + 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.") if sentry_sdk: @@ -885,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( @@ -897,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 @@ -908,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 @@ -921,7 +926,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, trigger: str): """Save current CPython sha1 and current translation sha1. Using this we can deduce if a rebuild is needed or not. @@ -932,17 +937,24 @@ 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), + "triggered_by": trigger, + "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") + table = tomlkit.inline_table() + table |= state + logging.info("Saved new rebuild state for %s: %s", key, table.as_string()) + def symlink( www_root: Path,