Repository navigation
Expand file tree
/
Copy pathsop_analyzer.py
More file actions
416 lines (343 loc) · 16 KB
/
Copy pathsop_analyzer.py
File metadata and controls
416 lines (343 loc) · 16 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
"""
SOP Analyzer - Приложение для анализа .sop файлов
"""
import json
import zipfile
import zlib
import argparse
import sys
from pathlib import Path
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
import datetime
@dataclass
class RecordStats:
"""Статистика по записям"""
total: int = 0
deletes: int = 0
strong_overwrites: int = 0
@dataclass
class TableInfo:
"""Информация о таблице"""
name: str
record_count: int
actions: Dict[str, int]
class SOPAnalyzer:
"""Анализатор SOP файлов"""
def __init__(self, sop_file_path: str):
self.sop_file_path = Path(sop_file_path)
self._data = None
def load_data(self) -> Dict[str, Any]:
"""Загрузка и декодирование данных из SOP файла"""
if self._data is not None:
return self._data
if not self.sop_file_path.exists():
raise FileNotFoundError(f"SOP файл не найден: {self.sop_file_path}")
try:
with zipfile.ZipFile(self.sop_file_path, 'r') as sop_zip:
# Ищем файл с данными
data_files = [f for f in sop_zip.namelist() if f.endswith('.data')]
if not data_files:
raise ValueError("В архиве не найден файл с данными (.data)")
# Читаем данные из .data файла
with sop_zip.open(data_files[0]) as data_file:
compressed_data = data_file.read()
# Пробуем разные методы декомпрессии
decompressed_data = self._decompress_data(compressed_data)
self._data = json.loads(decompressed_data.decode('utf-8'))
return self._data
except zipfile.BadZipFile:
raise ValueError("Некорректный ZIP архив")
except json.JSONDecodeError as e:
raise ValueError(f"Ошибка парсинга JSON: {e}")
def _decompress_data(self, compressed_data: bytes) -> bytes:
"""Декомпрессия данных с использованием различных методов"""
methods = [
self._try_raw_deflate,
self._try_zlib_deflate,
self._try_gzip_format,
self._try_with_headers
]
for method in methods:
try:
result = method(compressed_data)
print(f"✓ Успешная декомпрессия методом: {method.__name__}")
return result
except Exception as e:
print(f"✗ Метод {method.__name__} не сработал: {e}")
continue
raise ValueError("Не удалось декомпрессировать данные ни одним из методов")
def _try_raw_deflate(self, data: bytes) -> bytes:
"""Попытка RAW Deflate декомпрессии"""
return zlib.decompress(data, -15)
def _try_zlib_deflate(self, data: bytes) -> bytes:
"""Попытка Zlib декомпрессии"""
return zlib.decompress(data)
def _try_gzip_format(self, data: bytes) -> bytes:
"""Попытка декомпрессии как GZIP с заголовком"""
# Добавляем простой gzip заголовок
gzip_header = b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff'
gzip_data = gzip_header + data + b'\x00\x00\x00\x00\x00\x00\x00\x00'
return zlib.decompress(gzip_data, 15 + 32)
def _try_with_headers(self, data: bytes) -> bytes:
"""Попытка с различными заголовками"""
headers_to_try = [
b'', # Без заголовка
b'\x78\x9c', # Zlib заголовок
b'\x78\x01', # Zlib заголовок
b'\x78\xda', # Zlib заголовок
]
for header in headers_to_try:
try:
return zlib.decompress(header + data)
except:
continue
# Последняя попытка - убрать возможные лишние байты
for i in range(min(10, len(data))):
try:
return zlib.decompress(data[i:], -15)
except:
continue
raise ValueError("Все методы с заголовками не сработали")
def get_metadata(self) -> Dict[str, Any]:
"""Получить метаданные пакета"""
data = self.load_data()
return {
'name': data.get('name', 'Неизвестно'),
'pack_application_id': data.get('pack_application_id', 'Неизвестно'),
'timestamp': data.get('timestamp'),
'version': data.get('version', 'Неизвестно')
}
def analyze_records(self) -> RecordStats:
"""Анализ статистики записей"""
data = self.load_data()
records = data.get('records', [])
stats = RecordStats(total=len(records))
for record in records:
if record.get('action') == 'delete':
stats.deletes += 1
if record.get('is_strong_overwrite') is True:
stats.strong_overwrites += 1
return stats
def analyze_tables(self) -> List[TableInfo]:
"""Анализ таблиц и их записей"""
data = self.load_data()
records = data.get('records', [])
tables = {}
for record in records:
table_name = record.get('table_name', 'unknown')
action = record.get('action', 'unknown')
if table_name not in tables:
tables[table_name] = {'count': 0, 'actions': {}}
tables[table_name]['count'] += 1
tables[table_name]['actions'][action] = tables[table_name]['actions'].get(action, 0) + 1
# Сортируем таблицы по количеству записей
sorted_tables = sorted(tables.items(), key=lambda x: x[1]['count'], reverse=True)
return [
TableInfo(
name=table_name,
record_count=table_data['count'],
actions=table_data['actions']
)
for table_name, table_data in sorted_tables
]
def get_actions_summary(self) -> Dict[str, int]:
"""Сводка по действиям"""
data = self.load_data()
records = data.get('records', [])
actions = {}
for record in records:
action = record.get('action', 'unknown')
actions[action] = actions.get(action, 0) + 1
return actions
def get_raw_data_info(self) -> Dict[str, Any]:
"""Получить информацию о сырых данных (для диагностики)"""
try:
with zipfile.ZipFile(self.sop_file_path, 'r') as sop_zip:
data_files = [f for f in sop_zip.namelist() if f.endswith('.data')]
if not data_files:
return {'error': 'No .data file found'}
with sop_zip.open(data_files[0]) as data_file:
raw_data = data_file.read()
return {
'data_file_size': len(raw_data),
'first_10_bytes': raw_data[:10].hex(),
'last_10_bytes': raw_data[-10:].hex(),
'files_in_archive': sop_zip.namelist()
}
except Exception as e:
return {'error': str(e)}
def generate_report(self) -> Dict[str, Any]:
"""Генерация полного отчета"""
metadata = self.get_metadata()
record_stats = self.analyze_records()
tables = self.analyze_tables()
actions = self.get_actions_summary()
return {
'metadata': metadata,
'record_statistics': {
'total_records': record_stats.total,
'delete_operations': record_stats.deletes,
'strong_overwrites': record_stats.strong_overwrites,
'actions_breakdown': actions
},
'tables': [
{
'name': table.name,
'record_count': table.record_count,
'actions': table.actions
}
for table in tables
],
'file_info': {
'file_path': str(self.sop_file_path),
'file_size': self.sop_file_path.stat().st_size,
'analysis_date': datetime.datetime.now().isoformat()
}
}
class OutputFormatter:
"""Форматирование вывода"""
@staticmethod
def print_table(headers: List[str], rows: List[List[str]], title: str = ""):
"""Печать таблицы"""
if title:
print(f"\n{title}")
print("=" * 60)
if not rows:
print("Нет данных")
return
# Вычисляем ширину колонок
col_widths = []
for i, header in enumerate(headers):
max_width = len(header)
for row in rows:
if i < len(row):
max_width = max(max_width, len(str(row[i])))
col_widths.append(max_width + 2)
# Печатаем заголовок
header_line = "".join(f"{header:<{col_widths[i]}}" for i, header in enumerate(headers))
print(header_line)
print("-" * len(header_line))
# Печатаем строки
for row in rows:
line = "".join(f"{str(cell):<{col_widths[i]}}" for i, cell in enumerate(row))
print(line)
@staticmethod
def print_metadata(metadata: Dict[str, Any]):
"""Печать метаданных"""
print("\n📦 МЕТАДАННЫЕ ПАКЕТА")
print("=" * 40)
for key, value in metadata.items():
print(f"{key:>20}: {value}")
@staticmethod
def print_record_stats(stats: RecordStats, actions: Dict[str, int]):
"""Печать статистики записей"""
print("\n📊 СТАТИСТИКА ЗАПИСЕЙ")
print("=" * 40)
print(f"{'Всего записей':>20}: {stats.total}")
print(f"{'Операций удаления':>20}: {stats.deletes}")
print(f"{'Сильных перезаписей':>20}: {stats.strong_overwrites}")
if actions:
print(f"\n{'Действия':>20}:")
for action, count in sorted(actions.items()):
print(f"{'':>22} {action}: {count}")
@staticmethod
def print_tables_summary(tables: List[TableInfo]):
"""Печать сводки по таблицам"""
if not tables:
return
headers = ["Таблица", "Записей", "Действия"]
rows = []
for table in tables:
actions_str = ", ".join(f"{k}:{v}" for k, v in table.actions.items())
rows.append([table.name, str(table.record_count), actions_str])
OutputFormatter.print_table(headers, rows, "🗃️ ТАБЛИЦЫ")
@staticmethod
def print_raw_data_info(info: Dict[str, Any]):
"""Печать информации о сырых данных"""
print("\n🔧 ДИАГНОСТИЧЕСКАЯ ИНФОРМАЦИЯ")
print("=" * 40)
for key, value in info.items():
if key == 'files_in_archive':
print(f"{'Файлы в архиве':>20}:")
for file in value:
print(f"{'':>22} {file}")
else:
print(f"{key:>20}: {value}")
def main():
parser = argparse.ArgumentParser(
description='SOP Analyzer - Анализ пакетов данных .sop',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Примеры использования:
%(prog)s package.sop # Полный отчет
%(prog)s package.sop --metadata # Только метаданные
%(prog)s package.sop --stats # Статистика записей
%(prog)s package.sop --tables # Информация о таблицах
%(prog)s package.sop --json # Вывод в формате JSON
%(prog)s package.sop --debug # Диагностическая информация
"""
)
parser.add_argument('sop_file', help='Путь к .sop файлу')
parser.add_argument('--metadata', action='store_true', help='Показать метаданные пакета')
parser.add_argument('--stats', action='store_true', help='Показать статистику записей')
parser.add_argument('--tables', action='store_true', help='Показать информацию о таблицах')
parser.add_argument('--json', action='store_true', help='Вывод в формате JSON')
parser.add_argument('--debug', action='store_true', help='Диагностическая информация')
parser.add_argument('--verbose', '-v', action='store_true', help='Подробный вывод')
args = parser.parse_args()
# Проверка файла
if not Path(args.sop_file).exists():
print(f"❌ Ошибка: Файл {args.sop_file} не найден")
sys.exit(1)
try:
analyzer = SOPAnalyzer(args.sop_file)
# Режим отладки
if args.debug:
raw_info = analyzer.get_raw_data_info()
OutputFormatter.print_raw_data_info(raw_info)
return
# JSON вывод
if args.json:
report = analyzer.generate_report()
print(json.dumps(report, indent=2, ensure_ascii=False))
return
# Выбор режима отображения
if args.metadata:
OutputFormatter.print_metadata(analyzer.get_metadata())
elif args.stats:
stats = analyzer.analyze_records()
actions = analyzer.get_actions_summary()
OutputFormatter.print_record_stats(stats, actions)
elif args.tables:
tables = analyzer.analyze_tables()
OutputFormatter.print_tables_summary(tables)
else:
# Полный отчет
metadata = analyzer.get_metadata()
stats = analyzer.analyze_records()
actions = analyzer.get_actions_summary()
tables = analyzer.analyze_tables()
OutputFormatter.print_metadata(metadata)
OutputFormatter.print_record_stats(stats, actions)
OutputFormatter.print_tables_summary(tables)
if args.verbose:
print(f"\n📁 Файл: {args.sop_file}")
print(f"📏 Размер: {Path(args.sop_file).stat().st_size} байт")
except Exception as e:
print(f"❌ Ошибка при анализе файла: {e}")
if args.verbose:
import traceback
traceback.print_exc()
# Показываем диагностическую информацию при ошибке
try:
analyzer = SOPAnalyzer(args.sop_file)
raw_info = analyzer.get_raw_data_info()
print("\n💡 Диагностическая информация:")
OutputFormatter.print_raw_data_info(raw_info)
except:
pass
sys.exit(1)
if __name__ == "__main__":
main()