]> git.phdru.name Git - phdru.name/phdru.name.git/blob - remove_old_html.py
Refactor(dotfiles2html): Fix import
[phdru.name/phdru.name.git] / remove_old_html.py
1 #! /usr/bin/env python
2
3 __author__ = "Oleg Broytman <phd@phdru.name>"
4 __copyright__ = "Copyright (C) 2020 PhiloSoft Design"
5
6 import os
7 from blog_db import blog_root, load_blog
8 blog = load_blog()
9
10 # Run a loop over the blog posts:
11 # for every post note its previous and next posts
12 # and compare timestamps of previous and next HTML
13 # with the timestamp of the current template;
14 # if HTMLs are older - remove them,
15 # they will be regenerated with the corrected back/forth links.
16
17
18 def fullpath(key, post):
19     return os.path.join(*(blog_root,) + key + (post[0],))
20
21
22 def html(tmpl):
23     return tmpl[:-len('tmpl')] + 'html'
24
25
26 prev_key = prev_blog_post = current_key = current_blog_post = \
27     next_key = next_blog_post = None
28 for key in sorted(blog):
29     for blog_post in sorted(blog[key]):
30         if current_blog_post:
31             prev_key = current_key
32             prev_blog_post = current_blog_post
33         if next_blog_post:
34             current_key = next_key
35             current_blog_post = next_blog_post
36         next_key = key
37         next_blog_post = blog_post
38
39         if current_key:
40             current_tmpl = fullpath(current_key, current_blog_post)
41             try:
42                 current_mtime = os.path.getmtime(current_tmpl)
43             except OSError:
44                 current_mtime = None
45
46         if prev_key:
47             prev_html = html(fullpath(prev_key, prev_blog_post))
48             try:
49                 prev_mtime = os.path.getmtime(prev_html)
50             except OSError:
51                 pass
52             else:
53                 if current_mtime is not None and (prev_mtime < current_mtime):
54                     os.remove(prev_html)
55         if current_key and next_key:
56             next_html = html(fullpath(next_key, next_blog_post))
57             try:
58                 next_mtime = os.path.getmtime(next_html)
59             except OSError:
60                 pass
61             else:
62                 if current_mtime is not None and (next_mtime < current_mtime):
63                     os.remove(next_html)
64
65 current_tmpl = fullpath(key, blog_post)
66 try:
67     current_mtime = os.path.getmtime(current_tmpl)
68 except OSError:
69     pass
70 else:
71     current_mtime = None
72
73 prev_html = html(fullpath(current_key, current_blog_post))
74 try:
75     prev_mtime = os.path.getmtime(prev_html)
76 except OSError:
77     pass
78 else:
79     if current_mtime is not None and (prev_mtime < current_mtime):
80         os.remove(prev_html)