working MVP
This commit is contained in:
parent
1601d8a300
commit
1ff38457a1
49
main.py
49
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)
|
||||
|
||||
schedule = Scheduler()
|
||||
schedule.cyclic(dt.timedelta(seconds=10), _watcher)
|
||||
|
||||
print(schedule)
|
||||
|
||||
while True:
|
||||
schedule.exec_jobs()
|
||||
time.sleep(1)
|
Loading…
Reference in New Issue
Block a user