Skip to content

Commit

Permalink
Remove generated files from git
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj committed Jan 22, 2025
1 parent a5cd8f6 commit 103a8c7
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 1,059 deletions.
167 changes: 167 additions & 0 deletions test/website/test_process_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
sys.path.append(str(Path(__file__).resolve().parent.parent.parent / "website"))
from process_notebooks import (
add_authors_and_social_img_to_blog_posts,
add_front_matter_to_metadata_mdx,
cleanup_tmp_dirs_if_no_metadata,
convert_callout_blocks,
ensure_mint_json_exists,
extract_example_group,
Expand All @@ -34,6 +36,171 @@ def test_ensure_mint_json():
ensure_mint_json_exists(tmp_path) # Should not raise any exception


def test_cleanup_tmp_dirs_if_no_metadata():
# Test without the tmp_dir / "snippets" / "data" / "NotebooksMetadata.mdx"
# the tmp_dir / "notebooks" should be removed.
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)
notebooks_dir = tmp_path / "notebooks"
notebooks_dir.mkdir(parents=True, exist_ok=True)
(notebooks_dir / "example-1.mdx").touch()
(notebooks_dir / "example-2.mdx").touch()

cleanup_tmp_dirs_if_no_metadata(tmp_path)
assert not notebooks_dir.exists()

# Test with the tmp_dir / "snippets" / "data" / "NotebooksMetadata.mdx"
# the tmp_dir / "notebooks" should not be removed.
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)

notebooks_dir = tmp_path / "notebooks"
notebooks_dir.mkdir(parents=True, exist_ok=True)
(notebooks_dir / "example-1.mdx").touch()
(notebooks_dir / "example-2.mdx").touch()

metadata_dir = tmp_path / "snippets" / "data"
metadata_dir.mkdir(parents=True, exist_ok=True)
(metadata_dir / "NotebooksMetadata.mdx").touch()

cleanup_tmp_dirs_if_no_metadata(tmp_path)
assert notebooks_dir.exists()


class TestAddFrontMatterToMetadataMdx:
def test_without_metadata_mdx(self):
front_matter_dict = {
"title": "some title",
"link": "/notebooks/some-title",
"description": "some description",
"image": "some image",
"tags": ["tag1", "tag2"],
"source_notebook": "/notebook/some-title.ipynb",
}
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)

metadata_dir = tmp_path / "snippets" / "data"
metadata_dir.mkdir(parents=True, exist_ok=True)

rendered_mdx = tmp_path / "source"
rendered_mdx.mkdir(parents=True, exist_ok=True)
(rendered_mdx / "some-title.mdx").touch()

# when the metadata file is not present, the file should be created and the front matter should be added
add_front_matter_to_metadata_mdx(front_matter_dict, tmp_path, rendered_mdx)

assert (metadata_dir / "NotebooksMetadata.mdx").exists()

with open(metadata_dir / "NotebooksMetadata.mdx", "r") as f:
actual = f.read()

assert (
actual
== """{/*
Auto-generated file - DO NOT EDIT
Please edit the add_front_matter_to_metadata_mdx function in process_notebooks.py
*/}
export const notebooksMetadata = [
{
"title": "some title",
"link": "/notebooks/source",
"description": "some description",
"image": "some image",
"tags": [
"tag1",
"tag2"
],
"source": "/notebook/some-title.ipynb"
}
];
"""
)

def test_with_metadata_mdx(self):
front_matter_dict = {
"title": "some title",
"link": "/notebooks/some-title",
"description": "some description",
"image": "some image",
"tags": ["tag1", "tag2"],
"source_notebook": "/notebook/some-title.ipynb",
}
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)

metadata_dir = tmp_path / "snippets" / "data"
metadata_dir.mkdir(parents=True, exist_ok=True)
metadata_content = """{/*
Auto-generated file - DO NOT EDIT
Please edit the add_front_matter_to_metadata_mdx function in process_notebooks.py
*/}
export const notebooksMetadata = [
{
"title": "some other title",
"link": "/notebooks/some-other-title",
"description": "some other description",
"image": "some other image",
"tags": [
"tag3",
"tag4"
],
"source": "/notebook/some-other-title.ipynb"
}
];
"""
with open(metadata_dir / "NotebooksMetadata.mdx", "w") as f:
f.write(metadata_content)

