-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaosp_tracker.py
More file actions
314 lines (269 loc) · 10.2 KB
/
aosp_tracker.py
File metadata and controls
314 lines (269 loc) · 10.2 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
#!/usr/bin/env python
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "beautifulsoup4",
# "requests",
# ]
# ///
"""AOSP tracker."""
import argparse
import os
import re
import subprocess
from dataclasses import dataclass
from datetime import date
from pathlib import Path
from time import sleep
from bs4 import BeautifulSoup
from requests import Response, Session
from requests.exceptions import RequestException
@dataclass(frozen=True, slots=True)
class Settings:
refs_url: str = "https://android.googlesource.com/platform/frameworks/base/+refs"
refs_base_url: str = "https://android.googlesource.com/platform/frameworks/base/+"
bulletin_index_url: str = "https://source.android.com/docs/security/bulletin"
telegram_chat: str = "@aosptracker"
bot_token: str = ""
git_oauth_token: str = ""
request_timeout: int = 30
retry_attempts: int = 3
retry_delay_seconds: int = 2
@dataclass(frozen=True, slots=True)
class SecurityBulletinInfo:
latest: str
link: str
patch: str
class UpstreamUnavailableError(RuntimeError):
pass
def read_lines(file_path: Path) -> list[str]:
if not file_path.exists():
return []
return [line.strip() for line in file_path.read_text().splitlines() if line.strip()]
def write_lines(file_path: Path, lines: list[str]) -> None:
text = "\n".join(lines)
if text:
text += "\n"
file_path.write_text(text)
def fetch_url(session: Session, url: str, cfg: Settings) -> Response:
last_error: Exception | None = None
for attempt in range(1, cfg.retry_attempts + 1):
try:
response = session.get(url, timeout=cfg.request_timeout)
response.raise_for_status()
return response
except RequestException as error:
last_error = error
if attempt < cfg.retry_attempts:
sleep(cfg.retry_delay_seconds * attempt)
raise UpstreamUnavailableError(
f"GET {url} failed after {cfg.retry_attempts} attempts: {last_error}"
)
def fetch_refs(session: Session, cfg: Settings) -> tuple[list[str], list[str]]:
page = BeautifulSoup(fetch_url(session, cfg.refs_url, cfg).content, "html.parser")
branches: list[str] = []
tags: list[str] = []
for section in page.find_all("div", {"class": "RefList"}):
title = section.find("h3", {"class": "RefList-title"})
if title is None:
continue
values = [item.get_text(strip=True) for item in section.find_all("li")]
if "Branches" in title.get_text():
branches.extend(values)
elif "Tags" in title.get_text():
tags.extend(values)
if not branches or not tags:
raise RuntimeError("Failed to parse refs page (empty branches or tags)")
return branches, tags
def post_to_telegram(session: Session, message: str, cfg: Settings) -> None:
if not cfg.bot_token:
raise RuntimeError("bottoken is required")
params = (
("chat_id", cfg.telegram_chat),
("text", message),
("parse_mode", "Markdown"),
("disable_web_page_preview", "yes"),
)
telegram_url = f"https://api.telegram.org/bot{cfg.bot_token}/sendMessage"
response = session.post(telegram_url, params=params, timeout=cfg.request_timeout)
response.raise_for_status()
def update_refs_files(
session: Session,
refs_name: str,
values: list[str],
cfg: Settings,
send_telegram: bool,
max_telegram_messages: int,
) -> None:
current_path = Path(refs_name)
old_path = Path(f"{refs_name}_old")
changes_path = Path(f"{refs_name}_changes")
previous_values = read_lines(current_path)
if current_path.exists():
current_path.replace(old_path)
elif not old_path.exists():
old_path.write_text("")
write_lines(current_path, values)
previous_values_set = set(previous_values)
changes = [value for value in values if value and value not in previous_values_set]
write_lines(changes_path, changes)
if not send_telegram:
return
if len(changes) > max_telegram_messages:
print(
f"Skipped Telegram for {refs_name}: {len(changes)} changes exceeds cap ({max_telegram_messages})"
)
return
ref_type = "branch" if refs_name == "branches" else "tag"
for ref_name in changes:
message = f"New {ref_type} detected! `{ref_name}` [Check Here]({cfg.refs_base_url}/{ref_name})"
post_to_telegram(session, message, cfg)
def fetch_security_bulletin(session: Session, cfg: Settings) -> SecurityBulletinInfo:
bulletin_page = BeautifulSoup(
fetch_url(session, cfg.bulletin_index_url, cfg).content, "html.parser"
)
bulletin_links: list[tuple[str, str]] = []
for item in bulletin_page.find_all("a", href=True):
href_attr = item.get("href")
if not isinstance(href_attr, str):
continue
href = href_attr
match = re.search(r"^/docs/security/bulletin/\d{4}/(\d{4}-\d{2}-\d{2})/?$", href)
if match:
bulletin_links.append((match.group(1), href))
if not bulletin_links:
raise RuntimeError("Failed to parse security bulletin links")
latest, latest_path = max(bulletin_links)
link = (
latest_path
if latest_path.startswith("http")
else f"https://source.android.com{latest_path}"
)
detail_text = BeautifulSoup(
fetch_url(session, link, cfg).content, "html.parser"
).get_text(" ", strip=True)
patch_levels = sorted(set(re.findall(r"\b\d{4}-\d{2}-(?:01|05)\b", detail_text)))
patch = " | ".join(patch_levels) if patch_levels else latest
return SecurityBulletinInfo(latest=latest, link=link, patch=patch)
def fetch_bulletin_links(session: Session, cfg: Settings, bulletin_date: str) -> list[str]:
bulletin_page = BeautifulSoup(
fetch_url(session, cfg.bulletin_index_url, cfg).content, "html.parser"
)
return sorted(
{
f"https://source.android.com{href}"
for item in bulletin_page.find_all("a", href=True)
if isinstance((href := item.get("href")), str)
and re.search(rf"^/docs/security/bulletin(?:/[^/]+)?/\d{{4}}/{re.escape(bulletin_date)}/?$", href)
}
)
def update_security_patch(
session: Session, bulletin: SecurityBulletinInfo, cfg: Settings, send_telegram: bool
) -> None:
current_path = Path("security_patch")
old_path = Path("security_patch_old")
if not current_path.exists():
current_path.write_text(bulletin.latest)
return
previous_value = current_path.read_text().strip()
current_path.replace(old_path)
current_path.write_text(bulletin.latest)
if bulletin.latest == previous_value:
return
if not send_telegram:
return
bulletin_links = "\n".join(f"- {url}" for url in fetch_bulletin_links(session, cfg, bulletin.latest))
message = (
f"New Security Patch detected! [{bulletin.latest}]({bulletin.link})\n"
f"__Patch__: {bulletin.patch}\n"
f"__Bulletins__:\n{bulletin_links}\n"
)
post_to_telegram(session, message, cfg)
def git_commit_push(cfg: Settings) -> None:
today = str(date.today())
tracked_files = ["branches", "tags", "security_patch"]
subprocess.run(["git", "add", *tracked_files], check=True)
staged_diff = subprocess.run(
["git", "diff", "--cached", "--quiet", "--", *tracked_files], check=False
)
if staged_diff.returncode == 0:
print("No tracked changes to commit")
return
if staged_diff.returncode != 1:
raise RuntimeError("Failed to inspect staged git changes")
subprocess.run(
[
"git",
"-c",
"user.name=XiaomiFirmwareUpdater",
"-c",
"user.email=xiaomifirmwareupdater@gmail.com",
"commit",
"-m",
f"[skip ci] sync: {today}",
],
check=True,
)
if cfg.git_oauth_token:
push_command = [
"git",
"push",
"-q",
f"https://{cfg.git_oauth_token}@github.com/androidtrackers/aosp-tracker.git",
"HEAD:master",
]
elif os.environ.get("GITHUB_ACTIONS") == "true":
push_command = ["git", "push", "-q", "origin", "HEAD:master"]
else:
raise RuntimeError("XFU is required outside GitHub Actions")
subprocess.run(push_command, check=True)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--parse-only", action="store_true")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--send-telegram", action="store_true")
parser.add_argument("--push", action="store_true")
parser.add_argument("--max-telegram-messages", type=int, default=20)
return parser.parse_args()
def main() -> int:
args = parse_args()
cfg = Settings(
bot_token=os.environ.get("bottoken", ""),
git_oauth_token=os.environ.get("XFU", ""),
)
send_telegram = args.send_telegram and not args.dry_run
push = args.push and not args.dry_run
with Session() as session:
try:
branches, tags = fetch_refs(session, cfg)
bulletin = fetch_security_bulletin(session, cfg)
if args.parse_only:
print(
f"Parsed branches={len(branches)} tags={len(tags)} latest_security_patch={bulletin.latest}"
)
return 0
update_refs_files(
session,
"branches",
branches,
cfg,
send_telegram,
args.max_telegram_messages,
)
update_refs_files(
session, "tags", tags, cfg, send_telegram, args.max_telegram_messages
)
update_security_patch(session, bulletin, cfg, send_telegram)
if push:
git_commit_push(cfg)
return 0
except UpstreamUnavailableError as error:
print(f"Run skipped due to upstream outage: {error}")
if os.environ.get("GITHUB_ACTIONS") == "true":
return 0
return 1
except Exception as error:
print(f"Run failed safely: {error}")
return 1
if __name__ == "__main__":
raise SystemExit(main())