~cytrogen/srht-deploy

ref: 098f0ded66b5f614ec99f0d3efb89b3a5b1bc0f6 srht-deploy/todo-custom/fix_jinja2_do.py -rw-r--r-- 994 bytes
098f0ded — Cytrogen 初始提交:sourcehut Docker Compose 自托管部署 10 days ago
                                                                                
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
"""Enable Jinja2 'do' extension in sourcehut Flask app.

Upstream fix: https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/39036
"""
import glob

candidates = glob.glob("/usr/lib/python3.*/site-packages/srht/flask.py")
if not candidates:
    raise FileNotFoundError("Cannot find srht/flask.py")

source_file = candidates[0]

with open(source_file) as f:
    content = f.read()

target = "self.jinja_loader = ChoiceLoader"
patch = '        self.jinja_env.add_extension("jinja2.ext.do")'

if "jinja2.ext.do" not in content:
    lines = content.split("\n")
    patched = False
    for i, line in enumerate(lines):
        if target in line:
            lines.insert(i + 1, patch)
            patched = True
            break
    if not patched:
        raise RuntimeError(f"Could not find '{target}' in {source_file}")
    with open(source_file, "w") as f:
        f.write("\n".join(lines))
    print(f"Patched {source_file}: enabled jinja2.ext.do")
else:
    print(f"Already patched: {source_file}")