rendered_mdx = tmp_path / "source"
rendered_mdx.mkdir(parents=True, exist_ok=True)
(rendered_mdx / "some-title.mdx").touch()

# when the metadata file is present, the front matter should be added to the existing metadata
add_front_matter_to_metadata_mdx(front_matter_dict, tmp_path, rendered_mdx)

assert (metadata_dir / "NotebooksMetadata.mdx").exists()

with open(metadata_dir / "NotebooksMetadata.mdx", "r") as f:
actual = f.read()

assert (
actual
== """{/*
Auto-generated file - DO NOT EDIT
Please edit the add_front_matter_to_metadata_mdx function in process_notebooks.py
*/}
export const notebooksMetadata = [
{
"title": "some other title",
"link": "/notebooks/some-other-title",
"description": "some other description",
"image": "some other image",
"tags": [
"tag3",
"tag4"
],
"source": "/notebook/some-other-title.ipynb"
},
{
"title": "some title",
"link": "/notebooks/source",
"description": "some description",
"image": "some image",
"tags": [
"tag1",
"tag2"
],
"source": "/notebook/some-title.ipynb"
}
];
"""
)


class TestAddBlogsToNavigation:
@pytest.fixture
def test_dir(self):
Expand Down
1 change: 1 addition & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ docs/reference
/notebooks
/blog
mint.json
/snippets/data/NotebooksMetadata.mdx

docs/tutorial/*.mdx
docs/tutorial/**/*.png
Expand Down
38 changes: 30 additions & 8 deletions website/process_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,21 @@ def add_front_matter_to_metadata_mdx(

metadata_mdx = website_dir / "snippets" / "data" / "NotebooksMetadata.mdx"

if not metadata_mdx.exists():
with open(metadata_mdx, "w", encoding="utf-8") as f:
f.write(
"{/*\nAuto-generated file - DO NOT EDIT\nPlease edit the add_front_matter_to_metadata_mdx function in process_notebooks.py\n*/}\n\n"
)
f.write("export const notebooksMetadata = [];\n")

metadata = []
if metadata_mdx.exists():
with open(metadata_mdx, encoding="utf-8") as f:
content = f.read()
if content:
start = content.find("export const notebooksMetadata = [")
end = content.rfind("]")
if start != -1 and end != -1:
metadata = json.loads(content[start + 32 : end + 1])
with open(metadata_mdx, encoding="utf-8") as f:
content = f.read()
if content:
start = content.find("export const notebooksMetadata = [")
end = content.rfind("]")
if start != -1 and end != -1:
metadata = json.loads(content[start + 32 : end + 1])

# Create new entry for current notebook
entry = {
Expand Down Expand Up @@ -899,6 +905,21 @@ def ensure_mint_json_exists(website_dir: Path) -> None:
sys.exit(1)


def cleanup_tmp_dirs_if_no_metadata(website_dir: Path) -> None:
"""Remove the temporary notebooks directory if NotebooksMetadata.mdx is not found.
This is to ensure a clean build and generate the metadata file as well as to
update the navigation with correct entries.
"""
metadata_mdx = website_dir / "snippets" / "data" / "NotebooksMetadata.mdx"
if not metadata_mdx.exists():
print(f"NotebooksMetadata.mdx not found at {metadata_mdx}")

notebooks_dir = website_dir / "notebooks"
print(f"Removing the {notebooks_dir} and to ensure a clean build.")
shutil.rmtree(notebooks_dir, ignore_errors=True)


def main() -> None:
script_dir = Path(__file__).parent.absolute()
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -929,6 +950,7 @@ def main() -> None:
sys.exit(1)

ensure_mint_json_exists(args.website_directory)
cleanup_tmp_dirs_if_no_metadata(args.website_directory)

if args.notebooks:
collected_notebooks = args.notebooks
Expand Down
Loading

0 comments on commit 103a8c7

Please sign in to comment.