Skip to content

Commit

Permalink
Fix No such file or directory in test
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-cty committed Sep 11, 2022
1 parent 88b1fad commit d541335
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions codecarbon/core/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import shutil
import subprocess
from contextlib import contextmanager
from os.path import expandvars
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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)

Expand All @@ -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)
4 changes: 2 additions & 2 deletions tests/test_core_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import shutil
import tempfile

from codecarbon.core.util import backup, resolve_path
Expand All @@ -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()

0 comments on commit d541335

Please sign in to comment.