lupaxa-github-intel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lupaxa/github_intel/__init__.py +13 -0
- lupaxa/github_intel/__main__.py +8 -0
- lupaxa/github_intel/app.py +493 -0
- lupaxa/github_intel/checks/__init__.py +1 -0
- lupaxa/github_intel/checks/base.py +17 -0
- lupaxa/github_intel/checks/releases.py +59 -0
- lupaxa/github_intel/checks/workflows.py +34 -0
- lupaxa/github_intel/cli.py +699 -0
- lupaxa/github_intel/config/__init__.py +1 -0
- lupaxa/github_intel/config/defaults.py +150 -0
- lupaxa/github_intel/config/editor.py +316 -0
- lupaxa/github_intel/config/loader.py +412 -0
- lupaxa/github_intel/config/models.py +310 -0
- lupaxa/github_intel/config/paths.py +84 -0
- lupaxa/github_intel/config/profiles.py +143 -0
- lupaxa/github_intel/constants.py +54 -0
- lupaxa/github_intel/dates.py +179 -0
- lupaxa/github_intel/errors.py +72 -0
- lupaxa/github_intel/github/__init__.py +1 -0
- lupaxa/github_intel/github/auth.py +40 -0
- lupaxa/github_intel/github/client.py +239 -0
- lupaxa/github_intel/github/rate_limit.py +107 -0
- lupaxa/github_intel/layout.py +141 -0
- lupaxa/github_intel/logging_setup.py +51 -0
- lupaxa/github_intel/present.py +361 -0
- lupaxa/github_intel/reports/__init__.py +1 -0
- lupaxa/github_intel/reports/render.py +113 -0
- lupaxa/github_intel/storage/__init__.py +1 -0
- lupaxa/github_intel/storage/database.py +419 -0
- lupaxa/github_intel/sync/__init__.py +1 -0
- lupaxa/github_intel/sync/engine.py +671 -0
- lupaxa/github_intel/sync/summary.py +308 -0
- lupaxa/github_intel/tui/__init__.py +1 -0
- lupaxa/github_intel/tui/application.py +487 -0
- lupaxa/github_intel/views.py +896 -0
- lupaxa_github_intel-0.1.0.dist-info/METADATA +180 -0
- lupaxa_github_intel-0.1.0.dist-info/RECORD +41 -0
- lupaxa_github_intel-0.1.0.dist-info/WHEEL +5 -0
- lupaxa_github_intel-0.1.0.dist-info/entry_points.txt +2 -0
- lupaxa_github_intel-0.1.0.dist-info/licenses/LICENCE +21 -0
- lupaxa_github_intel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
"""
|
|
2
|
+
SQLite cache.
|
|
3
|
+
|
|
4
|
+
One lock serialises writes. Network code stays outside the lock.
|
|
5
|
+
Expired rows are kept until a newer result or an explicit clear.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
import time
|
|
16
|
+
|
|
17
|
+
import aiosqlite
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"Database",
|
|
21
|
+
"Document",
|
|
22
|
+
"StoredCheck",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
_SCHEMA = """
|
|
26
|
+
CREATE TABLE IF NOT EXISTS documents (
|
|
27
|
+
repo TEXT NOT NULL,
|
|
28
|
+
category TEXT NOT NULL,
|
|
29
|
+
body TEXT,
|
|
30
|
+
etag TEXT,
|
|
31
|
+
fetched_at REAL,
|
|
32
|
+
error TEXT,
|
|
33
|
+
PRIMARY KEY (repo, category)
|
|
34
|
+
);
|
|
35
|
+
CREATE TABLE IF NOT EXISTS check_results (
|
|
36
|
+
repo TEXT NOT NULL,
|
|
37
|
+
name TEXT NOT NULL,
|
|
38
|
+
status TEXT NOT NULL,
|
|
39
|
+
summary TEXT NOT NULL,
|
|
40
|
+
failure INTEGER NOT NULL,
|
|
41
|
+
checked_at REAL NOT NULL,
|
|
42
|
+
PRIMARY KEY (repo, name)
|
|
43
|
+
);
|
|
44
|
+
CREATE TABLE IF NOT EXISTS sync_runs (
|
|
45
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
46
|
+
started_at REAL NOT NULL,
|
|
47
|
+
finished_at REAL,
|
|
48
|
+
stats_json TEXT NOT NULL
|
|
49
|
+
);
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True)
|
|
54
|
+
class Document:
|
|
55
|
+
"""One cached API payload."""
|
|
56
|
+
|
|
57
|
+
repo: str
|
|
58
|
+
category: str
|
|
59
|
+
body: str | None
|
|
60
|
+
etag: str | None
|
|
61
|
+
fetched_at: float | None
|
|
62
|
+
error: str | None
|
|
63
|
+
|
|
64
|
+
def json_body(self) -> object:
|
|
65
|
+
"""
|
|
66
|
+
Return the parsed JSON body.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
Decoded JSON, or None when the body is empty.
|
|
70
|
+
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
if self.body is None or self.body == "":
|
|
74
|
+
return None
|
|
75
|
+
return json.loads(self.body)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(frozen=True)
|
|
79
|
+
class StoredCheck:
|
|
80
|
+
"""One stored check row."""
|
|
81
|
+
|
|
82
|
+
repo: str
|
|
83
|
+
name: str
|
|
84
|
+
status: str
|
|
85
|
+
summary: str
|
|
86
|
+
failure: bool
|
|
87
|
+
checked_at: float
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class Database:
|
|
91
|
+
"""Async SQLite cache with a single in-process writer."""
|
|
92
|
+
|
|
93
|
+
def __init__(self, path: Path) -> None:
|
|
94
|
+
"""
|
|
95
|
+
Point at a database file. The file is created on first open.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
path: SQLite path.
|
|
99
|
+
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
self.path = path
|
|
103
|
+
self._lock = asyncio.Lock()
|
|
104
|
+
self._db: aiosqlite.Connection | None = None
|
|
105
|
+
|
|
106
|
+
async def open(self) -> None:
|
|
107
|
+
"""Create the file, enable WAL, and apply the schema."""
|
|
108
|
+
|
|
109
|
+
async with self._lock:
|
|
110
|
+
if self._db is not None:
|
|
111
|
+
return
|
|
112
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
113
|
+
if os.name != "nt":
|
|
114
|
+
self.path.parent.chmod(0o700)
|
|
115
|
+
self._db = await aiosqlite.connect(self.path)
|
|
116
|
+
self._db.row_factory = aiosqlite.Row
|
|
117
|
+
await self._db.execute("PRAGMA journal_mode=WAL")
|
|
118
|
+
await self._db.executescript(_SCHEMA)
|
|
119
|
+
await self._db.commit()
|
|
120
|
+
if os.name != "nt":
|
|
121
|
+
self.path.chmod(0o600)
|
|
122
|
+
|
|
123
|
+
async def close(self) -> None:
|
|
124
|
+
"""Close the connection."""
|
|
125
|
+
|
|
126
|
+
async with self._lock:
|
|
127
|
+
if self._db is not None:
|
|
128
|
+
await self._db.close()
|
|
129
|
+
self._db = None
|
|
130
|
+
|
|
131
|
+
async def get(self, repo: str, category: str) -> Document | None:
|
|
132
|
+
"""
|
|
133
|
+
Return one cached document.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
repo: Repository identifier.
|
|
137
|
+
category: Cache category.
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
The row, or None.
|
|
141
|
+
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
await self.open()
|
|
145
|
+
async with self._lock:
|
|
146
|
+
cursor = await self._connection().execute(
|
|
147
|
+
"""
|
|
148
|
+
SELECT repo, category, body, etag, fetched_at, error
|
|
149
|
+
FROM documents WHERE repo = ? AND category = ?
|
|
150
|
+
""",
|
|
151
|
+
(repo, category),
|
|
152
|
+
)
|
|
153
|
+
row = await cursor.fetchone()
|
|
154
|
+
if row is None:
|
|
155
|
+
return None
|
|
156
|
+
return _document(row)
|
|
157
|
+
|
|
158
|
+
async def save(
|
|
159
|
+
self,
|
|
160
|
+
repo: str,
|
|
161
|
+
category: str,
|
|
162
|
+
*,
|
|
163
|
+
body: object | None = None,
|
|
164
|
+
etag: str | None,
|
|
165
|
+
error: str | None,
|
|
166
|
+
keep_body: bool,
|
|
167
|
+
) -> None:
|
|
168
|
+
"""
|
|
169
|
+
Insert or update a document and refresh its timestamp.
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
repo: Repository identifier.
|
|
173
|
+
category: Cache category.
|
|
174
|
+
body: JSON value. Ignored when ``keep_body`` is true.
|
|
175
|
+
etag: New ETag.
|
|
176
|
+
error: Error text, or None on success.
|
|
177
|
+
keep_body: Preserve the previous body (HTTP 304).
|
|
178
|
+
|
|
179
|
+
"""
|
|
180
|
+
|
|
181
|
+
await self.open()
|
|
182
|
+
encoded = None if body is None else json.dumps(body)
|
|
183
|
+
async with self._lock:
|
|
184
|
+
if keep_body:
|
|
185
|
+
await self._connection().execute(
|
|
186
|
+
"""
|
|
187
|
+
UPDATE documents
|
|
188
|
+
SET etag = ?, fetched_at = ?, error = NULL
|
|
189
|
+
WHERE repo = ? AND category = ?
|
|
190
|
+
""",
|
|
191
|
+
(etag, time.time(), repo, category),
|
|
192
|
+
)
|
|
193
|
+
else:
|
|
194
|
+
await self._connection().execute(
|
|
195
|
+
"""
|
|
196
|
+
INSERT INTO documents (
|
|
197
|
+
repo, category, body, etag, fetched_at, error
|
|
198
|
+
)
|
|
199
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
200
|
+
ON CONFLICT(repo, category) DO UPDATE SET
|
|
201
|
+
body = excluded.body,
|
|
202
|
+
etag = excluded.etag,
|
|
203
|
+
fetched_at = excluded.fetched_at,
|
|
204
|
+
error = excluded.error
|
|
205
|
+
""",
|
|
206
|
+
(repo, category, encoded, etag, time.time(), error),
|
|
207
|
+
)
|
|
208
|
+
await self._connection().commit()
|
|
209
|
+
|
|
210
|
+
async def save_checks(self, repo: str, checks: list[StoredCheck]) -> None:
|
|
211
|
+
"""
|
|
212
|
+
Replace the stored checks for one repository.
|
|
213
|
+
|
|
214
|
+
Args:
|
|
215
|
+
repo: Repository identifier.
|
|
216
|
+
checks: New rows.
|
|
217
|
+
|
|
218
|
+
"""
|
|
219
|
+
|
|
220
|
+
await self.open()
|
|
221
|
+
async with self._lock:
|
|
222
|
+
await self._connection().execute(
|
|
223
|
+
"DELETE FROM check_results WHERE repo = ?",
|
|
224
|
+
(repo,),
|
|
225
|
+
)
|
|
226
|
+
await self._connection().executemany(
|
|
227
|
+
"""
|
|
228
|
+
INSERT INTO check_results (
|
|
229
|
+
repo, name, status, summary, failure, checked_at
|
|
230
|
+
)
|
|
231
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
232
|
+
""",
|
|
233
|
+
[
|
|
234
|
+
(
|
|
235
|
+
item.repo,
|
|
236
|
+
item.name,
|
|
237
|
+
item.status,
|
|
238
|
+
item.summary,
|
|
239
|
+
int(item.failure),
|
|
240
|
+
item.checked_at,
|
|
241
|
+
)
|
|
242
|
+
for item in checks
|
|
243
|
+
],
|
|
244
|
+
)
|
|
245
|
+
await self._connection().commit()
|
|
246
|
+
|
|
247
|
+
async def checks_for(self, repo: str) -> list[StoredCheck]:
|
|
248
|
+
"""
|
|
249
|
+
Return stored checks for one repository.
|
|
250
|
+
|
|
251
|
+
Args:
|
|
252
|
+
repo: Repository identifier.
|
|
253
|
+
|
|
254
|
+
Returns:
|
|
255
|
+
Check rows.
|
|
256
|
+
|
|
257
|
+
"""
|
|
258
|
+
|
|
259
|
+
await self.open()
|
|
260
|
+
async with self._lock:
|
|
261
|
+
cursor = await self._connection().execute(
|
|
262
|
+
"""
|
|
263
|
+
SELECT repo, name, status, summary, failure, checked_at
|
|
264
|
+
FROM check_results WHERE repo = ? ORDER BY name
|
|
265
|
+
""",
|
|
266
|
+
(repo,),
|
|
267
|
+
)
|
|
268
|
+
rows = await cursor.fetchall()
|
|
269
|
+
return [_check(row) for row in rows]
|
|
270
|
+
|
|
271
|
+
async def documents_for(self, repo: str) -> list[Document]:
|
|
272
|
+
"""
|
|
273
|
+
Return every cached document for one repository.
|
|
274
|
+
|
|
275
|
+
Args:
|
|
276
|
+
repo: Repository identifier.
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
Document rows.
|
|
280
|
+
|
|
281
|
+
"""
|
|
282
|
+
|
|
283
|
+
await self.open()
|
|
284
|
+
async with self._lock:
|
|
285
|
+
cursor = await self._connection().execute(
|
|
286
|
+
"""
|
|
287
|
+
SELECT repo, category, body, etag, fetched_at, error
|
|
288
|
+
FROM documents WHERE repo = ? ORDER BY category
|
|
289
|
+
""",
|
|
290
|
+
(repo,),
|
|
291
|
+
)
|
|
292
|
+
rows = await cursor.fetchall()
|
|
293
|
+
return [_document(row) for row in rows]
|
|
294
|
+
|
|
295
|
+
async def clear(self, repo: str | None = None) -> None:
|
|
296
|
+
"""
|
|
297
|
+
Delete cached documents and checks. A full clear also deletes sync history.
|
|
298
|
+
|
|
299
|
+
Args:
|
|
300
|
+
repo: Limit the delete to one repository. None clears everything.
|
|
301
|
+
|
|
302
|
+
"""
|
|
303
|
+
|
|
304
|
+
await self.open()
|
|
305
|
+
async with self._lock:
|
|
306
|
+
if repo is None:
|
|
307
|
+
await self._connection().execute("DELETE FROM documents")
|
|
308
|
+
await self._connection().execute("DELETE FROM check_results")
|
|
309
|
+
await self._connection().execute("DELETE FROM sync_runs")
|
|
310
|
+
else:
|
|
311
|
+
await self._connection().execute(
|
|
312
|
+
"DELETE FROM documents WHERE repo = ?",
|
|
313
|
+
(repo,),
|
|
314
|
+
)
|
|
315
|
+
await self._connection().execute(
|
|
316
|
+
"DELETE FROM check_results WHERE repo = ?",
|
|
317
|
+
(repo,),
|
|
318
|
+
)
|
|
319
|
+
await self._connection().commit()
|
|
320
|
+
|
|
321
|
+
async def vacuum(self) -> None:
|
|
322
|
+
"""Rebuild the database file."""
|
|
323
|
+
|
|
324
|
+
await self.open()
|
|
325
|
+
async with self._lock:
|
|
326
|
+
await self._connection().execute("VACUUM")
|
|
327
|
+
|
|
328
|
+
async def counts(self) -> dict[str, int]:
|
|
329
|
+
"""
|
|
330
|
+
Return row counts.
|
|
331
|
+
|
|
332
|
+
Returns:
|
|
333
|
+
Document, check, and sync-run counts.
|
|
334
|
+
|
|
335
|
+
"""
|
|
336
|
+
|
|
337
|
+
await self.open()
|
|
338
|
+
async with self._lock:
|
|
339
|
+
documents = await self._count("documents")
|
|
340
|
+
checks = await self._count("check_results")
|
|
341
|
+
runs = await self._count("sync_runs")
|
|
342
|
+
return {"documents": documents, "checks": checks, "sync_runs": runs}
|
|
343
|
+
|
|
344
|
+
async def latest_sync_started(self) -> float | None:
|
|
345
|
+
"""
|
|
346
|
+
Return when the newest synchronisation started.
|
|
347
|
+
|
|
348
|
+
Returns:
|
|
349
|
+
Epoch seconds, or None when no run has been recorded.
|
|
350
|
+
|
|
351
|
+
"""
|
|
352
|
+
|
|
353
|
+
await self.open()
|
|
354
|
+
async with self._lock:
|
|
355
|
+
cursor = await self._connection().execute(
|
|
356
|
+
"SELECT started_at FROM sync_runs ORDER BY started_at DESC LIMIT 1",
|
|
357
|
+
)
|
|
358
|
+
row = await cursor.fetchone()
|
|
359
|
+
return None if row is None else float(row[0])
|
|
360
|
+
|
|
361
|
+
async def record_run(
|
|
362
|
+
self,
|
|
363
|
+
started: float,
|
|
364
|
+
finished: float,
|
|
365
|
+
stats: dict[str, object],
|
|
366
|
+
) -> None:
|
|
367
|
+
"""
|
|
368
|
+
Append one synchronisation history row.
|
|
369
|
+
|
|
370
|
+
Args:
|
|
371
|
+
started: Start timestamp.
|
|
372
|
+
finished: Finish timestamp.
|
|
373
|
+
stats: JSON-serialisable statistics.
|
|
374
|
+
|
|
375
|
+
"""
|
|
376
|
+
|
|
377
|
+
await self.open()
|
|
378
|
+
async with self._lock:
|
|
379
|
+
await self._connection().execute(
|
|
380
|
+
"""
|
|
381
|
+
INSERT INTO sync_runs (started_at, finished_at, stats_json)
|
|
382
|
+
VALUES (?, ?, ?)
|
|
383
|
+
""",
|
|
384
|
+
(started, finished, json.dumps(stats)),
|
|
385
|
+
)
|
|
386
|
+
await self._connection().commit()
|
|
387
|
+
|
|
388
|
+
def _connection(self) -> aiosqlite.Connection:
|
|
389
|
+
if self._db is None:
|
|
390
|
+
message = "Database is not open."
|
|
391
|
+
raise RuntimeError(message)
|
|
392
|
+
return self._db
|
|
393
|
+
|
|
394
|
+
async def _count(self, table: str) -> int:
|
|
395
|
+
cursor = await self._connection().execute(f"SELECT COUNT(*) FROM {table}") # noqa: S608
|
|
396
|
+
row = await cursor.fetchone()
|
|
397
|
+
return 0 if row is None else int(row[0])
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def _document(row: aiosqlite.Row) -> Document:
|
|
401
|
+
return Document(
|
|
402
|
+
repo=str(row["repo"]),
|
|
403
|
+
category=str(row["category"]),
|
|
404
|
+
body=None if row["body"] is None else str(row["body"]),
|
|
405
|
+
etag=None if row["etag"] is None else str(row["etag"]),
|
|
406
|
+
fetched_at=None if row["fetched_at"] is None else float(row["fetched_at"]),
|
|
407
|
+
error=None if row["error"] is None else str(row["error"]),
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _check(row: aiosqlite.Row) -> StoredCheck:
|
|
412
|
+
return StoredCheck(
|
|
413
|
+
repo=str(row["repo"]),
|
|
414
|
+
name=str(row["name"]),
|
|
415
|
+
status=str(row["status"]),
|
|
416
|
+
summary=str(row["summary"]),
|
|
417
|
+
failure=bool(row["failure"]),
|
|
418
|
+
checked_at=float(row["checked_at"]),
|
|
419
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Synchronisation."""
|