2019-09-30 15:38:41 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Update (or create if not existed) field 'seo.date_modified'
|
|
|
|
in posts' Front Matter by their latest git commit date.
|
|
|
|
|
|
|
|
Dependencies:
|
|
|
|
- git
|
|
|
|
- ruamel.yaml
|
|
|
|
|
2020-01-02 16:17:49 +03:00
|
|
|
v2.0
|
|
|
|
https://github.com/cotes2020/jekyll-theme-chirpy
|
2019-09-30 15:38:41 +03:00
|
|
|
© 2018-2019 Cotes Chung
|
|
|
|
Licensed under MIT
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2019-11-18 16:36:45 +03:00
|
|
|
import getopt
|
2019-09-30 15:38:41 +03:00
|
|
|
import subprocess
|
|
|
|
import shutil
|
2019-11-18 16:36:45 +03:00
|
|
|
import datetime
|
|
|
|
import time
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
from enum import Enum
|
2019-09-30 15:38:41 +03:00
|
|
|
from ruamel.yaml import YAML
|
2019-11-18 16:36:45 +03:00
|
|
|
|
2019-09-30 15:38:41 +03:00
|
|
|
from utils.common import get_yaml
|
2020-04-02 18:25:19 +03:00
|
|
|
from utils.common import get_makrdown_files
|
2019-09-30 15:38:41 +03:00
|
|
|
from utils.common import check_py_version
|
|
|
|
|
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
Date = Enum('Date', ('GIT', 'FS'))
|
|
|
|
|
|
|
|
POSTS_PATH = '_posts'
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
def help():
|
|
|
|
print("Usage: "
|
2019-11-18 16:36:45 +03:00
|
|
|
" python update_posts_lastmod.py [options]\n"
|
2019-09-30 15:38:41 +03:00
|
|
|
"Options:\n"
|
2019-11-18 16:36:45 +03:00
|
|
|
" -f, --file <file path> Read a file.\n"
|
|
|
|
" -d, --dir <directory path> Read from a directory.\n"
|
|
|
|
" -h, --help Print help information\n"
|
|
|
|
" -v, --verbose Print verbose logs\n"
|
|
|
|
" -t, --datetime < git | fs > Chose post's datetime source, "
|
|
|
|
"'git' for git-log, 'fs' for filesystem, default to 'git'.\n")
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
|
|
|
2020-04-02 18:25:19 +03:00
|
|
|
def update_lastmod(posts, verbose, date):
|
2019-09-30 15:38:41 +03:00
|
|
|
count = 0
|
|
|
|
yaml = YAML()
|
|
|
|
|
2020-04-02 18:25:19 +03:00
|
|
|
for post in posts:
|
2019-11-18 16:36:45 +03:00
|
|
|
|
|
|
|
lastmod = ''
|
|
|
|
|
|
|
|
if date == Date.GIT:
|
|
|
|
git_log_count = subprocess.getoutput(
|
|
|
|
"git log --pretty=%ad \"{}\" | wc -l".format(post))
|
|
|
|
|
|
|
|
if git_log_count == "1":
|
|
|
|
continue
|
|
|
|
|
|
|
|
git_lastmod = subprocess.getoutput(
|
|
|
|
"git log -1 --pretty=%ad --date=iso \"{}\"".format(post))
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
if not git_lastmod:
|
|
|
|
continue
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
lates_commit = subprocess.check_output(
|
|
|
|
['git', 'log', '-1', '--pretty=%B', post]).decode('utf-8')
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
if "[Automation]" in lates_commit and "Lastmod" in lates_commit:
|
|
|
|
continue
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
lastmod = git_lastmod
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
elif date == Date.FS:
|
|
|
|
t = os.path.getmtime(post)
|
|
|
|
dt = datetime.datetime.fromtimestamp(t)
|
|
|
|
lastmod = dt.strftime('%F %T') + time.strftime(' %z')
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
|
|
frontmatter, line_num = get_yaml(post)
|
|
|
|
meta = yaml.load(frontmatter)
|
|
|
|
|
|
|
|
if 'seo' in meta:
|
|
|
|
if ('date_modified' in meta['seo'] and
|
2019-11-18 16:36:45 +03:00
|
|
|
meta['seo']['date_modified'] == lastmod):
|
2019-09-30 15:38:41 +03:00
|
|
|
continue
|
|
|
|
else:
|
2019-11-18 16:36:45 +03:00
|
|
|
meta['seo']['date_modified'] = lastmod
|
2019-09-30 15:38:41 +03:00
|
|
|
else:
|
2019-11-18 16:36:45 +03:00
|
|
|
meta.insert(line_num, 'seo', dict(date_modified=lastmod))
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
|
|
output = 'new.md'
|
|
|
|
if os.path.isfile(output):
|
|
|
|
os.remove(output)
|
|
|
|
|
|
|
|
with open(output, 'w', encoding='utf-8') as new, \
|
|
|
|
open(post, 'r', encoding='utf-8') as old:
|
|
|
|
new.write("---\n")
|
|
|
|
yaml.dump(meta, new)
|
|
|
|
new.write("---\n")
|
|
|
|
line_num += 2
|
|
|
|
|
|
|
|
lines = old.readlines()
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if line_num > 0:
|
|
|
|
line_num -= 1
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
new.write(line)
|
|
|
|
|
|
|
|
shutil.move(output, post)
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print("[INFO] update 'lastmod' for:" + post)
|
|
|
|
|
|
|
|
if count > 0:
|
|
|
|
print("[INFO] Success to update lastmod for {} post(s).".format(count))
|
|
|
|
|
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
def main(argv):
|
2019-09-30 15:38:41 +03:00
|
|
|
check_py_version()
|
|
|
|
|
2020-04-02 18:25:19 +03:00
|
|
|
specific = False
|
|
|
|
posts = []
|
2019-09-30 15:38:41 +03:00
|
|
|
verbose = False
|
2019-11-18 16:36:45 +03:00
|
|
|
date = Date.GIT
|
|
|
|
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(
|
|
|
|
argv, "hf:d:vt:",
|
|
|
|
["file=", "dir=", "help", "verbose", "datetime="])
|
|
|
|
except getopt.GetoptError:
|
|
|
|
help()
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt == '-h':
|
|
|
|
help()
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
elif opt == '-f' or opt == '--file':
|
2020-04-02 18:25:19 +03:00
|
|
|
posts.append(arg)
|
|
|
|
specific = True
|
2019-11-18 16:36:45 +03:00
|
|
|
|
|
|
|
elif opt == '-d' or opt == '--dir':
|
2020-04-02 18:25:19 +03:00
|
|
|
posts = get_makrdown_files(arg)
|
|
|
|
specific = True
|
2019-11-18 16:36:45 +03:00
|
|
|
|
|
|
|
elif opt == '-v' or opt == '--verbose':
|
|
|
|
verbose = True
|
|
|
|
|
|
|
|
elif opt == '-t' or opt == '--datetime':
|
|
|
|
if arg == 'git':
|
|
|
|
date = Date.GIT
|
|
|
|
elif arg == 'fs':
|
|
|
|
date = Date.FS
|
2019-09-30 15:38:41 +03:00
|
|
|
else:
|
2019-11-18 16:36:45 +03:00
|
|
|
help()
|
|
|
|
sys.exit(2)
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2020-04-02 18:25:19 +03:00
|
|
|
if not specific:
|
|
|
|
posts = get_makrdown_files(POSTS_PATH)
|
|
|
|
|
|
|
|
update_lastmod(posts, verbose, date)
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
|
|
|
2019-11-18 16:36:45 +03:00
|
|
|
main(sys.argv[1:])
|