Files
PrimAITE/src/primaite/setup/reset_demo_notebooks.py

53 lines
1.7 KiB
Python
Raw Normal View History

2025-01-02 15:05:06 +00:00
# © Crown-owned copyright 2025, Defence Science and Technology Laboratory UK
2025-03-13 14:57:24 +00:00
"""Clear the user data directory of example notebooks and copy fresh copies in."""
import filecmp
import shutil
from logging import Logger
from pathlib import Path
from primaite import getLogger, PRIMAITE_PATHS
_LOGGER: Logger = getLogger(__name__)
2023-07-14 12:01:38 +01:00
def should_copy_file(src: Path, dest: Path, overwrite_existing: bool) -> bool:
"""
Determine if the file should be copied.
:param src: The source file Path.
:param dest: The destination file Path.
:param overwrite_existing: A bool to toggle replacing existing edited files on or off.
:return: True if file should be copied, otherwise False.
"""
if not dest.is_file():
return True
if overwrite_existing and not filecmp.cmp(src, dest):
return True
return False
2023-07-14 12:01:38 +01:00
def run(overwrite_existing: bool = True) -> None:
"""
Resets the demo Jupyter notebooks in the user's app notebooks directory.
2023-07-04 11:11:52 +01:00
:param overwrite_existing: A bool to toggle replacing existing edited notebooks on or off.
"""
primaite_root = Path(__file__).parent.parent
example_notebooks_user_dir = PRIMAITE_PATHS.user_notebooks_path / "example_notebooks"
example_notebooks_user_dir.mkdir(exist_ok=True, parents=True)
for src_fp in primaite_root.glob("**/*.ipynb"):
dst_fp = example_notebooks_user_dir / src_fp.name
if should_copy_file(src_fp, dst_fp, overwrite_existing):
print(dst_fp)
shutil.copy2(src_fp, dst_fp)
_LOGGER.info(f"Reset example notebook: {dst_fp}")
2024-01-25 14:52:48 +00:00
src = primaite_root / "notebooks/_package_data/"
dst = example_notebooks_user_dir / "_package_data/"
shutil.copytree(src, dst, dirs_exist_ok=True)