-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
184 lines (153 loc) · 5.46 KB
/
scanner.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sqlite3
import logging
from datetime import datetime
from pathlib import Path
from scan_one import scan_py_file
DB_FILE = 'data.db'
SOURCES_FILE = 'sources.txt'
g_log = None
def create_tables(connection):
cursor = connection.cursor()
sql = """
CREATE TABLE IF NOT EXISTS "files" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"fname" TEXT NOT NULL
);
"""
cursor.execute(sql)
sql = """
CREATE TABLE IF NOT EXISTS "global_vars" (
"fname" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"line_no" INTEGER NOT NULL,
UNIQUE (fname, line_no) ON CONFLICT REPLACE
);
"""
cursor.execute(sql)
sql = """
CREATE TABLE IF NOT EXISTS "classes" (
"fname" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"doc_string" TEXT NOT NULL DEFAULT '',
"line_no" INTEGER NOT NULL,
UNIQUE (fname, line_no) ON CONFLICT REPLACE
);
"""
cursor.execute(sql)
sql = """
CREATE TABLE IF NOT EXISTS "functions" (
"fname" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"args" TEXT NOT NULL DEFAULT '',
"class_name" TEXT NOT NULL DEFAULT '',
"doc_string" TEXT NOT NULL DEFAULT '',
"line_no" INTEGER,
UNIQUE (fname, line_no) ON CONFLICT REPLACE
);
"""
cursor.execute(sql)
connection.commit()
def get_fname_id(fname, connection):
connection = sqlite3.connect(DB_FILE)
cursor = connection.cursor()
# print(fname)
cursor.execute("""SELECT id
FROM files
WHERE fname=?;
""", (str(fname),))
res = cursor.fetchone()
if res:
return res[0]
cursor.execute(f"""
INSERT INTO files(fname)
VALUES (?)""", (str(fname),))
connection.commit()
cursor.execute("""SELECT id
FROM files
WHERE fname=?;
""", (str(fname),))
res = cursor.fetchone()
return res[0]
def add_func(fnameId, data, connection):
cursor = connection.cursor()
name = data['name']
args = data['args']
className = data['class_name']
docStr = data['doc_str']
lineNo = data['line_no']
cursor.execute(f"""
INSERT OR REPLACE into functions
(name, args, class_name, fname, doc_string, line_no)
VALUES (?, ?, ?, ?, ?, ?)""", (name, args, className, fnameId, docStr, lineNo))
def add_class(fnameId, data, connection):
cursor = connection.cursor()
name = data['name']
docStr = data['doc_str']
lineNo = data['line_no']
cursor.execute(f"""
INSERT OR REPLACE into classes
(name, fname, doc_string, line_no)
VALUES (?, ?, ?, ?)""", (name, fnameId, docStr, lineNo))
def add_global_var(fnameId, data, connection):
cursor = connection.cursor()
name = data['name']
lineNo = data['line_no']
cursor.execute(f"""
INSERT OR REPLACE into global_vars
(name, fname, line_no)
VALUES (?, ?, ?)""", (name, fnameId, lineNo))
def process_py_file(fname, connection):
fname = Path(fname).resolve()
fnameId = get_fname_id(fname, connection)
try:
for d in scan_py_file(fname):
# print(d)
if d['type'] == 'func':
add_func(fnameId, d, connection)
elif d['type'] == 'class':
add_class(fnameId, d, connection)
elif d['type'] == 'global_var':
add_global_var(fnameId, d, connection)
else:
raise Exception(f'Unsupported type: {d["type"]}')
except Exception as e:
g_log.warning(f'Не удалось обработать файл "{fname}", {e}')
return
connection.commit()
def process_dir(dirName, connection):
for f in dirName.glob('**/*.py'):
g_log.info(f'Обрабатываем файл "{f}"')
process_py_file(f, connection)
def init_logger():
logger = logging.getLogger('server')
# вывод лога в файл
logger.setLevel(logging.DEBUG)
dt = datetime.now().isoformat().replace(':', '.')
scriptName = f'scanner_{dt}.log'
handler = logging.FileHandler(scriptName, mode='w', encoding='utf-8')
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# вывод лога на экран
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
if __name__ == '__main__':
g_log = init_logger()
g_log.info(f'Открываем файл базы данных "{DB_FILE}"')
connection = sqlite3.connect(DB_FILE)
create_tables(connection)
g_log.info(f'Читаем список источников из файла "{SOURCES_FILE}"')
sources = Path(SOURCES_FILE).read_text().splitlines()
for srcDir in sources:
srcDir = srcDir.strip()
if not srcDir or srcDir.startswith('#'):
continue
g_log.info(f'Начинаем обработку источника "{srcDir}"')
process_dir(Path(srcDir), connection)
connection.commit()
connection.close()