Skip to content

Commit

Permalink
add method to remove datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
lobis committed Dec 6, 2023
1 parent 224d479 commit 897f824
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/geant4_python_application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


def _setup_manager(self, *args, **kwargs):
install_datasets()
install_datasets(show_progress=False)
return self._setup_manager(*args, **kwargs)


Expand Down
21 changes: 18 additions & 3 deletions src/geant4_python_application/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import concurrent.futures
import hashlib
import os
import shutil
import tarfile
import tempfile
from collections import namedtuple
Expand Down Expand Up @@ -97,7 +98,9 @@
)


def _download_extract_dataset(dataset: Dataset, progress_bar_position: int = 0):
def _download_extract_dataset(
dataset: Dataset, progress_bar_position: int = 0, show_progress: bool = True
):
filename = dataset.filename
urlpath = f"{url}/{filename}.{dataset.version}.tar.gz"
r = requests.get(urlpath, stream=True)
Expand All @@ -112,6 +115,7 @@ def _download_extract_dataset(dataset: Dataset, progress_bar_position: int = 0):
unit_scale=True,
desc=f"Downloading and Extracting {filename}",
position=progress_bar_position,
disable=not show_progress,
) as pbar:
for chunk in r.iter_content(chunk_size=chunk_size):
f.write(chunk)
Expand All @@ -130,7 +134,7 @@ def _download_extract_dataset(dataset: Dataset, progress_bar_position: int = 0):
pbar.update(total_size)


def install_datasets(force: bool = False):
def install_datasets(force: bool = False, show_progress: bool = True):
os.makedirs(data_dir, exist_ok=True)

os.environ["GEANT4_DATA_DIR"] = data_dir
Expand All @@ -148,11 +152,22 @@ def install_datasets(force: bool = False):
max_workers=len(datasets_to_download)
) as executor:
futures = [
executor.submit(_download_extract_dataset, dataset, i)
executor.submit(_download_extract_dataset, dataset, i, show_progress)
for i, dataset in enumerate(datasets_to_download)
]
concurrent.futures.wait(futures)


def reinstall_datasets():
install_datasets(force=True)


def uninstall_datasets():
dir_to_remove = os.path.dirname(data_dir)
package_dir = os.path.dirname(__file__)

if not os.path.relpath(package_dir, dir_to_remove).startswith(".."):
raise RuntimeError(
f"Refusing to remove {dir_to_remove} because it is not a subdirectory of {package_dir}"
)
shutil.rmtree(dir_to_remove, ignore_errors=True)

0 comments on commit 897f824

Please sign in to comment.