Add new feature to lastmod script tool.
This commit is contained in:
parent
299d7e799f
commit
5a4803a3d7
1 changed files with 77 additions and 32 deletions
|
@ -16,29 +16,45 @@ Licensed under MIT
|
||||||
import sys
|
import sys
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
|
import getopt
|
||||||
import subprocess
|
import subprocess
|
||||||
import shutil
|
import shutil
|
||||||
|
import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
from ruamel.yaml import YAML
|
from ruamel.yaml import YAML
|
||||||
|
|
||||||
from utils.common import get_yaml
|
from utils.common import get_yaml
|
||||||
from utils.common import check_py_version
|
from utils.common import check_py_version
|
||||||
|
|
||||||
|
|
||||||
POSTS_PATH = "_posts"
|
Date = Enum('Date', ('GIT', 'FS'))
|
||||||
|
|
||||||
|
POSTS_PATH = '_posts'
|
||||||
|
|
||||||
|
|
||||||
def help():
|
def help():
|
||||||
print("Usage: "
|
print("Usage: "
|
||||||
" python update_posts_lastmod.py [option]\n"
|
" python update_posts_lastmod.py [options]\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
" -v, --verbose Print verbose logs\n")
|
" -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")
|
||||||
|
|
||||||
|
|
||||||
def update_lastmod(verbose):
|
def update_lastmod(path, verbose, date):
|
||||||
count = 0
|
count = 0
|
||||||
yaml = YAML()
|
yaml = YAML()
|
||||||
|
|
||||||
for post in glob.glob(os.path.join(POSTS_PATH, "*.md")):
|
for post in glob.glob(path):
|
||||||
|
|
||||||
|
lastmod = ''
|
||||||
|
|
||||||
|
if date == Date.GIT:
|
||||||
git_log_count = subprocess.getoutput(
|
git_log_count = subprocess.getoutput(
|
||||||
"git log --pretty=%ad \"{}\" | wc -l".format(post))
|
"git log --pretty=%ad \"{}\" | wc -l".format(post))
|
||||||
|
|
||||||
|
@ -57,17 +73,24 @@ def update_lastmod(verbose):
|
||||||
if "[Automation]" in lates_commit and "Lastmod" in lates_commit:
|
if "[Automation]" in lates_commit and "Lastmod" in lates_commit:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
lastmod = git_lastmod
|
||||||
|
|
||||||
|
elif date == Date.FS:
|
||||||
|
t = os.path.getmtime(post)
|
||||||
|
dt = datetime.datetime.fromtimestamp(t)
|
||||||
|
lastmod = dt.strftime('%F %T') + time.strftime(' %z')
|
||||||
|
|
||||||
frontmatter, line_num = get_yaml(post)
|
frontmatter, line_num = get_yaml(post)
|
||||||
meta = yaml.load(frontmatter)
|
meta = yaml.load(frontmatter)
|
||||||
|
|
||||||
if 'seo' in meta:
|
if 'seo' in meta:
|
||||||
if ('date_modified' in meta['seo'] and
|
if ('date_modified' in meta['seo'] and
|
||||||
meta['seo']['date_modified'] == git_lastmod):
|
meta['seo']['date_modified'] == lastmod):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
meta['seo']['date_modified'] = git_lastmod
|
meta['seo']['date_modified'] = lastmod
|
||||||
else:
|
else:
|
||||||
meta.insert(line_num, 'seo', dict(date_modified=git_lastmod))
|
meta.insert(line_num, 'seo', dict(date_modified=lastmod))
|
||||||
|
|
||||||
output = 'new.md'
|
output = 'new.md'
|
||||||
if os.path.isfile(output):
|
if os.path.isfile(output):
|
||||||
|
@ -99,23 +122,45 @@ def update_lastmod(verbose):
|
||||||
print("[INFO] Success to update lastmod for {} post(s).".format(count))
|
print("[INFO] Success to update lastmod for {} post(s).".format(count))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main(argv):
|
||||||
check_py_version()
|
check_py_version()
|
||||||
|
|
||||||
|
path = os.path.join(POSTS_PATH, "*.md")
|
||||||
verbose = False
|
verbose = False
|
||||||
|
date = Date.GIT
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
try:
|
||||||
for arg in sys.argv:
|
opts, args = getopt.getopt(
|
||||||
if arg == sys.argv[0]:
|
argv, "hf:d:vt:",
|
||||||
continue
|
["file=", "dir=", "help", "verbose", "datetime="])
|
||||||
else:
|
except getopt.GetoptError:
|
||||||
if arg == '-v' or arg == '--verbose':
|
help()
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
for opt, arg in opts:
|
||||||
|
if opt == '-h':
|
||||||
|
help()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
elif opt == '-f' or opt == '--file':
|
||||||
|
path = arg
|
||||||
|
|
||||||
|
elif opt == '-d' or opt == '--dir':
|
||||||
|
path = os.path.join(arg, "*.md")
|
||||||
|
|
||||||
|
elif opt == '-v' or opt == '--verbose':
|
||||||
verbose = True
|
verbose = True
|
||||||
|
|
||||||
|
elif opt == '-t' or opt == '--datetime':
|
||||||
|
if arg == 'git':
|
||||||
|
date = Date.GIT
|
||||||
|
elif arg == 'fs':
|
||||||
|
date = Date.FS
|
||||||
else:
|
else:
|
||||||
help()
|
help()
|
||||||
return
|
sys.exit(2)
|
||||||
|
|
||||||
update_lastmod(verbose)
|
update_lastmod(path, verbose, date)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main(sys.argv[1:])
|
||||||
|
|
Loading…
Reference in a new issue