From 375fe36902531662f3f7c73b624d2cc5840c0d69 Mon Sep 17 00:00:00 2001 From: Kylian Schmidt Date: Mon, 8 Sep 2025 15:59:43 +0200 Subject: [PATCH] Fix pipeline attempt (6): Backup --- tests/test_backup.py | 20 ++++++-------------- utils/backup.py | 35 +++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/tests/test_backup.py b/tests/test_backup.py index 7044a01..d21e289 100644 --- a/tests/test_backup.py +++ b/tests/test_backup.py @@ -16,27 +16,19 @@ def test_backup_creates_zip(tmp_path, monkeypatch): monkeypatch.setattr(backup, 'WEB_FOLDER', web_folder) monkeypatch.setattr(backup, 'BACKUP_FOLDER', backup_folder) - # Manually execute the backup logic + # Call the backup function + backup.create_backup() + + # Check that backup was created today = datetime.date.today().strftime('%Y%m%d') backup_name = f'backup-{today}.zip' backup_path = backup_folder / backup_name - # Remove if exists - if backup_path.exists(): - backup_path.unlink() - - # Create backup manually using the backup module's logic - with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf: - for path in web_folder.rglob("*"): - if path.is_file(): - arcname = path.relative_to(web_folder.parent) - zipf.write(path, arcname) - assert backup_path.exists() with zipfile.ZipFile(backup_path, 'r') as z: names = z.namelist() assert any('file1.txt' in n for n in names) assert any('file2.txt' in n for n in names) - + # Cleanup: remove the backup file after test - backup_path.unlink() + backup_path.unlink() \ No newline at end of file diff --git a/utils/backup.py b/utils/backup.py index d1937a4..0741c51 100644 --- a/utils/backup.py +++ b/utils/backup.py @@ -5,19 +5,26 @@ from pathlib import Path WEB_FOLDER = Path("plots") BACKUP_FOLDER = Path("backups") -today = datetime.date.today().strftime("%Y%m%d") -backup_name = f"backup-{today}.zip" -backup_path = BACKUP_FOLDER / backup_name -BACKUP_FOLDER.mkdir(parents=True, exist_ok=True) +def create_backup(): + """Create a backup of the web folder.""" + today = datetime.date.today().strftime("%Y%m%d") + backup_name = f"backup-{today}.zip" + backup_path = BACKUP_FOLDER / backup_name -if backup_path.exists(): - print(f"Backup already exists: {backup_path}") -else: - print(f"Creating backup: {backup_path}") - with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf: - for path in WEB_FOLDER.rglob("*"): - if path.is_file(): - arcname = path.relative_to(WEB_FOLDER.parent) - zipf.write(path, arcname) - print("✅ Backup complete.") + BACKUP_FOLDER.mkdir(parents=True, exist_ok=True) + + if backup_path.exists(): + print(f"Backup already exists: {backup_path}") + else: + print(f"Creating backup: {backup_path}") + with zipfile.ZipFile(backup_path, "w", zipfile.ZIP_DEFLATED) as zipf: + for path in WEB_FOLDER.rglob("*"): + if path.is_file(): + arcname = path.relative_to(WEB_FOLDER.parent) + zipf.write(path, arcname) + print("✅ Backup complete.") + + +if __name__ == "__main__": + create_backup()