Skip to content

Commit

Permalink
refactor(file-manager): remove unused or trivial methods from `FileMa…
Browse files Browse the repository at this point in the history
…nager`
  • Loading branch information
laurent-laporte-pro committed Sep 22, 2023
1 parent 88efc98 commit fbb60e0
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 161 deletions.
32 changes: 0 additions & 32 deletions antareslauncher/file_manager/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ def listdir_of(self, directory):
list_dir.sort()
return list_dir

@staticmethod
def is_dir(dir_path: Path):
return Path(dir_path).is_dir()

def _get_list_dir_without_subdir(self, dir_path, subdir_to_exclude):
"""Make a list of all the folders inside a directory except one
Expand Down Expand Up @@ -118,34 +114,6 @@ def zip_dir_excluding_subdir(self, dir_path, zipfile_path, subdir_to_exclude):
)
return Path(zipfile_path).is_file()

def unzip(self, file_path: str):
"""Unzips the result of the antares job once is has been downloaded
Args:
file_path: The path to the file
Returns:
True if the file has been extracted, False otherwise
"""
self.logger.info(f"Unzipping {file_path}")
try:
with zipfile.ZipFile(file=file_path) as zip_file:
progress_bar = self.display.generate_progress_bar(
zip_file.namelist(),
desc="Extracting archive:",
total=len(zip_file.namelist()),
)
for file in progress_bar:
zip_file.extract(
member=file,
path=Path(file_path).parent,
)
return True
except ValueError:
return False
except FileNotFoundError:
return False

def make_dir(self, directory_name):
self.logger.info(f"Creating directory {directory_name}")
os.makedirs(directory_name, exist_ok=True)
Expand Down
1 change: 0 additions & 1 deletion antareslauncher/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def run_with(
)
study_list_composer = StudyListComposer(
repo=data_repo,
file_manager=file_manager,
display=display,
parameters=StudyListComposerParameters(
studies_in_dir=arguments.studies_in,
Expand Down
12 changes: 4 additions & 8 deletions antareslauncher/use_cases/create_list/study_list_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ class StudyListComposer:
def __init__(
self,
repo: DataRepoTinydb,
file_manager: FileManager,
display: DisplayTerminal,
parameters: StudyListComposerParameters,
):
self._repo = repo
self._file_manager = file_manager
self._display = display
self._studies_in_dir = parameters.studies_in_dir
self.time_limit = parameters.time_limit
Expand Down Expand Up @@ -116,10 +114,9 @@ def update_study_database(self):

self._new_study_added = False

directories = self._file_manager.listdir_of(self._studies_in_dir)
for directory in directories:
directory_path = Path(self._studies_in_dir) / Path(directory)
if self._file_manager.is_dir(directory_path):
directories = Path(self._studies_in_dir).iterdir()
for directory_path in sorted(directories):
if directory_path.is_dir():
self._update_database_with_directory(directory_path)

if not self._new_study_added:
Expand All @@ -136,8 +133,7 @@ def _update_database_with_new_study(
)
self._update_database_with_study(buffer_study)

def _update_database_with_directory(self, directory_path: t.Union[str, Path]):
directory_path = Path(directory_path)
def _update_database_with_directory(self, directory_path: Path):
solver_version = get_solver_version(directory_path)
antares_version = self.antares_version or solver_version
if not antares_version:
Expand Down
47 changes: 0 additions & 47 deletions tests/integration/test_integration_study_list_composer.py

This file was deleted.

60 changes: 60 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import shutil
from pathlib import Path
from unittest import mock

import pytest
from antareslauncher.data_repo.data_repo_tinydb import DataRepoTinydb
from antareslauncher.display.display_terminal import DisplayTerminal
from antareslauncher.use_cases.create_list.study_list_composer import (
StudyListComposer,
StudyListComposerParameters,
)
from tests.unit.assets import ASSETS_DIR


@pytest.fixture(name="studies_in_dir")
def studies_in_dir_fixture(tmp_path: Path) -> str:
studies_in_dir = tmp_path.joinpath("STUDIES-IN")
assets_dir = ASSETS_DIR.joinpath("study_list_composer/studies")
shutil.copytree(assets_dir, studies_in_dir)
return str(studies_in_dir)


@pytest.fixture(name="repo")
def repo_fixture(tmp_path: Path) -> DataRepoTinydb:
return DataRepoTinydb(
database_file_path=tmp_path.joinpath("repo.json"),
db_primary_key="name",
)


@pytest.fixture(name="study_list_composer")
def study_list_composer_fixture(
tmp_path: Path,
repo: DataRepoTinydb,
studies_in_dir: str,
) -> StudyListComposer:
display = mock.Mock(spec=DisplayTerminal)
composer = StudyListComposer(
repo=repo,
display=display,
parameters=StudyListComposerParameters(
studies_in_dir=studies_in_dir,
time_limit=42,
n_cpu=24,
log_dir=str(tmp_path.joinpath("LOGS")),
xpansion_mode=None,
output_dir=str(tmp_path.joinpath("FINISHED")),
post_processing=False,
antares_versions_on_remote_server=[
"800",
"810",
"820",
"830",
"840",
"850",
],
other_options="",
),
)
return composer
17 changes: 0 additions & 17 deletions tests/unit/test_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from antareslauncher.display.display_terminal import DisplayTerminal
from antareslauncher.file_manager import file_manager
from antareslauncher.study_dto import StudyDTO
from tests.data import DATA_DIR

DIR_TO_ZIP = DATA_DIR / "file-manager-test" / "to-zip"
Expand Down Expand Up @@ -48,22 +47,6 @@ def test_golden_master_for_zip_study_excluding_output_dir(self, tmp_path):
assert result_zip_file.is_file()
result_zip_file.unlink()

@pytest.mark.unit_test
def test_unzip(self):
"""
Tests the scenario where the specified zip file does not exist. The expected outcome is
that the function returns False, indicating that the zip file could not be unzipped.
This test is checking that the function correctly handles the case where the input file is not present.
"""
# given
study = StudyDTO("path")
display_terminal = DisplayTerminal()
my_file_manager = file_manager.FileManager(display_terminal)
# when
output = my_file_manager.unzip(study.local_final_zipfile_path)
# then
assert output is False

@pytest.mark.unit_test
def test__get_list_dir_without_subdir(self):
"""
Expand Down
56 changes: 0 additions & 56 deletions tests/unit/test_study_list_composer.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import shutil
from pathlib import Path
from unittest import mock

