Skip to content

Commit 173ee2d

Browse files
committed
Add a dev_script
代码量统计工具
1 parent 68af4e2 commit 173ee2d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

DevScripts/CLOC_Script.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
调用cloc代码行数统计工具统计本项目代码量的脚本
3+
4+
https://github.com/AlDanial/cloc
5+
"""
6+
7+
import pathlib
8+
from os import popen
9+
10+
# 配置项
11+
Git_Ignore_File = "../.gitignore" # gitignore文件路径,用以排除不需要列目录的文件
12+
Project_Root_Path = "../" # 项目根目录路径
13+
# 配置项完
14+
15+
16+
def cloc(gitignore_file: str = ".gitignore") -> None:
17+
"""
18+
使用cloc统计项目代码行数 \n
19+
:param gitignore_file: gitignore文件路径
20+
:return: None
21+
"""
22+
23+
ignored_dir = ""
24+
gitignore_file_p = pathlib.Path(gitignore_file)
25+
with gitignore_file_p.open("r", encoding="UTF-8") as f:
26+
for dir_name in f.readlines():
27+
if not dir_name.startswith("#"):
28+
dir_name = dir_name.replace("/", "").replace("\n", ",")
29+
ignored_dir += dir_name
30+
31+
# 调用cloc,并排除gitignore中的目录,需要提前将cloc添加到系统环境变量
32+
cmd = f"cloc --exclude-dir {ignored_dir} {Project_Root_Path}"
33+
34+
with popen(cmd) as p:
35+
cmd_result = p.read()
36+
# 如果cmd执行正常退出则p.close()返回None,失败则返回状态码
37+
if p.close():
38+
print("cloc调用失败,请检查")
39+
else:
40+
# 根据cloc返回结果,连续两个换行符后面的内容是需要的信息
41+
cloc_result = cmd_result.split("\n\n", 1)[1]
42+
print(cloc_result)
43+
# TODO 将 self.cloc_result 写入 Markdown 文件
44+
45+
46+
if __name__ == "__main__":
47+
cloc(Git_Ignore_File)

0 commit comments

Comments
 (0)