Download terminal_backup_script_021/backup_script_evaluator.py from Cognyzer/OsWorld: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/datasets/Cognyzer/OsWorld/resolve/main/terminal_backup_script_021/backup_script_evaluator.py
- Command line
-
hf download hf://datasets/Cognyzer/OsWorld/terminal_backup_script_021/backup_script_evaluator.py
-
curl -L -o backup_script_evaluator.py https://huggingface.co/datasets/Cognyzer/OsWorld/resolve/main/terminal_backup_script_021/backup_script_evaluator.py
2.64 kB
| import os | |
| import stat | |
| import sys | |
| import subprocess | |
| import glob | |
| HOME_DIR = "/home/jkim" | |
| BACKUPS_DIR = "/home/jkim/backups" | |
| SCRIPT_PATH = "/home/jkim/backup_script.sh" | |
| EXPECTED_USER = "jkim" | |
| def run_sudo_cmd(cmd): | |
| """Run command with sudo and return output""" | |
| try: | |
| result = subprocess.run( | |
| ["sudo", "bash", "-c", cmd], | |
| capture_output=True, | |
| text=True, | |
| timeout=10 | |
| ) | |
| return result.stdout.strip(), result.returncode | |
| except Exception as e: | |
| return str(e), 1 | |
| # ---------- Check .tmp files removed ---------- | |
| output, _ = run_sudo_cmd(f"ls -la {HOME_DIR}/*.tmp 2>/dev/null | wc -l") | |
| try: | |
| tmp_count = int(output) | |
| except: | |
| tmp_count = 0 | |
| if tmp_count > 0: | |
| print("TMP_FILES_STILL_PRESENT") | |
| sys.exit(0) | |
| print("TMP_FILES_REMOVED") | |
| # ---------- Check .bak files removed ---------- | |
| output, _ = run_sudo_cmd(f"ls -la {HOME_DIR}/*.bak 2>/dev/null | wc -l") | |
| try: | |
| bak_count = int(output) | |
| except: | |
| bak_count = 0 | |
| if bak_count > 0: | |
| print("BAK_FILES_STILL_PRESENT") | |
| sys.exit(0) | |
| print("BAK_FILES_REMOVED") | |
| # ---------- Check backups directory exists ---------- | |
| output, rc = run_sudo_cmd(f"test -d {BACKUPS_DIR} && echo 'EXISTS'") | |
| if "EXISTS" not in output: | |
| print("BACKUPS_DIR_MISSING") | |
| sys.exit(0) | |
| print("BACKUPS_DIR_EXISTS") | |
| # ---------- Check script exists ---------- | |
| output, rc = run_sudo_cmd(f"test -f {SCRIPT_PATH} && echo 'EXISTS'") | |
| if "EXISTS" not in output: | |
| print("SCRIPT_MISSING") | |
| sys.exit(0) | |
| print("SCRIPT_EXISTS") | |
| # ---------- Check shebang ---------- | |
| output, rc = run_sudo_cmd(f"head -1 {SCRIPT_PATH}") | |
| if output.strip() != "#!/bin/bash": | |
| print("SHEBANG_MISSING") | |
| sys.exit(0) | |
| print("SHEBANG_OK") | |
| # ---------- Check rsync command ---------- | |
| output, rc = run_sudo_cmd(f"cat {SCRIPT_PATH}") | |
| if "rsync -a --delete /home/jkim/Documents/ /home/jkim/backups/" not in output: | |
| print("RSYNC_COMMAND_MISSING") | |
| sys.exit(0) | |
| print("RSYNC_COMMAND_OK") | |
| # ---------- Check ownership ---------- | |
| output, rc = run_sudo_cmd(f"stat -c '%U' {SCRIPT_PATH}") | |
| if output.strip() != EXPECTED_USER: | |
| print(f"OWNER_MISMATCH_{output.strip()}") | |
| sys.exit(0) | |
| print("OWNER_OK") | |
| # ---------- Check permissions (700) ---------- | |
| output, rc = run_sudo_cmd(f"stat -c '%a' {SCRIPT_PATH}") | |
| if output.strip() != "700": | |
| print(f"PERMS_BAD_{output.strip()}") | |
| sys.exit(0) | |
| print("PERMS_OK") | |
| # ---------- Check executable ---------- | |
| output, rc = run_sudo_cmd(f"test -x {SCRIPT_PATH} && echo 'EXEC'") | |
| if "EXEC" not in output: | |
| print("EXECUTABLE_BAD") | |
| sys.exit(0) | |
| print("EXECUTABLE_OK") | |
| print("ALL_CHECKS_PASSED") | |