import pytest

from antareslauncher.data_repo.data_repo_tinydb import DataRepoTinydb
from antareslauncher.display.display_terminal import DisplayTerminal
from antareslauncher.file_manager.file_manager import FileManager
from antareslauncher.use_cases.create_list.study_list_composer import (
StudyListComposer,
StudyListComposerParameters,
get_solver_version,
)
from tests.unit.assets import ASSETS_DIR

CONFIG_NOMINAL_VERSION = """\
[antares]
Expand Down Expand Up @@ -73,55 +66,6 @@ def test_get_solver_version(
assert actual == expected


@pytest.fixture(name="studies_in_dir")
def studies_in_dir_fixture(tmp_path: Path) -> str:
studies_in_dir = tmp_path.joinpath("STUDIES-IN")
assets_dir = ASSETS_DIR.joinpath("study_list_composer/studies")
shutil.copytree(assets_dir, studies_in_dir)
return str(studies_in_dir)


@pytest.fixture(name="repo")
def repo_fixture(tmp_path: Path) -> DataRepoTinydb:
return DataRepoTinydb(
database_file_path=tmp_path.joinpath("repo.json"),
db_primary_key="name",
)


@pytest.fixture(name="study_list_composer")
def study_list_composer_fixture(
tmp_path: Path,
repo: DataRepoTinydb,
studies_in_dir: str,
) -> StudyListComposer:
display = mock.Mock(spec=DisplayTerminal)
composer = StudyListComposer(
repo=repo,
file_manager=FileManager(display_terminal=display),
display=display,
parameters=StudyListComposerParameters(
studies_in_dir=studies_in_dir,
time_limit=42,
n_cpu=24,
log_dir=str(tmp_path.joinpath("LOGS")),
xpansion_mode=None,
output_dir=str(tmp_path.joinpath("FINISHED")),
post_processing=False,
antares_versions_on_remote_server=[
"800",
"810",
"820",
"830",
"840",
"850",
],
other_options="",
),
)
return composer


class TestStudyListComposer:
@pytest.mark.parametrize("xpansion_mode", ["r", "cpp", ""])
def test_update_study_database__xpansion_mode(
Expand Down

0 comments on commit fbb60e0

Please sign in to comment.