Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 12.1 backport #9636

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/ert/analysis/misfit_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ def get_scaling_factor(nr_observations: int, nr_components: int) -> float:
nr_components is the number of primary components from PCA analysis
below a user threshold
"""
logger.info(
f"Calculation scaling factor, nr of primary components: "
f"{nr_components}, number of observations: {nr_observations}"
)
if nr_components == 0:
nr_components = 1
logger.warning(
Expand Down Expand Up @@ -160,4 +156,5 @@ def main(
scale_factor = get_scaling_factor(len(index), components)
nr_components[index] *= components
scale_factors[index] *= scale_factor
logger.info(f"Calculated scaling factors for {len(scale_factors)} clusters")
return scale_factors, clusters, nr_components
4 changes: 2 additions & 2 deletions src/ert/gui/tools/plot/plottery/plots/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def plotHistogram(
else:
current_min = data[ensemble.name].min()
current_max = data[ensemble.name].max()
minimum = current_min if minimum is None else min(minimum, current_min) # type: ignore
maximum = current_max if maximum is None else max(maximum, current_max) # type: ignore
minimum = current_min if minimum is None else min(minimum, current_min)
maximum = current_max if maximum is None else max(maximum, current_max)
max_element_count = max(max_element_count, len(data[ensemble.name].index))

bin_count = ceil(sqrt(max_element_count))
Expand Down
23 changes: 17 additions & 6 deletions src/ert/resources/shell_scripts/copy_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@ def copy_directory(src_path: str, target_path: str) -> None:
print(f"Copying directory structure {src_path} -> {target_path}")
if os.path.isdir(target_path):
target_path = os.path.join(target_path, src_basename)
shutil.copytree(src_path, target_path, dirs_exist_ok=True)
try:
shutil.copytree(src_path, target_path, dirs_exist_ok=True)
except shutil.Error as err:
# Check for shutil bug in Python <3.14:
if len(err.args[0]) > 10 and {
len(somestring) for somestring in err.args[0]
} == {1}:
# https://github.com/python/cpython/issues/102931
# This can only occur when the shutil.Error is a single error
raise OSError("".join(err.args[0])) from err
else:
raise OSError(
", ".join([err_arg[2] for err_arg in err.args[0]])
) from err
else:
raise OSError(
f"Input argument:'{src_path}' "
f"Input argument: '{src_path}' "
"does not correspond to an existing directory"
)

Expand All @@ -31,7 +44,5 @@ def copy_directory(src_path: str, target_path: str) -> None:
target_path = sys.argv[2]
try:
copy_directory(src_path, target_path)
except OSError as e:
sys.exit(
f"COPY_DIRECTORY failed with the following error: {''.join(e.args[0])}"
)
except OSError as oserror:
sys.exit(f"COPY_DIRECTORY failed with the following error(s): {oserror}")
8 changes: 5 additions & 3 deletions src/everest/detached/jobs/everserver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import datetime
import json
import logging
import os
Expand All @@ -7,7 +8,6 @@
import threading
import traceback
from base64 import b64encode
from datetime import datetime, timedelta
from functools import partial
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -439,8 +439,10 @@ def _generate_certificate(cert_folder: str):
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.utcnow())
.not_valid_after(datetime.utcnow() + timedelta(days=365)) # 1 year
.not_valid_before(datetime.datetime.now(datetime.UTC))
.not_valid_after(
datetime.datetime.now(datetime.UTC) + datetime.timedelta(days=365)
) # 1 year
.add_extension(
x509.SubjectAlternativeName([x509.DNSName(f"{cert_name}")]),
critical=False,
Expand Down
4 changes: 3 additions & 1 deletion src/everest/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def warn_user_that_runpath_is_nonempty() -> None:

def _roll_dir(old_name):
old_name = os.path.realpath(old_name)
new_name = old_name + datetime.datetime.utcnow().strftime("__%Y-%m-%d_%H.%M.%S.%f")
new_name = old_name + datetime.datetime.now(datetime.UTC).strftime(
"__%Y-%m-%d_%H.%M.%S.%f"
)
os.rename(old_name, new_name)
logging.getLogger(EVEREST).info(f"renamed {old_name} to {new_name}")

Expand Down
66 changes: 51 additions & 15 deletions tests/ert/unit_tests/resources/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,27 +398,63 @@ def test_that_delete_directory_can_delete_directories_with_internal_symlinks():


@pytest.mark.usefixtures("use_tmpdir")
def test_copy_directory_error():
with pytest.raises(OSError, match="existing directory"):
copy_directory("does/not/exist", "target")
def test_copy_directory_errors_when_source_directory_does_not_exist():
not_existing = "does/not/exist"
with pytest.raises(
OSError,
match=f"Input argument: '{not_existing}' does not correspond to an existing directory",
):
copy_directory(not_existing, "target")


@pytest.mark.usefixtures("use_tmpdir")
def test_copy_directory_errors_when_source_directory_is_file():
textfilename = "hei"
Path("file").write_text(textfilename, encoding="utf-8")
with pytest.raises(
OSError,
match=f"Input argument: '{textfilename}' does not correspond to an existing directory",
):
copy_directory(textfilename, "target")

with open("file", "w", encoding="utf-8") as f:
f.write("hei")

with pytest.raises(OSError, match="existing directory"):
copy_directory("hei", "target")
@pytest.mark.usefixtures("use_tmpdir")
def test_copy_directory_errors_when_copying_dir_to_itself():
empty_dir = Path("testdir")
empty_dir.mkdir()

empty_dir = "emptytestdir"
if not os.path.exists(empty_dir):
os.makedirs(empty_dir)
(empty_dir / "file").write_text("some_text", encoding="utf-8")

file_path = os.path.join(empty_dir, "file")
with pytest.raises(OSError, match="are the same file"):
copy_directory(empty_dir.name, ".")

with open(file_path, "w", encoding="utf-8") as f:
f.write("some_text")

with pytest.raises(OSError):
copy_directory(empty_dir, ".")
@pytest.mark.usefixtures("use_tmpdir")
def test_copy_directory_errors_when_symlinks_point_nowhere():
somedir = "somedir"
some_symlink = f"{somedir}/some_symlink"
Path(somedir).mkdir()
os.symlink("/not_existing", some_symlink)
with pytest.raises(OSError, match=f"No such file or directory: '{some_symlink}'"):
copy_directory(somedir, "copydir")


@pytest.mark.usefixtures("use_tmpdir")
def test_copy_directory_reports_multiple_errors():
somedir = "somedir"
some_symlink = f"{somedir}/some_symlink"
some_other_symlink = f"{somedir}/some_other_symlink"

Path(somedir).mkdir()
os.symlink("/not_existing", some_symlink)
os.symlink("/not_existing", some_other_symlink)
try:
copy_directory(somedir, "copydir")
raise AssertionError("copy_directory should raise")
except OSError as err:
# (The order of occurence of the filenames in the string is non-deterministic)
assert some_symlink in str(err)
assert some_other_symlink in str(err)


@pytest.mark.usefixtures("use_tmpdir")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ install_data:
target: templates

forward_model:
- well_swapping run --priorities well_priorities.json --constraints swapping_constraints.json --cases wells.json --output well_swap_output.json --config files/well_swap_config.yml
- well_swapping --priorities well_priorities.json --constraints swapping_constraints.json --cases wells.json --output well_swap_output.json --config files/well_swap_config.yml

####################################
# FROM HERE ON, NOTHING NEW...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ install_data:
target: templates

forward_model:
- well_swapping run --priorities well_priorities.json --constraints swapping_constraints.json --cases wells.json --output well_swap_output.json --config files/well_swap_config.yml
- well_swapping --priorities well_priorities.json --constraints swapping_constraints.json --cases wells.json --output well_swap_output.json --config files/well_swap_config.yml

####################################
# FROM HERE ON, NOTHING NEW...
Expand Down
Loading