-
Notifications
You must be signed in to change notification settings - Fork 19
/
create_tree.py
112 lines (97 loc) · 3.03 KB
/
create_tree.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import sys
import getopt
import subprocess
import fire
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
purple='\033[0;35m'
cyan='\033[0;36m'
white='\033[0;37m'
end='\033[0m'
'''
通过运行脚本,读取文件夹的文件生成目录,为了gitbook和wiki
使用: python3 create_tree.py -h 查看帮助
'''
# 忽略的文件夹
ignoreFolder=['.git', 'backup', '.vscode', 'ARTS']
# 所有要被忽略的文件
ignoreFile=['PULL_REQUEST_TEMPLATE.md', 'ISSUE_TEMPLATE.md', 'CODE_OF_CONDUCT.md','README.md',
'Readme.md', 'CSS3.md', 'HTML5.md', '_Sidebar.md', 'SUMMARY.md']
result = []
def logError(msg):
print('%s%s%s'%(red, msg, end))
def logInfo(msg):
print('%s%s%s'%(green, msg, end))
def printParam(verb, args, comment):
print(' %s%-5s %s%-6s %s%s'%(green, verb, yellow, args, end, comment))
def help():
print('run: %s %s <verb> %s <args>%s'%('create_tree.py', green, yellow, end))
printParam('-h', '', 'help')
printParam('-s', '', 'show catalog')
printParam('-a', '', 'append cataloag to SUMMARAY.md')
# 列出所有文件的列表 并排好序
def listFiles(name):
lists = []
dir_list = []
temp = os.listdir(name)
temp.sort()
for r in temp:
if(not os.path.isdir(name+'/'+r)):
lists.append(r)
else:
dir_list.append(r)
lists.sort()
lists = dir_list + lists
return lists
# 处理文件
def handlerFile(name, count, path):
temp = ' '*count
if not name in ignoreFile:
result.append(temp+'* [ '+name[:-3]+' ](/'+path+')')
# 处理标题(文件夹)
def handlerFolder(name, count):
temp = ' '*(int(count)-1)
result.append(temp+'* 【 '+name+' 】')
# 递归 读取文件夹
def readFolder(name, count):
handlerFolder(name,count)
for fold in listFiles(name):
if fold in ignoreFolder:
continue
if not os.path.isdir(name+'/'+fold):
handlerFile(fold, count, name+'/'+fold)
else:
readFolder(name+'/'+fold, count+1)
def readAll():
Folders = os.listdir('./')
Folders.sort()
# 处理根目录下的md文件
for fold in Folders:
if fold.endswith('.md') and not fold in ignoreFile:
result.append("* [ "+fold[:-3]+" ](./"+fold+")")
# 得到根目录下所有文件夹,然后开始递归得到所有文件
for fold in Folders:
if os.path.isdir(fold) and not fold in ignoreFolder :
readFolder(fold, 1)
def main(verb=None):
if verb == '-h':
help()
return
# 只在终端输出
if verb == "-s":
readAll()
for res in result:
print(res)
# 追加到SUMMARY
if verb == "-a":
readAll()
subprocess.call('mv SUMMARY.md SUMMARY.md.bak', shell=True)
with open('SUMMARY.md','w+') as dest:
dest.write('# Summary\n\n* [ Introduction ](README.md)\n\n')
for res in result:
dest.write(res+'\n')
logInfo('complete refresh catalog file')
fire.Fire(main)