Update public source
This commit is contained in:
@@ -0,0 +1,314 @@
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
import sqlite3
|
||||
import csv
|
||||
|
||||
from vendor_import import VendorImporter
|
||||
|
||||
class EnvironmentChecker:
|
||||
"""
|
||||
Проверка окружения проекта.
|
||||
|
||||
Проверяются:
|
||||
|
||||
- наличие необходимых каталогов;
|
||||
- наличие бинарных файлов;
|
||||
- права на выполнение;
|
||||
- версия hcxdumptool;
|
||||
- наличие базы производителей.
|
||||
|
||||
Основная база проекта не проверяется,
|
||||
так как она может быть создана автоматически
|
||||
при первом запуске.
|
||||
"""
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config
|
||||
):
|
||||
|
||||
self.config = config
|
||||
|
||||
self.log = logging.getLogger(
|
||||
"environment"
|
||||
)
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Главная проверка
|
||||
# ---------------------------------------------------------
|
||||
|
||||
def check(self):
|
||||
|
||||
self.log.info(
|
||||
"Checking application environment..."
|
||||
)
|
||||
|
||||
|
||||
self.check_binary(
|
||||
self.config.hcxdumptool,
|
||||
"hcxdumptool"
|
||||
)
|
||||
|
||||
|
||||
self.check_binary(
|
||||
self.config.hcxpcapngtool,
|
||||
"hcxpcapngtool"
|
||||
)
|
||||
|
||||
|
||||
self.check_hcxdumptool_version()
|
||||
|
||||
|
||||
self.check_vendor_database()
|
||||
|
||||
|
||||
self.log.info(
|
||||
"Environment OK"
|
||||
)
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Проверка бинарника
|
||||
# ---------------------------------------------------------
|
||||
|
||||
def check_binary(
|
||||
self,
|
||||
path,
|
||||
name
|
||||
):
|
||||
|
||||
if not path.exists():
|
||||
|
||||
raise RuntimeError(
|
||||
|
||||
f"{name} not found:\n\n{path}"
|
||||
|
||||
)
|
||||
|
||||
|
||||
if not path.is_file():
|
||||
|
||||
raise RuntimeError(
|
||||
|
||||
f"{name} is not a file:\n\n{path}"
|
||||
|
||||
)
|
||||
|
||||
|
||||
if not os.access(
|
||||
path,
|
||||
os.X_OK
|
||||
):
|
||||
|
||||
raise RuntimeError(
|
||||
|
||||
f"{name} is not executable:\n\n"
|
||||
f"{path}\n\n"
|
||||
f"Run:\n"
|
||||
f"chmod +x {path}"
|
||||
|
||||
)
|
||||
|
||||
|
||||
self.log.info(
|
||||
f"{name}: OK"
|
||||
)
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Проверка версии hcxdumptool
|
||||
# ---------------------------------------------------------
|
||||
|
||||
def check_hcxdumptool_version(self):
|
||||
|
||||
result = subprocess.run(
|
||||
|
||||
[
|
||||
str(
|
||||
self.config.hcxdumptool
|
||||
),
|
||||
"--help"
|
||||
],
|
||||
|
||||
capture_output=True,
|
||||
|
||||
text=True
|
||||
|
||||
)
|
||||
|
||||
|
||||
output = (
|
||||
result.stdout
|
||||
+
|
||||
result.stderr
|
||||
)
|
||||
|
||||
|
||||
if "7." not in output:
|
||||
|
||||
raise RuntimeError(
|
||||
|
||||
"Unsupported hcxdumptool version.\n"
|
||||
"Version 7.x is required."
|
||||
|
||||
)
|
||||
|
||||
|
||||
self.log.info(
|
||||
"hcxdumptool version: OK"
|
||||
)
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Проверка базы производителей
|
||||
# ---------------------------------------------------------
|
||||
|
||||
def check_vendor_database(self):
|
||||
|
||||
database = self.config.vendor_database
|
||||
|
||||
rebuild = False
|
||||
|
||||
if not database.exists():
|
||||
|
||||
self.log.warning(
|
||||
"Vendor database not found"
|
||||
)
|
||||
|
||||
rebuild = True
|
||||
|
||||
elif database.stat().st_size == 0:
|
||||
|
||||
self.log.warning(
|
||||
"Vendor database is empty"
|
||||
)
|
||||
|
||||
rebuild = True
|
||||
|
||||
else:
|
||||
|
||||
try:
|
||||
|
||||
connection = sqlite3.connect(
|
||||
database
|
||||
)
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute(
|
||||
"SELECT name FROM sqlite_master "
|
||||
"WHERE type='table' "
|
||||
"AND name='oui'"
|
||||
)
|
||||
|
||||
if cursor.fetchone() is None:
|
||||
|
||||
raise RuntimeError(
|
||||
"vendors table not found"
|
||||
)
|
||||
|
||||
cursor.execute(
|
||||
|
||||
"SELECT COUNT(*) FROM oui"
|
||||
|
||||
)
|
||||
|
||||
records = cursor.fetchone()[0]
|
||||
|
||||
if records < 50000:
|
||||
|
||||
raise RuntimeError(
|
||||
|
||||
f"Only {records} records"
|
||||
|
||||
)
|
||||
|
||||
cursor.execute(
|
||||
|
||||
"SELECT value "
|
||||
"FROM metadata "
|
||||
"WHERE key='import_status'"
|
||||
|
||||
)
|
||||
|
||||
row = cursor.fetchone()
|
||||
|
||||
if (
|
||||
|
||||
row is None
|
||||
|
||||
or
|
||||
|
||||
row[0] != "ok"
|
||||
|
||||
):
|
||||
|
||||
raise RuntimeError(
|
||||
|
||||
"Invalid import_status"
|
||||
|
||||
)
|
||||
|
||||
connection.close()
|
||||
|
||||
self.log.info(
|
||||
|
||||
f"Vendor database: OK ({records} records)"
|
||||
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
except KeyboardInterrupt:
|
||||
|
||||
self.log.warning(
|
||||
"Download cancelled by user"
|
||||
)
|
||||
|
||||
raise
|
||||
|
||||
except Exception as error:
|
||||
|
||||
self.log.warning(
|
||||
|
||||
f"Vendor database is invalid: {error}"
|
||||
|
||||
)
|
||||
|
||||
rebuild = True
|
||||
|
||||
if rebuild:
|
||||
|
||||
database.unlink(
|
||||
missing_ok=True
|
||||
)
|
||||
|
||||
self.log.info(
|
||||
"Creating vendor database..."
|
||||
)
|
||||
|
||||
importer = VendorImporter(
|
||||
self.config
|
||||
)
|
||||
|
||||
importer.bootstrap()
|
||||
|
||||
if not database.exists():
|
||||
|
||||
raise RuntimeError(
|
||||
|
||||
"Unable to create vendor database."
|
||||
|
||||
)
|
||||
|
||||
self.log.info(
|
||||
"Vendor database created"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user