|
16 | 16 | import sys |
17 | 17 | import glob |
18 | 18 | import os |
| 19 | +import getopt |
19 | 20 | import subprocess |
20 | 21 | import shutil |
| 22 | +import datetime |
| 23 | +import time |
21 | 24 |
|
| 25 | +from enum import Enum |
22 | 26 | from ruamel.yaml import YAML |
| 27 | + |
23 | 28 | from utils.common import get_yaml |
24 | 29 | from utils.common import check_py_version |
25 | 30 |
|
26 | 31 |
|
27 | | -POSTS_PATH = "_posts" |
| 32 | +Date = Enum('Date', ('GIT', 'FS')) |
| 33 | + |
| 34 | +POSTS_PATH = '_posts' |
28 | 35 |
|
29 | 36 |
|
30 | 37 | def help(): |
31 | 38 | print("Usage: " |
32 | | - " python update_posts_lastmod.py [option]\n" |
| 39 | + " python update_posts_lastmod.py [options]\n" |
33 | 40 | "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") |
35 | 47 |
|
36 | 48 |
|
37 | | -def update_lastmod(verbose): |
| 49 | +def update_lastmod(path, verbose, date): |
38 | 50 | count = 0 |
39 | 51 | yaml = YAML() |
40 | 52 |
|
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)) |
44 | 66 |
|
45 | | - if git_log_count == "1": |
46 | | - continue |
| 67 | + if not git_lastmod: |
| 68 | + continue |
47 | 69 |
|
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') |
50 | 72 |
|
51 | | - if not git_lastmod: |
52 | | - continue |
| 73 | + if "[Automation]" in lates_commit and "Lastmod" in lates_commit: |
| 74 | + continue |
53 | 75 |
|
54 | | - lates_commit = subprocess.check_output( |
55 | | - ['git', 'log', '-1', '--pretty=%B', post]).decode('utf-8') |
| 76 | + lastmod = git_lastmod |
56 | 77 |
|
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') |
59 | 82 |
|
60 | 83 | frontmatter, line_num = get_yaml(post) |
61 | 84 | meta = yaml.load(frontmatter) |
62 | 85 |
|
63 | 86 | if 'seo' in meta: |
64 | 87 | if ('date_modified' in meta['seo'] and |
65 | | - meta['seo']['date_modified'] == git_lastmod): |
| 88 | + meta['seo']['date_modified'] == lastmod): |
66 | 89 | continue |
67 | 90 | else: |
68 | | - meta['seo']['date_modified'] = git_lastmod |
| 91 | + meta['seo']['date_modified'] = lastmod |
69 | 92 | else: |
70 | | - meta.insert(line_num, 'seo', dict(date_modified=git_lastmod)) |
| 93 | + meta.insert(line_num, 'seo', dict(date_modified=lastmod)) |
71 | 94 |
|
72 | 95 | output = 'new.md' |
73 | 96 | if os.path.isfile(output): |
@@ -99,23 +122,45 @@ def update_lastmod(verbose): |
99 | 122 | print("[INFO] Success to update lastmod for {} post(s).".format(count)) |
100 | 123 |
|
101 | 124 |
|
102 | | -def main(): |
| 125 | +def main(argv): |
103 | 126 | check_py_version() |
104 | 127 |
|
| 128 | + path = os.path.join(POSTS_PATH, "*.md") |
105 | 129 | 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 |
111 | 159 | else: |
112 | | - if arg == '-v' or arg == '--verbose': |
113 | | - verbose = True |
114 | | - else: |
115 | | - help() |
116 | | - return |
| 160 | + help() |
| 161 | + sys.exit(2) |
117 | 162 |
|
118 | | - update_lastmod(verbose) |
| 163 | + update_lastmod(path, verbose, date) |
119 | 164 |
|
120 | 165 |
|
121 | | -main() |
| 166 | +main(sys.argv[1:]) |
0 commit comments