Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .internal/update_outputs/fake_browser.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
then, removes them after the execution
"""

import json
import sys
from typing import Callable

from notebook_utils import read_notebook, write_notebook

RANDOM_SEED = 8

INJECTED_CELLS = [
Expand Down Expand Up @@ -40,19 +41,6 @@
NBFORMAT_VERSION_WITH_CELL_ID = (4, 5)


def _read_notebook(path: str) -> dict:
with open(path, encoding="utf-8") as f:
return json.load(f)


def _write_notebook(path: str, notebook: dict) -> None:
# the same format as `nbformat.write` - unescaped unicode, and a trailing newline
# so that rewriting a notebook changes only what we've edited
with open(path, "w", encoding="utf-8") as f:
json.dump(notebook, f, indent=1, ensure_ascii=False)
f.write("\n")


def _make_cell(index: int, source: str, with_id: bool) -> dict:
cell = {
"cell_type": "code",
Expand All @@ -67,7 +55,7 @@ def _make_cell(index: int, source: str, with_id: bool) -> dict:


def add_cells(notebook_path: str) -> None:
notebook = _read_notebook(notebook_path)
notebook = read_notebook(notebook_path)

nbformat_version = notebook["nbformat"], notebook["nbformat_minor"]
with_id = nbformat_version >= NBFORMAT_VERSION_WITH_CELL_ID
Expand All @@ -77,12 +65,12 @@ def add_cells(notebook_path: str) -> None:
]

notebook["cells"][:0] = cells
_write_notebook(notebook_path, notebook)
write_notebook(notebook_path, notebook)


# does nothing if the cells aren't there, so it's safe to call more than once
def remove_cells(notebook_path: str) -> None:
notebook = _read_notebook(notebook_path)
notebook = read_notebook(notebook_path)

cells = notebook["cells"]
injected = cells[: len(INJECTED_CELLS)]
Expand All @@ -97,7 +85,7 @@ def remove_cells(notebook_path: str) -> None:
if type(cell.get("execution_count")) is int:
cell["execution_count"] -= num_executed

_write_notebook(notebook_path, notebook)
write_notebook(notebook_path, notebook)


ACTIONS: dict[str, Callable[[str], None]] = {
Expand Down
14 changes: 14 additions & 0 deletions .internal/update_outputs/patch_notebook/notebook_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json


def read_notebook(path: str) -> dict:
with open(path, encoding="utf-8") as f:
return json.load(f)


def write_notebook(path: str, notebook: dict) -> None:
# the same format as `nbformat.write` - unescaped unicode, and a trailing newline
# so that rewriting a notebook changes only what we've edited
with open(path, "w", encoding="utf-8") as f:
json.dump(notebook, f, indent=1, ensure_ascii=False)
f.write("\n")
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#!/usr/bin/env python3
"""
usage: update_links.py <notebook to update> <notebook to take the links from>

copies the platform links into the outputs of a notebook, keeping the rest of its outputs as they are
"""

import json
import re
import sys

from notebook_utils import read_notebook, write_notebook

LINK_PATTERN = re.compile(
r"Quantum program link: https://platform\.classiq\.io/circuit/\S+\n"
)
Expand All @@ -29,6 +35,26 @@ def extract_links(cells: list[dict]) -> list[Link]:
return links


def validate_links(old_links: list[Link], new_links: list[Link]) -> bool:
if len(old_links) != len(new_links):
print(
f"Error: the notebook printed {len(new_links)} links, but its outputs have {len(old_links)}",
file=sys.stderr,
)
return False

old_cell_indices = [cell_index for cell_index, _, _ in old_links]
new_cell_indices = [cell_index for cell_index, _, _ in new_links]
if old_cell_indices != new_cell_indices:
print(
"Error: the notebook printed its links in other cells than in its outputs",
file=sys.stderr,
)
return False

return True


def update_links(
cells: list[dict], old_links: list[Link], new_links: list[Link]
) -> bool:
Expand All @@ -42,68 +68,38 @@ def update_links(
text[index] = line.replace(old_link, new_link)
break
else:
print("Couldn't find link. This shouldn't happen.")
print(
"Error: couldn't find a link. this shouldn't happen.", file=sys.stderr
)
return False
return True


def validate_links_list(
old_links: list[Link], new_links: list[Link], force: bool
) -> bool:
# verify list length
if len(old_links) != len(new_links):
print("Error: Link count mismatch in force mode")
return False

if force:
# verify cell index
old_indices = [i for i, _ in old_links]
new_indices = [i for i, _ in new_links]
if old_indices != new_indices:
print("Error: Link cell indices mismatch")
return False

# verify link pattern
if not all(LINK_PATTERN.match(link) for _, link in new_links):
print("Error: Not all new links match expected pattern")
return False

return True


def main(dest_file: str, source_file: str, force: bool = False) -> bool:
with open(dest_file) as f:
old_nb = json.load(f)
with open(source_file) as f:
new_nb = json.load(f)
def main(dest_file: str, source_file: str) -> bool:
old_nb = read_notebook(dest_file)
new_nb = read_notebook(source_file)

old_links = extract_links(old_nb["cells"])
new_links = extract_links(new_nb["cells"])

if not validate_links_list(old_links, new_links, force):
if not validate_links(old_links, new_links):
return False

if not update_links(old_nb["cells"], old_links, new_links):
return False

with open(dest_file, "w") as f:
json.dump(old_nb, f, indent=1)
write_notebook(dest_file, old_nb)

print(f"Updated notebook written to: {dest_file}")
return True


if __name__ == "__main__":
force = False
if "--force" in sys.argv:
force = True
sys.argv.remove("--force")

if len(sys.argv) != 3:
print(
f"Usage: {sys.argv[0]} <file in which we'll update the links> <file from which to take the links> [--force]"
f"usage: {sys.argv[0]} <notebook to update> <notebook to take the links from>",
file=sys.stderr,
)
sys.exit(1)

if not main(sys.argv[1], sys.argv[2], force):
sys.exit(1)
sys.exit(not main(sys.argv[1], sys.argv[2]))
Loading
Loading