2023-07-21 14:54:09 +01:00
|
|
|
# © Crown-owned copyright 2023, Defence Science and Technology Laboratory UK
|
2023-06-07 22:40:16 +01:00
|
|
|
import filecmp
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2023-07-18 10:21:06 +01:00
|
|
|
from logging import Logger
|
2023-06-07 22:40:16 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pkg_resources
|
|
|
|
|
|
2023-07-21 14:00:50 +01:00
|
|
|
from primaite import getLogger, PRIMAITE_PATHS
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-07-18 10:21:06 +01:00
|
|
|
_LOGGER: Logger = getLogger(__name__)
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-07-14 12:01:38 +01:00
|
|
|
|
|
|
|
|
def run(overwrite_existing: bool = True) -> None:
|
2023-07-07 10:28:00 +01:00
|
|
|
"""
|
|
|
|
|
Resets the demo jupyter notebooks in the users app notebooks directory.
|
2023-06-07 22:40:16 +01:00
|
|
|
|
2023-07-04 11:11:52 +01:00
|
|
|
:param overwrite_existing: A bool to toggle replacing existing edited notebooks on or off.
|
2023-06-07 22:40:16 +01:00
|
|
|
"""
|
2023-06-30 16:52:57 +01:00
|
|
|
notebooks_package_data_root = pkg_resources.resource_filename("primaite", "notebooks/_package_data")
|
2023-06-07 22:40:16 +01:00
|
|
|
for subdir, dirs, files in os.walk(notebooks_package_data_root):
|
|
|
|
|
for file in files:
|
|
|
|
|
fp = os.path.join(subdir, file)
|
2023-06-30 16:52:57 +01:00
|
|
|
path_split = os.path.relpath(fp, notebooks_package_data_root).split(os.sep)
|
2023-07-21 14:00:50 +01:00
|
|
|
target_fp = PRIMAITE_PATHS.user_notebooks_path / Path(*path_split)
|
2023-06-07 22:40:16 +01:00
|
|
|
target_fp.parent.mkdir(exist_ok=True, parents=True)
|
|
|
|
|
copy_file = not target_fp.is_file()
|
|
|
|
|
|
|
|
|
|
if overwrite_existing and not copy_file:
|
2023-06-30 16:52:57 +01:00
|
|
|
copy_file = (not filecmp.cmp(fp, target_fp)) and (".ipynb_checkpoints" not in str(target_fp))
|
2023-06-07 22:40:16 +01:00
|
|
|
|
|
|
|
|
if copy_file:
|
|
|
|
|
shutil.copy2(fp, target_fp)
|
|
|
|
|
_LOGGER.info(f"Reset example notebook: {target_fp}")
|