This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
code_analyst.py
94 lines (79 loc) · 2.62 KB
/
code_analyst.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
# Copyright (c) 2020 Core2002
# ZxbSkyWorldPlugin is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# code_analyst.py
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import prettytable as pt
# 后缀集合
CPP_SUFFIX_SET = {'.h', '.hpp', '.hxx', '.c', '.cpp', '.cc', '.cxx'}
PYTHON_SUFFIX_SET = {'.py'}
JAVA_SUFFIX_SET = {'.java'}
# 全局变量
cpp_lines = 0
python_lines = 0
java_lines = 0
total_lines = 0
def list_files(path):
"""
遍历工程路径path,如果遇到文件则统计其行数,如果遇到目录则进行递归
"""
filenames = os.listdir(path)
for f in filenames:
fpath = os.path.join(path, f)
if os.path.isfile(fpath):
count_lines(fpath)
if os.path.isdir(fpath):
list_files(fpath)
def count_lines(fpath):
"""
对于文件fpath,计算它的行数,然后根据其后缀将它的行数加到相应的全局变量当中
"""
global CPP_SUFFIX_SET, PYTHON_SUFFIX_SET, JAVA_SUFFIX_SET
global cpp_lines, python_lines, java_lines, total_lines
# 统计行数
with open(fpath, 'rb') as f:
cnt = 0
last_data = '\n'
while True:
data = f.read(0x400000)
if not data:
break
cnt += data.count(b'\n')
last_data = data
if last_data[-1:] != b'\n':
cnt += 1
# 只统计C/C++,Python和Java这三类代码
suffix = os.path.splitext(fpath)[-1]
if suffix in CPP_SUFFIX_SET:
cpp_lines += cnt
elif suffix in PYTHON_SUFFIX_SET:
python_lines += cnt
elif suffix in JAVA_SUFFIX_SET:
java_lines += cnt
else:
pass
def print_result():
"""
本函数依赖库prettytable,请使用sudo pip3 install prettytable进行安装
"""
tb = pt.PrettyTable()
tb.field_names = ['CPP', 'PYTHON', 'JAVA', 'TOTAL']
tb.add_row([cpp_lines, python_lines, java_lines, total_lines])
print(tb)
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage : python3 code_analyst.py project_path")
else:
project_path = sys.argv[1]
list_files(project_path)
total_lines = cpp_lines + python_lines + java_lines
print_result()