-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.py
More file actions
143 lines (119 loc) · 4.3 KB
/
Copy pathdiagnostics.py
File metadata and controls
143 lines (119 loc) · 4.3 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
"""Create a privacy-conscious diagnostics ZIP for bug reports."""
from __future__ import annotations
import json
import os
import platform
import shutil
import sqlite3
import sys
import tempfile
import zipfile
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from app_paths import (
BASE_DIR,
CONFIG_PATH,
DATABASE_PATH,
DIAGNOSTICS_DIR,
GUI_LOG_PATH,
CLI_LOG_PATH,
describe_storage,
ensure_app_dirs,
portable_mode_enabled,
)
SENSITIVE_CONFIG_KEYS = {
"output_dir",
"download_dir",
"path",
"proxy",
"username",
"password",
"token",
"api_key",
}
def _sanitize_config(value: Any) -> Any:
"""Remove paths, credentials, and other sensitive configuration values."""
if isinstance(value, dict):
cleaned: dict[str, Any] = {}
for key, child in value.items():
lowered = key.lower()
if any(sensitive in lowered for sensitive in SENSITIVE_CONFIG_KEYS):
cleaned[key] = "<redacted>"
else:
cleaned[key] = _sanitize_config(child)
return cleaned
if isinstance(value, list):
return [_sanitize_config(item) for item in value]
return value
def _database_summary() -> dict[str, Any]:
"""Read schema and record counts without copying the user's full database."""
if not DATABASE_PATH.exists():
return {"exists": False}
result: dict[str, Any] = {
"exists": True,
"size_bytes": DATABASE_PATH.stat().st_size,
"tables": {},
}
with sqlite3.connect(DATABASE_PATH) as connection:
tables = connection.execute(
"""
SELECT name
FROM sqlite_master
WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
ORDER BY name
"""
).fetchall()
for (table_name,) in tables:
# Table names come from sqlite_master rather than user input.
count = connection.execute(
f'SELECT COUNT(*) FROM "{table_name}"'
).fetchone()[0]
result["tables"][table_name] = count
integrity = connection.execute("PRAGMA integrity_check").fetchone()[0]
result["integrity_check"] = integrity
return result
def create_diagnostics_bundle() -> Path:
"""Create and return a ZIP suitable for attaching to a bug report."""
ensure_app_dirs()
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
destination = DIAGNOSTICS_DIR / f"UnityScraper-Diagnostics-{timestamp}.zip"
system_info = {
"created_utc": datetime.now(timezone.utc).isoformat(),
"python": sys.version,
"platform": platform.platform(),
"machine": platform.machine(),
"processor": platform.processor(),
"executable": Path(sys.executable).name,
"frozen": bool(getattr(sys, "frozen", False)),
"portable_mode": portable_mode_enabled(),
"storage": describe_storage(),
"database": _database_summary(),
}
with tempfile.TemporaryDirectory(prefix="unityscraper-diagnostics-") as temp_name:
temp = Path(temp_name)
(temp / "system.json").write_text(
json.dumps(system_info, indent=2, default=str),
encoding="utf-8",
)
if CONFIG_PATH.exists():
try:
config = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
sanitized = _sanitize_config(config)
(temp / "config-sanitized.json").write_text(
json.dumps(sanitized, indent=2, default=str),
encoding="utf-8",
)
except (OSError, json.JSONDecodeError) as exc:
(temp / "config-error.txt").write_text(str(exc), encoding="utf-8")
for source in (GUI_LOG_PATH, CLI_LOG_PATH):
if source.exists():
# Keep the final 500 KiB so diagnostics do not become enormous.
with source.open("rb") as stream:
stream.seek(max(0, source.stat().st_size - 500_000))
data = stream.read()
(temp / source.name).write_bytes(data)
with zipfile.ZipFile(destination, "w", zipfile.ZIP_DEFLATED) as archive:
for path in temp.iterdir():
archive.write(path, path.name)
return destination