-
Notifications
You must be signed in to change notification settings - Fork 6
/
touch.py
62 lines (49 loc) · 1.78 KB
/
touch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# 'Cos the touch plugin doesn't work when I publish/rsync for some weird
# reason. Couldn't be bothered to fix it when writing this was quicker.
#
import datetime
import os
import sys
content_dir = None
output_dir = None
def metadata(path, filename):
rst_file = os.path.join(path, filename)
date, timestamp, slug = None, None, None
with open(rst_file) as f:
for line in f.readlines():
if line.startswith(':date:'):
_, date = line.split(' ', 1)
if date.count(':') > 1:
form = '%Y-%m-%d %H:%M:%S'
else:
form = '%Y-%m-%d %H:%M'
date = date.strip()
timestamp = datetime.datetime.strptime(date, form).timestamp()
if line.startswith(':slug:'):
slug = line.split(' ')[1].strip()
return date, timestamp, slug
def html_file(date, filename):
year, month, day = date.split(' ')[0].split('-')
return '{}/{}/{}/{}/index.html'.format(year, month, day, filename)
def touch(filename, timestamp):
global output_dir
path = os.path.join(output_dir, filename)
print('touching ->', path)
os.utime(path, (timestamp, timestamp))
print('touching ->', path.replace('index.html', ''))
os.utime(path.replace('index.html', ''), (timestamp, timestamp))
def walk():
global content_dir
for rst_file in os.listdir(content_dir):
if not rst_file.endswith('.rst'):
continue
date, timestamp, slug = metadata(content_dir, rst_file)
html = html_file(date, slug)
touch(html, timestamp)
if __name__ == "__main__":
if len(sys.argv) > 2:
content_dir, output_dir = sys.argv[1:3]
walk()
else:
print("Usage: python3 touch.py CONTENT_DIR OUTPUT_DIR")