X-Git-Url: https://git.phdru.name/?p=phdru.name%2Fphdru.name.git;a=blobdiff_plain;f=remove_old_html.py;fp=remove_old_html.py;h=904ff9eb8a6d934e3c63bcb4a1bfbf51eba2cad7;hp=0000000000000000000000000000000000000000;hb=e968f29c9bd237139cd9a7f662562f69ecdc057c;hpb=0f441641cea7980190fc22b97bc6779a5e96f1aa 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)