SpiecsEngine
 
Loading...
Searching...
No Matches
Statistics.py
Go to the documentation of this file.
1'''
2@files Statics.py.
3@brief Statics some information.
4@author Spices.
5'''
6
7import os
8import argparse
9from itertools import (takewhile, repeat)
10
11'''
12@brief Files basic root folder.
13'''
14rootFolder: str = ''
15
16'''
17@brief Files with those extensions can be counted.
18'''
19extensions: dict = {
20 'c': 1,
21 'h': 1,
22 'cpp': 1,
23 'hpp': 1,
24 'lua': 1,
25 'py': 1,
26 'material': 1,
27 'glsl': 1,
28 'mesh': 1,
29 'task': 1,
30 'vert': 1,
31 'geom': 1,
32 'frag': 1,
33 'comp': 1,
34 'rgen': 1,
35 'rchit': 1,
36 'rmiss': 1,
37 'bat': 1,
38}
39
40'''
41@brief Files in those folders can not be counted.
42'''
43ignoreFolders: dict = {
44 'spv': 1,
45 'vendor': 1,
46 '.git': 1,
47 '.idea': 1,
48 '.vs': 1,
49 'bin': 1,
50 'bin-int': 1,
51 'venv': 1,
52}
53
54'''
55@brief Counted rows.
56'''
57nRows: int = 0
58
59'''
60@brief Counted files.
61'''
62nFiles: int = 0
63
64def iter_count(file_name: str):
65
66 '''
67 @brief calculate file rows.
68 @param[in] file_name File's full name.
69 @return Returns file rows.
70 '''
71
72 buffer = 1024 * 1024
73 with open(file_name, encoding='gb18030', errors='ignore') as f:
74 buf_gen = takewhile(lambda x: x, (f.read(buffer) for _ in repeat(None)))
75 n = sum(buf.count('\n') for buf in buf_gen) + 1
76 return n
77
78def iter_files(file_dir: str):
79
80 '''
81 @brief Iter all files and count rows.
82 @param[in] file_dir File's folder.
83 '''
84
85 global extensions
86 global ignoreFolders
87 global nRows
88 global nFiles
89
90 for filepath, dirnames, filenames in os.walk(file_dir):
91 for filename in filenames:
92
93 ignore = False
94 for ignoreFolder in ignoreFolders:
95 if filepath.find(ignoreFolder) != -1:
96 ignore = True
97 break
98
99 if ignore:
100 continue
101
102 fullname = os.path.join(filepath, filename)
103 if extensions.get(os.path.splitext(filename)[-1][1:]) is not None:
104 nFiles += 1
105 nRows += iter_count(os.path.join(filepath, filename))
106
107def main():
108
109 '''
110 @brief Iter all files and count rows and print result.
111 '''
112
113 global rootFolder
114
115 parser = argparse.ArgumentParser(description='Statistics Spices Solution')
116 parser.add_argument('--rootFolder', type=str, required=True, help='root folder path, e.g. C:/')
117 args = parser.parse_args()
118
119 if not os.path.isdir(args.rootFolder):
120 raise Exception('Invalid argument for --rootFolder: {}'.format(args.rootFolder))
121
122 rootFolder = args.rootFolder
123 iter_files(rootFolder)
124 print("Spices Project Statistics:")
125 print("nRows : %d" % nRows)
126 print("nFiles: %d" % nFiles)
127
128if __name__ == '__main__':
129 main()
iter_count(str file_name)
Definition Statistics.py:64
iter_files(str file_dir)
Definition Statistics.py:78