-
Notifications
You must be signed in to change notification settings - Fork 5
/
travisPorcess.py
51 lines (45 loc) · 1.37 KB
/
travisPorcess.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
from meta import meta_data, path
import time
import os
import shutil
import re
#在文件开头进行添加
def appendFILE(path, content):
with open(path, 'r+', encoding='utf-8') as f:
old = f.read()
f.seek(0)
f.write(content)
f.write(re.sub(r"^\s*#[^#\n]+\n", "", old)) #删除标题
#获取文件头信息
def getHEAD(head):
HEAD = "---\n"
for k in head:
if isinstance(head[k], list):
content = '\n'
for i in head[k]:
content += ' - \"'+i+'\"\n'
HEAD += "%s: %s\n" % (k, content[0:-1])
else:
HEAD += "%s: \"%s\"\n" % (k, head[k])
HEAD += "---\n"
# print(HEAD)
return HEAD
#处理.md文件
def processMD(path, meta):
appendFILE(path, getHEAD(meta))
print('Processed file %s' % path)
#进行MD文件目录的处理
#扫描文件目录
#如果遇到文件目录,则processMDIR递归
#如果遇到.md文件则调用processMD处理
def processMDIR(path, meta_data):
for i in os.listdir(path):
p = os.path.join(path, i)
if os.path.isdir(p):
processMDIR(p, meta_data)
elif p[-3:] == '.md' and os.path.isfile(p):
processMD(p, meta_data[p] if p in meta_data else {})
print('Processed directory %s' % path)
if __name__ == "__main__":
shutil.copytree(path, path+'.bak')
processMDIR(path, meta_data)