From 636ac88daf2a599b3b7dfc94180f58b8c62cb81f Mon Sep 17 00:00:00 2001 From: thematdev Date: Wed, 22 Sep 2021 14:29:02 +0300 Subject: [PATCH] Added lockfile system --- app/__init__.py | 35 ++++++++++++++++++++++++++++++----- app/hash_manager.py | 17 +++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 app/hash_manager.py diff --git a/app/__init__.py b/app/__init__.py index 69fbc15..5632ca3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,7 +1,8 @@ -from flask import Flask, url_for, render_template +from flask import Flask, url_for, render_template, abort import os import json from app.config import PANDOC_LINK, PANDOC_PATH +from app.hash_manager import hash_file_sha512 import subprocess app = Flask(__name__) @@ -24,17 +25,41 @@ def dated_url_for(endpoint, **values): return url_for(endpoint, **values) +def render_page(page): + path = f'{PANDOC_PATH}/{page}' + if not os.path.exists(f'{path}'): + raise Exception("Page doesn't exist!") + in_filename = f'{path}/main.md' + out_filename = f'{path}/render.html' + os.system(f'pandoc {in_filename} --to html --mathjax --output {out_filename}') + print('Creating lock file') + os.system(f'echo {hash_file_sha512(in_filename)} > {out_filename}.lock') + + @app.route(f'/{PANDOC_LINK}/') def get_pandoc_page(page): - with open(f'{PANDOC_PATH}/{page}/config.json') as f: + path = f'{PANDOC_PATH}/{page}' + if not os.path.exists(f'{path}'): + abort(404) + with open(f'{path}/config.json') as f: data = json.loads(f.read()) - filename = f'{PANDOC_PATH}/{page}/main.md' - inserted = subprocess.Popen(f'pandoc {filename} -t html --mathjax', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8') + if not os.path.exists(f'{path}/render.html') or not os.path.exists(f'{path}/render.html.lock'): + print('Rendered page or lockfile for {page} does not exist! Rendering {page}') + render_page(page) + else: + with open(f'{path}/render.html.lock', 'r') as f: + rendered_hash = f.read().strip() + current_hash = hash_file_sha512(f'{path}/main.md') + if rendered_hash != current_hash: + print(f'CURRENT: {current_hash}, RENDERED: {rendered_hash}') + print('Integrity test failed, rendering {page}!') + render_page(page) template = data['template'] + with open(f'{path}/render.html', 'r') as f: + inserted = f.read() return render_template(template, markdown=inserted) @app.route('/') def index(): return render_template('index.html') - diff --git a/app/hash_manager.py b/app/hash_manager.py new file mode 100644 index 0000000..0f3acd7 --- /dev/null +++ b/app/hash_manager.py @@ -0,0 +1,17 @@ +import hashlib + + +BUF_SZ = 4096 + + +def hash_file_sha512(filename): + res = hashlib.sha512() + with open(filename, 'rb') as f: + data = f.read(BUF_SZ) + while data: + res.update(data) + data = f.read(BUF_SZ) + return res.hexdigest() + + +