From 1ff38457a113ef6757bdc7362e1e44faf00f9bb0 Mon Sep 17 00:00:00 2001 From: bvn13 Date: Wed, 9 Oct 2024 23:39:22 +0300 Subject: [PATCH] working MVP --- main.py | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 3d10fd7..979240d 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,12 @@ import argparse +import subprocess + from app.logger import logger as l import datetime as dt import subprocess as proc from scheduler import Scheduler import time +from time import localtime, strftime delta = dt.timedelta(hours=3) tz_msk = dt.timezone(offset=delta, name="MSK") @@ -12,10 +15,10 @@ logger = l.getChild(__name__) logger.warning("Starting") parser = argparse.ArgumentParser(__name__) -parser.add_argument("d", help="Directory to watch") +parser.add_argument("-w", "--watch", help="Directory to watch") args = parser.parse_args() -directory = args.d +directory = args.watch logger.info(f"Directory to watch: {directory}") @@ -24,15 +27,37 @@ def c(command: str) -> list[str]: return command.split(" ") def _watcher() -> None: - g_status = proc.check_output(c("git status -s")) - if g_status is not None: - proc.run(c("git add .")) - now = time.ctime() - proc.run(c(f"git commit -m '{now}'")) - proc.run(c("git fetch")) - proc.run(c("git rebase")) + logger.info("Starting to watch") + g_status = proc.check_output(c("git status -s"), cwd=directory) + output = '' + if len(g_status) > 0: + logger.info("Changes found") + output += "\n{}".format(proc.run(c("git add ."), cwd=directory, stderr=subprocess.STDOUT)) + now = strftime("%Y-%m-%dT%H:%M:%S", localtime()) + output += "\n{}".format(proc.run(c(f"git commit -m '{now}'"), cwd=directory, stderr=subprocess.STDOUT)) + output += "\n{}".format(proc.run(c("git fetch"), cwd=directory)) + try: + out = proc.run(c("git rebase"), cwd=directory, stderr=subprocess.STDOUT) + output += "\n{}".format(out) + except subprocess.SubprocessError as ex: + output += "\n{}".format(ex.output) + if ex.output.find('--skip') > 0: + proc.run(c("git rebase --skip"), stderr=subprocess.STDOUT) + else: + logger.error("Unexpected output") + output += "\n{}".format(proc.run(c("git push"), cwd=directory)) + else: + output += "\n{}".format(proc.run(c("git pull"), cwd=directory, stderr=subprocess.STDOUT)) + logger.info(output) + logger.info("Done") -schedule = Scheduler(tzinfo=tz_msk) -schedule.minutely() -schedule.daily(dt.time(hour=7, minute=5, tzinfo=tz_msk), _watcher) \ No newline at end of file + +schedule = Scheduler() +schedule.cyclic(dt.timedelta(seconds=10), _watcher) + +print(schedule) + +while True: + schedule.exec_jobs() + time.sleep(1) \ No newline at end of file