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