From 157d8ae7222fa93e5939b1a9bcaa9db4f67cf70f Mon Sep 17 00:00:00 2001 From: benoit-cty <6603048+benoit-cty@users.noreply.github.com> Date: Wed, 15 Jun 2022 08:51:54 +0200 Subject: [PATCH] Fix No such file or directory in test --- codecarbon/core/util.py | 11 ++++++----- tests/test_core_util.py | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/codecarbon/core/util.py b/codecarbon/core/util.py index 58a1ce1cb..9a1f03b24 100644 --- a/codecarbon/core/util.py +++ b/codecarbon/core/util.py @@ -1,5 +1,6 @@ import os import re +import shutil import subprocess from contextlib import contextmanager from os.path import expandvars @@ -64,7 +65,7 @@ def backup(file_path: Union[str, Path], ext: Optional[str] = ".bak") -> None: backup = parent / file_name idx += 1 - file_path.rename(backup) + shutil.copyfile(file_path, backup) def detect_cpu_model() -> str: @@ -78,7 +79,7 @@ def detect_cpu_model() -> str: def count_cpus() -> int: if os.environ.get("SLURM_JOB_ID") is None: - return psutil.cpu_count(logical=False) + return psutil.cpu_count(logical=True) try: scontrol = subprocess.check_output( @@ -89,7 +90,7 @@ def count_cpus() -> int: "Error running `scontrol show job $SLURM_JOBID` " + "to count SLURM-available cpus. Using the machine's cpu count." ) - return psutil.cpu_count(logical=False) + return psutil.cpu_count(logical=True) num_cpus_matches = re.findall(r"NumCPUs=\d+", scontrol) @@ -98,14 +99,14 @@ def count_cpus() -> int: "Could not find NumCPUs= after running `scontrol show job $SLURM_JOBID` " + "to count SLURM-available cpus. Using the machine's cpu count." ) - return psutil.cpu_count(logical=False) + return psutil.cpu_count(logical=True) if len(num_cpus_matches) > 1: logger.warning( "Unexpected output after running `scontrol show job $SLURM_JOBID` " + "to count SLURM-available cpus. Using the machine's cpu count." ) - return psutil.cpu_count(logical=False) + return psutil.cpu_count(logical=True) num_cpus = num_cpus_matches[0].replace("NumCPUs=", "") return int(num_cpus) diff --git a/tests/test_core_util.py b/tests/test_core_util.py index 413f9d8e4..ef76fe27e 100644 --- a/tests/test_core_util.py +++ b/tests/test_core_util.py @@ -1,4 +1,4 @@ -import os +import shutil import tempfile from codecarbon.core.util import backup, resolve_path @@ -11,7 +11,7 @@ def test_backup(): assert expected_backup_path.exists() # re-create file and back it up again second_file = tempfile.NamedTemporaryFile() - os.rename(second_file.name, first_file.name) + shutil.copyfile(second_file.name, first_file.name) backup(first_file.name) backup_of_backup_path = resolve_path(f"{first_file.name}_0.bak") assert backup_of_backup_path.exists()