Skip to content
Merged
Changes from 2 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
14 changes: 13 additions & 1 deletion src/custodian/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ def backup(filenames, prefix="error", directory="./") -> None:
series of error.1.tar.gz, error.2.tar.gz, ... will be generated.
directory (str): directory where the files exist
"""
num = max([0] + [int(file.split(".")[-3]) for file in glob(os.path.join(directory, f"{prefix}.*.tar.gz"))])
backup_files = glob(os.path.join(directory, f"{prefix}.*.tar.gz")) + glob(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be done as a single glob. glob(os.path.join(directory, f"{prefix}.*.tar*")). There is no need to add two globs.

os.path.join(directory, f"{prefix}.*.tar")
)
nums = []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nums = [0]

for file in backup_files:
try:
if file.endswith(".tar.gz"):
nums.append(int(file.split(".")[-3]))
elif file.endswith(".tar"):
nums.append(int(file.split(".")[-2]))
except (ValueError, IndexError):
continue
num = max([0, *nums])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just needs to be num = max(nums)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions. I have introduced the changes.

prefix = f"{prefix}.{num + 1}"
filename = os.path.join(directory, f"{prefix}.tar.gz")
logging.info(f"Backing up run to {filename}.")
Expand Down