Skip to content

Commit 1bcf4e0

Browse files
committed
Merge branch 'master' of github.com:materialsproject/custodian
2 parents 8a6c1e4 + c3edc8f commit 1bcf4e0

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/custodian/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ def backup(filenames, prefix="error", directory="./") -> None:
2121
directory (str): directory where the files exist
2222
"""
2323
num = max([0] + [int(file.split(".")[-3]) for file in glob(os.path.join(directory, f"{prefix}.*.tar.gz"))])
24-
filename = os.path.join(directory, f"{prefix}.{num + 1}.tar.gz")
24+
prefix = f"{prefix}.{num + 1}"
25+
filename = os.path.join(directory, f"{prefix}.tar.gz")
2526
logging.info(f"Backing up run to {filename}.")
2627
with tarfile.open(filename, "w:gz") as tar:
2728
for fname in filenames:
2829
for file in glob(os.path.join(directory, fname)):
29-
tar.add(file)
30+
tar.add(file, arcname=os.path.join(prefix, os.path.basename(file)))
3031

3132

3233
def get_execution_host_info():

tests/test_utils.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,43 @@ def some_func(x):
3030
assert n_calls == 3
3131

3232

33-
def test_backup(tmp_path) -> None:
33+
def test_backup(tmp_path, monkeypatch) -> None:
34+
monkeypatch.chdir(tmp_path)
35+
with open("INCAR", "w") as f:
36+
f.write("This is a test file.")
37+
38+
backup(["INCAR"])
39+
40+
assert Path("error.1.tar.gz").exists()
41+
with tarfile.open("error.1.tar.gz", "r:gz") as tar:
42+
assert len(tar.getmembers()) == 1
43+
assert tar.getnames() == ["error.1/INCAR"]
44+
45+
46+
def test_backup_with_glob(tmp_path, monkeypatch) -> None:
47+
monkeypatch.chdir(tmp_path)
48+
with open("INCAR", "w") as f:
49+
f.write("This is a test file.")
50+
with open("POSCAR", "w") as f:
51+
f.write("This is a test file.")
52+
with open("garbage", "w") as f:
53+
f.write("This is a test file.")
54+
55+
backup(["*CAR"])
56+
57+
assert Path("error.1.tar.gz").exists()
58+
with tarfile.open("error.1.tar.gz", "r:gz") as tar:
59+
assert len(tar.getmembers()) == 2
60+
assert tar.getnames() == ["error.1/INCAR", "error.1/POSCAR"]
61+
62+
63+
def test_backup_with_directory(tmp_path) -> None:
3464
with open(tmp_path / "INCAR", "w") as f:
3565
f.write("This is a test file.")
3666

3767
backup(["INCAR"], directory=tmp_path)
3868

3969
assert Path(tmp_path / "error.1.tar.gz").exists()
4070
with tarfile.open(tmp_path / "error.1.tar.gz", "r:gz") as tar:
41-
assert len(tar.getmembers()) > 0
71+
assert len(tar.getmembers()) == 1
72+
assert tar.getnames() == ["error.1/INCAR"]

0 commit comments

Comments
 (0)