|
| 1 | +"""Check the frequency of the rebuild loop. |
| 2 | +
|
| 3 | +This must be run in a directory that has the ``docsbuild.log*`` files. |
| 4 | +For example: |
| 5 | +
|
| 6 | +.. code-block:: bash |
| 7 | +
|
| 8 | + $ scp "[email protected]:/var/log/docsbuild/docsbuild.log*" docsbuild-logs |
| 9 | + $ python check_times.py |
| 10 | +""" |
| 11 | + |
| 12 | +import datetime as dt |
| 13 | +import gzip |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +from build_docs import format_seconds |
| 17 | + |
| 18 | + |
| 19 | +def get_lines() -> list[str]: |
| 20 | + lines = [] |
| 21 | + zipped_logs = list(Path.cwd().glob("docsbuild.log.*.gz")) |
| 22 | + zipped_logs.sort(key=lambda p: int(p.name.split(".")[-2]), reverse=True) |
| 23 | + for logfile in zipped_logs: |
| 24 | + with gzip.open(logfile, "rt", encoding="utf-8") as f: |
| 25 | + lines += f.readlines() |
| 26 | + with open("docsbuild.log", encoding="utf-8") as f: |
| 27 | + lines += f.readlines() |
| 28 | + return lines |
| 29 | + |
| 30 | + |
| 31 | +def calc_time(lines: list[str]) -> None: |
| 32 | + start = end = language = version = start_timestamp = None |
| 33 | + reason = lang_ver = "" |
| 34 | + |
| 35 | + print("Start | Version | Language | Build | Trigger") |
| 36 | + print(":-- | :--: | :--: | --: | :--:") |
| 37 | + |
| 38 | + for line in lines: |
| 39 | + line = line.strip() |
| 40 | + |
| 41 | + if ": Should rebuild: " in line: |
| 42 | + if "no previous state found" in line: |
| 43 | + reason = "brand new" |
| 44 | + elif "new translations" in line: |
| 45 | + reason = "translation" |
| 46 | + elif "Doc/ has changed" in line: |
| 47 | + reason = "docs" |
| 48 | + else: |
| 49 | + reason = "" |
| 50 | + lang_ver = line.split(" ")[3].removesuffix(":") |
| 51 | + |
| 52 | + if line.endswith("Build start."): |
| 53 | + timestamp = line[:23].replace(",", ".") |
| 54 | + language, version = line.split(" ")[3].removesuffix(":").split("/") |
| 55 | + start = dt.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f") |
| 56 | + start_timestamp = f"{line[:16]} UTC" |
| 57 | + |
| 58 | + if start and ": Build done " in line: |
| 59 | + timestamp = line[:23].replace(",", ".") |
| 60 | + language, version = line.split(" ")[3].removesuffix(":").split("/") |
| 61 | + end = dt.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f") |
| 62 | + |
| 63 | + if start and end: |
| 64 | + duration = (end - start).total_seconds() |
| 65 | + fmt_duration = format_seconds(duration) |
| 66 | + if lang_ver != f"{language}/{version}": |
| 67 | + reason = "" |
| 68 | + print( |
| 69 | + f"{start_timestamp: <20} | {version: <7} | {language: <8} | {fmt_duration :<14} | {reason}" |
| 70 | + ) |
| 71 | + start = end = start_timestamp = None |
| 72 | + |
| 73 | + if ": Full build done" in line: |
| 74 | + timestamp = f"{line[:16]} UTC" |
| 75 | + _, fmt_duration = line.removesuffix(").").split("(") |
| 76 | + print( |
| 77 | + f"{timestamp: <20} | --FULL- | -BUILD-- | {fmt_duration :<14} | -----------" |
| 78 | + ) |
| 79 | + |
| 80 | + if start and end is None: |
| 81 | + print( |
| 82 | + f"{start_timestamp: <20} | {version: <7} | {language: <8} | In progress... | {reason}" |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + calc_time(get_lines()) |
0 commit comments