muzing 173ee2d7c3 Add a dev_script
代码量统计工具
2022-09-14 08:37:46 +08:00

48 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
调用cloc代码行数统计工具统计本项目代码量的脚本
https://github.com/AlDanial/cloc
"""
import pathlib
from os import popen
# 配置项
Git_Ignore_File = "../.gitignore" # gitignore文件路径用以排除不需要列目录的文件
Project_Root_Path = "../" # 项目根目录路径
# 配置项完
def cloc(gitignore_file: str = ".gitignore") -> None:
"""
使用cloc统计项目代码行数 \n
:param gitignore_file: gitignore文件路径
:return: None
"""
ignored_dir = ""
gitignore_file_p = pathlib.Path(gitignore_file)
with gitignore_file_p.open("r", encoding="UTF-8") as f:
for dir_name in f.readlines():
if not dir_name.startswith("#"):
dir_name = dir_name.replace("/", "").replace("\n", ",")
ignored_dir += dir_name
# 调用cloc并排除gitignore中的目录需要提前将cloc添加到系统环境变量
cmd = f"cloc --exclude-dir {ignored_dir} {Project_Root_Path}"
with popen(cmd) as p:
cmd_result = p.read()
# 如果cmd执行正常退出则p.close()返回None失败则返回状态码
if p.close():
print("cloc调用失败请检查")
else:
# 根据cloc返回结果连续两个换行符后面的内容是需要的信息
cloc_result = cmd_result.split("\n\n", 1)[1]
print(cloc_result)
# TODO 将 self.cloc_result 写入 Markdown 文件
if __name__ == "__main__":
cloc(Git_Ignore_File)