From 672046c4874803657629f672bdd99a796beeab9d Mon Sep 17 00:00:00 2001 From: IneeddHelp <158655561+IneeddHelp@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:20:37 -0600 Subject: [PATCH] Add files via upload --- cron-scheduler/create_cron.py | 238 ++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 cron-scheduler/create_cron.py diff --git a/cron-scheduler/create_cron.py b/cron-scheduler/create_cron.py new file mode 100644 index 0000000..fc91c00 --- /dev/null +++ b/cron-scheduler/create_cron.py @@ -0,0 +1,238 @@ +from crontab import CronItem, CronTab + +# TODO: FIX OVERRIDING PROBLEM + + +def print_time_prompt(): + print( + """ + Which of the following which you like to configure? + (Enter option number) + + 1. Time (specific and repetition) + 2. Day (Includes day of month) + 3. Month + 4. Day of the week + 5. Finished + """ + ) + + +def create_job(): + cron = CronTab(user=True) + + cron_command: str = input("What command would you like to be run?\n") + job = cron.new(command=cron_command) + prompt_cron_config(job) + cron.write() + + print("Cron job created.") + + +def do_append() -> bool: + print( + """ + Existing Rule detected, do you wish to append? + Y for append, N (or any other string) for overwrite + """ + ) + return input().strip().lower() == "y" + + +def set_time(job: CronItem): + + while True: + print( + """ + What Part of time would you like to configure? + + 1. Repeat at specific time + 2. Run every x minutes + 3. Run every x hours + """ + ) + user_in: str = input().strip() + if user_in == "1": + print( + """ + What Time would you like the command to run at? + Time must be in format HH:MM and in 24-hour time + """ + ) + time_rep: list[str] = input().strip().split(":") + if len(time_rep) == 2 and time_rep[0].isdigit() and time_rep[1].isdigit(): + if ( + int(time_rep[0]) >= 0 + and int(time_rep[0]) < 24 + and int(time_rep[1]) >= 0 + and int(time_rep[1]) < 60 + ): + job.hour.on(int(time_rep[0])) + job.minute.on(int(time_rep[1])) + print(f"Time set, job will repeat at {time_rep[0]}:{time_rep[1]}\n") + break + else: + print("Invalid minute or hour value\n") + else: + print("Invalid format of response\n") + elif user_in == "2": + print( + """ + At what minute interval would you like the command to repeat? + """ + ) + minute_in: str = input() + if minute_in.isdigit() and int(minute_in) > 0: + job.minute.every(int(minute_in)) + print(f"Repetition set, job will repeat every {minute_in} minutes") + break + else: + print("Invalid Minute value and/or format") + elif user_in == "3": + print( + """ + At what day interval would you like the command to repeat? + """ + ) + hour_in: str = input() + if hour_in.isdigit() and int(hour_in) > 0: + job.hour.every(int(hour_in)) + print(f"Repetition set, job will repeat every {hour_in} hours") + break + else: + print("Invalid Minute value and/or format") + else: + print("Invalid option, try again") + + +def set_day(job: CronItem): + while True: + print( + """ + What part of day (number) would you like to configure? + + 1. Repeat on certain day of the month + 2. Repeat every x days + """ + ) + user_in = input().strip() + + if user_in == "1": + print("Which day of the month would you like to repeat on?") + day_in = input() + if day_in.isdigit() and int(day_in) > 0 and int(day_in) <= 31: + job.dom.on(int(day_in)) + print( + f"Repetition set, job will repeat every {day_in} day of the month" + ) + break + else: + print("Invalid day value and/or format") + elif user_in == "2": + print( + """ + At what day interval would you like the job to repeat? + """ + ) + day_in = input() + if day_in.isdigit() and int(day_in) > 0 and int(day_in) <= 31: + job.day.every(int(day_in)) + print( + f"Repetition set, job will repeat every {day_in} day of the month" + ) + break + else: + print("Invalid day value and/or format") + else: + print("Invalid option selected. Try again") + + +def set_month(job: CronItem): + while True: + print( + """ + What part of month (number) would you like to configure? + + 1. Repeat on certain month of the year + 2. Repeat every x months + """ + ) + user_in = input().strip() + + if user_in == "1": + print("Which month of the year would you like to repeat on?") + month_in = input() + if month_in.isdigit() and int(month_in) > 0 and int(month_in) <= 12: + job.month.on(int(month_in)) + print( + f"Repetition set, job will repeat every {month_in} month of the year" + ) + break + else: + print("Invalid month value and/or format") + elif user_in == "2": + print( + """ + At what month interval would you like the job to repeat? + """ + ) + month_in = input() + if month_in.isdigit() and int(month_in) > 0 and int(month_in) <= 12: + job.month.every(int(month_in)) + print( + f"Repetition set, job will repeat every {month_in} month of the year" + ) + break + else: + print("Invalid month value and/or format") + else: + print("Invalid option selected. Try again") + + +def set_day_of_week(job: CronItem): + dow_dict = { + "0": "Sunday", + "1": "Monday", + "2": "Tuesday", + "3": "Wednesday", + "4": "Thursday", + "5": "Friday", + "6": "Saturday", + } + while True: + print("What day of the week would you like the job to repeat on? (0-6)") + user_in = input().strip() + if user_in.isdigit() and int(user_in) >= 0 and int(user_in) < 6: + job.dow.on(int(user_in)) + print(f"Repetition set, job will repeat on {dow_dict[user_in]}") + break + else: + print("Invalid day of week value/format. Try again") + + +def prompt_cron_config(job: CronItem): + + input_func_dict = { + "1": set_time, + "2": set_day, + "3": set_month, + "4": set_day_of_week, + } + while True: + print_time_prompt() + user_in: str = input("").strip() + try: + input_func_dict[user_in](job) + except KeyError: + if user_in == "5": + break + else: + print("Invalid option, try again") + + +if __name__ == "__main__": + + try: + create_job() + except KeyboardInterrupt: + print("\nINTERRUPT DETECTED. Exiting...")