chore: Update everything
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env -S uv run --script
|
||||
# /// script
|
||||
# dependencies = []
|
||||
# ///
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description="Install cron job")
|
||||
parser.add_argument("--name", required=True, help="Name for the cron job")
|
||||
parser.add_argument("command", nargs=argparse.REMAINDER, help="Command to run")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def get_current_crontab() -> str:
|
||||
try:
|
||||
result = subprocess.run(["crontab", "-l"], capture_output=True, text=True, check=True)
|
||||
return result.stdout
|
||||
except subprocess.CalledProcessError:
|
||||
return ""
|
||||
|
||||
|
||||
def create_cron_entry(name: str, command: list) -> str:
|
||||
command_str = " ".join(command)
|
||||
return f"0 0 * * * {command_str}"
|
||||
|
||||
|
||||
def job_exists(name: str, current_crontab: str) -> bool:
|
||||
comment = f"# {name}"
|
||||
return comment in current_crontab
|
||||
|
||||
|
||||
def install_cron_job(name: str, command: list) -> None:
|
||||
current_crontab = get_current_crontab()
|
||||
|
||||
if job_exists(name, current_crontab):
|
||||
logger.info(f"Cron job '{name}' already exists")
|
||||
return
|
||||
|
||||
cron_entry = create_cron_entry(name, command)
|
||||
job_comment = f"# {name}"
|
||||
new_crontab = current_crontab.rstrip()
|
||||
|
||||
if new_crontab and not new_crontab.endswith("\n"):
|
||||
new_crontab += "\n"
|
||||
|
||||
new_crontab += f"{job_comment}\n{cron_entry}\n"
|
||||
|
||||
process = subprocess.Popen(["crontab", "-"], stdin=subprocess.PIPE, text=True)
|
||||
process.communicate(input=new_crontab)
|
||||
|
||||
if process.returncode != 0:
|
||||
raise RuntimeError("Failed to install cron job")
|
||||
|
||||
logger.info(f"Cron job '{name}' installed successfully")
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
|
||||
if not args.command:
|
||||
raise ValueError("Command is required")
|
||||
|
||||
logger.info(f"Installing cron job '{args.name}' with command: {' '.join(args.command)}")
|
||||
install_cron_job(args.name, args.command)
|
||||
|
||||
logger.info("Installation completed successfully")
|
||||
logger.info("Check cron jobs with: crontab -l")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit(main())
|
||||
Reference in New Issue
Block a user