Skip to content

Commit 9c33256

Browse files
committed
Add new feature to lastmod script tool.
1 parent cef1a32 commit 9c33256

1 file changed

Lines changed: 78 additions & 33 deletions

File tree

_scripts/py/update_posts_lastmod.py

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,81 @@
1616
import sys
1717
import glob
1818
import os
19+
import getopt
1920
import subprocess
2021
import shutil
22+
import datetime
23+
import time
2124

25+
from enum import Enum
2226
from ruamel.yaml import YAML
27+
2328
from utils.common import get_yaml
2429
from utils.common import check_py_version
2530

2631

27-
POSTS_PATH = "_posts"
32+
Date = Enum('Date', ('GIT', 'FS'))
33+
34+
POSTS_PATH = '_posts'
2835

2936

3037
def help():
3138
print("Usage: "
32-
" python update_posts_lastmod.py [option]\n"
39+
" python update_posts_lastmod.py [options]\n"
3340
"Options:\n"
34-
" -v, --verbose Print verbose logs\n")
41+
" -f, --file <file path> Read a file.\n"
42+
" -d, --dir <directory path> Read from a directory.\n"
43+
" -h, --help Print help information\n"
44+
" -v, --verbose Print verbose logs\n"
45+
" -t, --datetime < git | fs > Chose post's datetime source, "
46+
"'git' for git-log, 'fs' for filesystem, default to 'git'.\n")
3547

3648

37-
def update_lastmod(verbose):
49+
def update_lastmod(path, verbose, date):
3850
count = 0
3951
yaml = YAML()
4052

41-
for post in glob.glob(os.path.join(POSTS_PATH, "*.md")):
42-
git_log_count = subprocess.getoutput(
43-
"git log --pretty=%ad \"{}\" | wc -l".format(post))
53+
for post in glob.glob(path):
54+
55+
lastmod = ''
56+
57+
if date == Date.GIT:
58+
git_log_count = subprocess.getoutput(
59+
"git log --pretty=%ad \"{}\" | wc -l".format(post))
60+
61+
if git_log_count == "1":
62+
continue
63+
64+
git_lastmod = subprocess.getoutput(
65+
"git log -1 --pretty=%ad --date=iso \"{}\"".format(post))
4466

45-
if git_log_count == "1":
46-
continue
67+
if not git_lastmod:
68+
continue
4769

48-
git_lastmod = subprocess.getoutput(
49-
"git log -1 --pretty=%ad --date=iso \"{}\"".format(post))
70+
lates_commit = subprocess.check_output(
71+
['git', 'log', '-1', '--pretty=%B', post]).decode('utf-8')
5072

51-
if not git_lastmod:
52-
continue
73+
if "[Automation]" in lates_commit and "Lastmod" in lates_commit:
74+
continue
5375

54-
lates_commit = subprocess.check_output(
55-
['git', 'log', '-1', '--pretty=%B', post]).decode('utf-8')
76+
lastmod = git_lastmod
5677

57-
if "[Automation]" in lates_commit and "Lastmod" in lates_commit:
58-
continue
78+
elif date == Date.FS:
79+
t = os.path.getmtime(post)
80+
dt = datetime.datetime.fromtimestamp(t)
81+
lastmod = dt.strftime('%F %T') + time.strftime(' %z')
5982

6083
frontmatter, line_num = get_yaml(post)
6184
meta = yaml.load(frontmatter)
6285

6386
if 'seo' in meta:
6487
if ('date_modified' in meta['seo'] and
65-
meta['seo']['date_modified'] == git_lastmod):
88+
meta['seo']['date_modified'] == lastmod):
6689
continue
6790
else:
68-
meta['seo']['date_modified'] = git_lastmod
91+
meta['seo']['date_modified'] = lastmod
6992
else:
70-
meta.insert(line_num, 'seo', dict(date_modified=git_lastmod))
93+
meta.insert(line_num, 'seo', dict(date_modified=lastmod))
7194

7295
output = 'new.md'
7396
if os.path.isfile(output):
@@ -99,23 +122,45 @@ def update_lastmod(verbose):
99122
print("[INFO] Success to update lastmod for {} post(s).".format(count))
100123

101124

102-
def main():
125+
def main(argv):
103126
check_py_version()
104127

128+
path = os.path.join(POSTS_PATH, "*.md")
105129
verbose = False
106-
107-
if len(sys.argv) > 1:
108-
for arg in sys.argv:
109-
if arg == sys.argv[0]:
110-
continue
130+
date = Date.GIT
131+
132+
try:
133+
opts, args = getopt.getopt(
134+
argv, "hf:d:vt:",
135+
["file=", "dir=", "help", "verbose", "datetime="])
136+
except getopt.GetoptError:
137+
help()
138+
sys.exit(2)
139+
140+
for opt, arg in opts:
141+
if opt == '-h':
142+
help()
143+
sys.exit()
144+
145+
elif opt == '-f' or opt == '--file':
146+
path = arg
147+
148+
elif opt == '-d' or opt == '--dir':
149+
path = os.path.join(arg, "*.md")
150+
151+
elif opt == '-v' or opt == '--verbose':
152+
verbose = True
153+
154+
elif opt == '-t' or opt == '--datetime':
155+
if arg == 'git':
156+
date = Date.GIT
157+
elif arg == 'fs':
158+
date = Date.FS
111159
else:
112-
if arg == '-v' or arg == '--verbose':
113-
verbose = True
114-
else:
115-
help()
116-
return
160+
help()
161+
sys.exit(2)
117162

118-
update_lastmod(verbose)
163+
update_lastmod(path, verbose, date)
119164

120165

121-
main()
166+
main(sys.argv[1:])

0 commit comments

Comments
 (0)