From e968f29c9bd237139cd9a7f662562f69ecdc057c Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 12 Jun 2020 14:57:06 +0300 Subject: [PATCH] Feat(remove_old_html.py): Remove old HTML in the blog They will be regenerated with the corrected back/forth links. --- TODO | 6 ---- reindex_blog_ru | 3 ++ remove_old_html.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 6 deletions(-) create mode 100755 remove_old_html.py diff --git a/TODO b/TODO index 219689c..a7a08bb 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,3 @@ -éÓÐÒÁ×ÉÔØ × ÂÌÏÇÅ ÓÓÙÌËÉ ×ÐÅÒ£Ä. - -óÁÍÁ-ÔÏ ÓÓÙÌËÁ × ËÏÄÅ ÅÓÔØ, ÎÏ ÎÁÄÏ ÐÅÒÅÇÅÎÅÒÉÒÏ×ÁÔØ HTML -ÐÒÅÄÙÄÕÝÅÊ ÓÔÒÁÎÉÃÙ. - - ðÒÏÓÔÁ×ÉÔØ ÎÁ ×ÓÅÈ ÓÔÒÁÎÉÃÁÈ ÓÓÙÌËÉ ÎÁ ×ÁÌÉÄÁÔÏÒÙ http://validator.w3.org/check/referer É http://jigsaw.w3.org/css-validator/check/referer diff --git a/reindex_blog_ru b/reindex_blog_ru index a02f190..1109cdc 100755 --- a/reindex_blog_ru +++ b/reindex_blog_ru @@ -5,5 +5,8 @@ umask 022 LC_TIME=ru_RU.KOI8-R export LC_TIME +cd "`dirname \"$0\"`" && +./remove_old_html.py && + cd ../htdocs/phdru.name && exec ../../phdru.name/reindex_blog.py diff --git a/remove_old_html.py b/remove_old_html.py new file mode 100755 index 0000000..904ff9e --- /dev/null +++ b/remove_old_html.py @@ -0,0 +1,80 @@ +#! /usr/bin/env python + +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2020 PhiloSoft Design" + +import os +from blog_db import blog_root, load_blog +blog = load_blog() + +# Run a loop over the blog posts: +# for every post note its previous and next posts +# and compare timestamps of previous and next HTML +# with the timestamp of the current template; +# if HTMLs are older - remove them, +# they will be regenerated with the corrected back/forth links. + + +def fullpath(key, post): + return os.path.join(*(blog_root,) + key + (post[0],)) + + +def html(tmpl): + return tmpl[:-len('tmpl')] + 'html' + + +prev_key = prev_blog_post = current_key = current_blog_post = \ + next_key = next_blog_post = None +for key in sorted(blog): + for blog_post in sorted(blog[key]): + if current_blog_post: + prev_key = current_key + prev_blog_post = current_blog_post + if next_blog_post: + current_key = next_key + current_blog_post = next_blog_post + next_key = key + next_blog_post = blog_post + + if current_key: + current_tmpl = fullpath(current_key, current_blog_post) + try: + current_mtime = os.path.getmtime(current_tmpl) + except OSError: + current_mtime = None + + if prev_key: + prev_html = html(fullpath(prev_key, prev_blog_post)) + try: + prev_mtime = os.path.getmtime(prev_html) + except OSError: + pass + else: + if current_mtime is not None and (prev_mtime < current_mtime): + os.remove(prev_html) + if current_key and next_key: + next_html = html(fullpath(next_key, next_blog_post)) + try: + next_mtime = os.path.getmtime(next_html) + except OSError: + pass + else: + if current_mtime is not None and (next_mtime < current_mtime): + os.remove(next_html) + +current_tmpl = fullpath(key, blog_post) +try: + current_mtime = os.path.getmtime(current_tmpl) +except OSError: + pass +else: + current_mtime = None + +prev_html = html(fullpath(current_key, current_blog_post)) +try: + prev_mtime = os.path.getmtime(prev_html) +except OSError: + pass +else: + if current_mtime is not None and (prev_mtime < current_mtime): + os.remove(prev_html) -- 2.39.2