working MVP

This commit is contained in:
bvn13 2024-10-09 23:39:22 +03:00
parent 1601d8a300
commit 1ff38457a1

49
main.py
View File

@ -1,9 +1,12 @@
import argparse import argparse
import subprocess
from app.logger import logger as l from app.logger import logger as l
import datetime as dt import datetime as dt
import subprocess as proc import subprocess as proc
from scheduler import Scheduler from scheduler import Scheduler
import time import time
from time import localtime, strftime
delta = dt.timedelta(hours=3) delta = dt.timedelta(hours=3)
tz_msk = dt.timezone(offset=delta, name="MSK") tz_msk = dt.timezone(offset=delta, name="MSK")
@ -12,10 +15,10 @@ logger = l.getChild(__name__)
logger.warning("Starting") logger.warning("Starting")
parser = argparse.ArgumentParser(__name__) 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() args = parser.parse_args()
directory = args.d directory = args.watch
logger.info(f"Directory to watch: {directory}") logger.info(f"Directory to watch: {directory}")
@ -24,15 +27,37 @@ def c(command: str) -> list[str]:
return command.split(" ") return command.split(" ")
def _watcher() -> None: def _watcher() -> None:
g_status = proc.check_output(c("git status -s")) logger.info("Starting to watch")
if g_status is not None: g_status = proc.check_output(c("git status -s"), cwd=directory)
proc.run(c("git add .")) output = ''
now = time.ctime() if len(g_status) > 0:
proc.run(c(f"git commit -m '{now}'")) logger.info("Changes found")
proc.run(c("git fetch")) output += "\n{}".format(proc.run(c("git add ."), cwd=directory, stderr=subprocess.STDOUT))
proc.run(c("git rebase")) 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 = Scheduler()
schedule.daily(dt.time(hour=7, minute=5, tzinfo=tz_msk), _watcher) schedule.cyclic(dt.timedelta(seconds=10), _watcher)
print(schedule)
while True:
schedule.exec_jobs()
time.sleep(1)