-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatic_html_page.py
executable file
·73 lines (54 loc) · 2.12 KB
/
static_html_page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from jinja2 import Environment, FileSystemLoader
from random import choice
from pathlib import Path
from shutil import copy
import datetime
import os
from git import Repo
from logger import logger
from jinja_filters import *
REPO_URL = f"https://{os.getenv("GH_PAT")}@github.com/drehelis/sammy_ofer"
TMP_REPO_DIR = "/tmp/sammy_ofer"
STATIC_HTML_FILENAME = "static.html"
GH_PAGES_BRANCH = "static_page"
absolute_path = Path(__file__).resolve().parent
def gen_static_page(obj):
environment = Environment(loader=FileSystemLoader(absolute_path / "assets/templates/"))
environment.filters["babel_format_full_heb"] = babel_format_full_heb
template = environment.get_template("static_page.jinja2")
content = template.render(games=obj)
try:
with open(absolute_path / STATIC_HTML_FILENAME, mode="r", encoding="utf-8") as f:
existing_content = f.read()
except FileNotFoundError:
existing_content = None
if existing_content == content:
return
with open(absolute_path / STATIC_HTML_FILENAME, mode="w", encoding="utf-8") as f:
f.write(content)
logger.info(f"Generated {STATIC_HTML_FILENAME} from template")
if os.getenv("SKIP_COMMIT"):
logger.info("SKIP_COMMIT is set, skipping git commit")
return
git_commit()
def git_commit():
try:
repo = Repo(TMP_REPO_DIR)
repo.remotes.origin.pull(GH_PAGES_BRANCH)
except:
repo = Repo.clone_from(REPO_URL, TMP_REPO_DIR)
repo.config_writer().set_value("user", "name", "sammy-ofer-bot").release()
repo.config_writer().set_value("user", "email", "[email protected]").release()
try:
repo.git.checkout(GH_PAGES_BRANCH)
except:
repo.git.checkout(b=GH_PAGES_BRANCH)
src = absolute_path / STATIC_HTML_FILENAME
copy(src, f"{TMP_REPO_DIR}/{STATIC_HTML_FILENAME}")
repo.index.add([STATIC_HTML_FILENAME])
if not repo.index.diff("HEAD"):
logger.info(f"{STATIC_HTML_FILENAME} is up to date")
return
logger.info(f"{STATIC_HTML_FILENAME} pushed to repo")
repo.index.commit(str(datetime.datetime.now()))
repo.git.push()