Skip to content

Commit

Permalink
add tools for automatically editing timeouts
Browse files Browse the repository at this point in the history
(cherry picked from commit 3054c92)
  • Loading branch information
srulre authored and NadavClassiq committed Nov 11, 2024
1 parent 52bd04e commit 8047feb
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
Empty file added .internal/timeouts/__init__.py
Empty file.
87 changes: 87 additions & 0 deletions .internal/timeouts/add_and_sort_timeouts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
import sys
from pathlib import Path

from constants import (
DEFAULT_TIMEOUT_IPYNB,
DEFAULT_TIMEOUT_QMOD,
ROOT,
TIMEOUTS_FILE,
TIMEOUTS_PATH,
)
import yaml


def main() -> bool:
timeouts = _load_timeouts()

is_ok = True
for file_path in ROOT.rglob("*"):
if not file_path.is_file():
continue
relative_path = file_path.relative_to(ROOT)
if _can_skip(relative_path):
continue
if not _handle_file(str(relative_path), timeouts):
is_ok = False

if is_ok:
is_ok = _sort_timeouts()

return is_ok


def _can_skip(filename: Path) -> bool:
if TIMEOUTS_FILE == filename.suffix or filename.suffix not in [".ipynb", ".qmod"]:
return True
if filename.parts[0] == "functions" and len(filename.parts[0]) > 1:
if filename.parts[1] in ("function_declarations", "open_library_definitions"):
return True
return False


def _handle_file(filename: str, timeouts: dict[str, float]) -> bool:
if filename in timeouts:
return True
else:
print(f"{filename} was not found in timeouts. Adding.")

if filename.endswith(".ipynb"):
timeout = DEFAULT_TIMEOUT_IPYNB
elif filename.endswith(".qmod"):
timeout = DEFAULT_TIMEOUT_QMOD
else:
print(f"Unknown file extension for file {filename}")
return False

timeouts[filename] = timeout

with open(TIMEOUTS_PATH, "w") as f:
yaml.dump(timeouts, f, sort_keys=True)

return False


def _load_timeouts() -> dict[str, float]:
with TIMEOUTS_PATH.open("r") as fobj:
return yaml.safe_load(fobj)


def _sort_timeouts() -> bool:
with TIMEOUTS_PATH.open("r") as fobj:
timeouts = yaml.safe_load(fobj)

sorted_timeouts = dict(sorted(timeouts.items()))

if list(timeouts.keys()) != list(sorted_timeouts.keys()):
with TIMEOUTS_PATH.open("w") as f:
yaml.dump(sorted_timeouts, f, sort_keys=True)
print(f"Timeouts were not sorted. Sorted them.")
return False

return True


if __name__ == "__main__":
if not main():
sys.exit(1)
39 changes: 39 additions & 0 deletions .internal/timeouts/clean_timeouts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
import subprocess
import sys
from pathlib import Path

import yaml

from constants import ROOT, TIMEOUTS_PATH


def main() -> bool:
with TIMEOUTS_PATH.open("r") as fobj:
timeouts: dict[str, float] = yaml.safe_load(fobj)

is_ok = True

new_timeouts = timeouts.copy()

for key in timeouts:
file_path = ROOT / key
if not file_path.exists():
is_ok = False

new_timeouts.pop(key)

if not is_ok:
print(
"There exists some entries in timeouts which are long gone. removing them."
)

with open(TIMEOUTS_PATH, "w") as f:
yaml.dump(new_timeouts, f, sort_keys=True)

return is_ok


if __name__ == "__main__":
if not main():
sys.exit(1)
11 changes: 11 additions & 0 deletions .internal/timeouts/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import subprocess
from pathlib import Path

Seconds = float

DEFAULT_TIMEOUT_IPYNB: Seconds = 20
DEFAULT_TIMEOUT_QMOD: Seconds = 10

ROOT = Path(subprocess.getoutput("git rev-parse --show-toplevel")) # noqa: S605
TIMEOUTS_FILE = "timeouts.yaml"
TIMEOUTS_PATH = ROOT / "tests" / "resources" / TIMEOUTS_FILE

0 comments on commit 8047feb

Please sign in to comment.