-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathcheck_build.py
More file actions
289 lines (240 loc) · 10.8 KB
/
Copy pathcheck_build.py
File metadata and controls
289 lines (240 loc) · 10.8 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
#!/usr/bin/env python3
"""check_build.py — Update data/win_builds.json from Microsoft release health pages.
Scrapes the official Microsoft documentation to discover new Windows build
numbers and adds them to the local data/win_builds.json dictionary, organized
by OS family (Windows 10, Windows 11, Windows Server XXXX, etc.).
Sources:
- Windows 10: https://learn.microsoft.com/en-us/windows/release-health/release-information
- Windows 11: https://learn.microsoft.com/en-us/windows/release-health/windows11-release-information
- Windows Server: https://learn.microsoft.com/en-us/windows/release-health/windows-server-release-info
Usage:
python check_build.py # update data/win_builds.json
python check_build.py --dry-run # show what would change, don't write
python check_build.py --wipe-clients # delete data/known_clients.json (reset IP cache)
"""
import json
import os
import re
import datetime
import argparse
from urllib.request import urlopen, Request
from urllib.error import URLError
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
_DATA_DIR = os.path.join(_SCRIPT_DIR, "data")
_BUILDS_JSON = os.path.join(_DATA_DIR, "win_builds.json")
_CLIENTS_JSON = os.path.join(_DATA_DIR, "known_clients.json")
# Client sources: scrape "Version XXX (OS build NNNNN)" patterns
CLIENT_SOURCES = [
{
"url": "https://learn.microsoft.com/en-us/windows/release-health/release-information",
"section": "Windows 10",
},
{
"url": "https://learn.microsoft.com/en-us/windows/release-health/windows11-release-information",
"section": "Windows 11",
},
]
# Server source: scrape "Windows Server XXXX (OS build NNNNN)" headers
SERVER_SOURCE = {
"url": "https://learn.microsoft.com/en-us/windows/release-health/windows-server-release-info",
}
HEADERS = {
"User-Agent": "check_build/1.0 (pywsus; build dictionary updater)",
"Accept": "text/html",
}
# ---------------------------------------------------------------------------
# Scraping
# ---------------------------------------------------------------------------
def fetch_page(url):
"""Fetch a page and return its HTML as a string."""
req = Request(url, headers=HEADERS)
try:
with urlopen(req, timeout=15) as resp:
return resp.read().decode("utf-8", errors="replace")
except URLError as e:
print(f" [!] Failed to fetch {url}: {e}")
return ""
def extract_client_builds(html_content):
"""Extract { base_build: version_label } from a Win10/Win11 release health page.
Looks for patterns like 'Version 22H2 (OS build 19045)'.
"""
results = {}
# Pattern 1: "Version 22H2 (OS build 19045)" style headers
for m in re.finditer(
r'Version\s+([\w.]+)\s*\(OS\s+build\s+(\d{5,})\)',
html_content, re.IGNORECASE
):
results[m.group(2)] = m.group(1)
# Pattern 2: table rows with version + build pairs
version_pat = re.compile(
r'<td[^>]*>\s*(?:Version\s+)?(1[5-9]\d{2}|2[0-9]H[12]|2[0-9]{3})\s*</td>',
re.IGNORECASE
)
build_pat = re.compile(r'<td[^>]*>\s*(\d{5,})(?:\.\d+)?\s*</td>')
for row in re.finditer(r'<tr[^>]*>(.*?)</tr>', html_content, re.DOTALL):
row_html = row.group(1)
ver_m = version_pat.search(row_html)
bld_m = build_pat.search(row_html)
if ver_m and bld_m:
build = bld_m.group(1)
if build not in results:
results[build] = ver_m.group(1)
return results
def extract_server_builds(html_content):
"""Extract { section_name: { base_build: version } } from the Server page.
Looks for detail headers like 'Windows Server 2025 (OS build 26100)'
and main table rows like 'Windows Server 2019 (version 1809)'.
"""
results = {} # { "Windows Server 2025": { "26100": "24H2" }, ... }
# Step 1: extract section → base_build from detail headers
# Pattern: "Windows Server XXXX (OS build NNNNN)"
section_builds = {}
for m in re.finditer(
r'Windows\s+Server\s+(\d{4})\s*\(OS\s+build\s+(\d{5,})\)',
html_content, re.IGNORECASE
):
name = f"Windows Server {m.group(1)}"
base_build = m.group(2)
section_builds[name] = base_build
# Step 2: extract version from main table
# Pattern: "Windows Server XXXX (version YYYY)" or just "Windows Server XXXX"
version_map = {}
for m in re.finditer(
r'Windows\s+Server\s+(\d{4})\s*\(version\s+([\w.]+)\)',
html_content, re.IGNORECASE
):
version_map[f"Windows Server {m.group(1)}"] = m.group(2)
# Step 3: for servers without explicit version, try to derive it
# Server 2025 = build 26100 = "24H2", Server 2022 = build 20348 = "21H2"
_KNOWN_SERVER_VERSIONS = {
"Windows Server 2025": "24H2",
"Windows Server 2022": "21H2",
}
# Combine
for name, base_build in section_builds.items():
version = version_map.get(name, _KNOWN_SERVER_VERSIONS.get(name, ""))
if name not in results:
results[name] = {}
if version:
results[name][base_build] = version
return results
# ---------------------------------------------------------------------------
# JSON management
# ---------------------------------------------------------------------------
def load_json():
"""Load the existing data/win_builds.json or create a skeleton."""
if os.path.exists(_BUILDS_JSON):
with open(_BUILDS_JSON, "r", encoding="utf-8") as f:
return json.load(f)
return {
"_comment": "OS family → { build_number: version }. Updated by check_build.py",
"_updated": "",
"_sources": [s["url"] for s in CLIENT_SOURCES] + [SERVER_SOURCE["url"]],
}
def save_json(data):
"""Write the updated JSON file with sorted builds per section."""
data["_updated"] = datetime.date.today().isoformat()
for key, val in data.items():
if key.startswith("_") or not isinstance(val, dict):
continue
data[key] = dict(sorted(val.items(), key=lambda x: int(x[0])))
os.makedirs(_DATA_DIR, exist_ok=True)
with open(_BUILDS_JSON, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
f.write("\n")
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Update data/win_builds.json from Microsoft release health pages.",
epilog=(
"Examples:\n"
" python check_build.py # fetch latest builds\n"
" python check_build.py --dry-run # preview without saving\n"
" python check_build.py --wipe-clients # reset known client IPs\n"
),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("--dry-run", action="store_true",
help="Show changes without writing to disk")
parser.add_argument("--wipe-clients", action="store_true",
help="Reset data/known_clients.json (clear all known client IPs)")
args = parser.parse_args()
# --- Wipe clients if requested ---
if args.wipe_clients:
os.makedirs(_DATA_DIR, exist_ok=True)
empty = {"_comment": "Known WSUS clients — { ip: {build, arch, os_desc} }. Auto-populated by pywsus."}
with open(_CLIENTS_JSON, "w", encoding="utf-8") as f:
json.dump(empty, f, indent=2, ensure_ascii=False)
f.write("\n")
print(f"[*] Cleared {_CLIENTS_JSON}")
return
data = load_json()
added = {}
# Count existing
existing_count = sum(len(v) for k, v in data.items()
if not k.startswith("_") and isinstance(v, dict))
print(f"[*] Current dictionary: {existing_count} builds in {_BUILDS_JSON}")
print()
# --- Client builds (Windows 10, Windows 11) ---
for source in CLIENT_SOURCES:
url = source["url"]
section = source["section"]
print(f"[*] Fetching {section} release info...")
html_content = fetch_page(url)
if not html_content:
continue
discovered = extract_client_builds(html_content)
print(f" Found {len(discovered)} build(s) on page")
existing = data.setdefault(section, {})
for build, version in discovered.items():
if build not in existing:
added[f"{section}/{build}"] = version
existing[build] = version
print(f" [+] NEW: {section} build {build} → version {version}")
elif existing[build] != version:
print(f" [~] UPDATE: {section} build {build}: "
f"{existing[build]} → {version}")
existing[build] = version
added[f"{section}/{build}"] = version
# --- Server builds ---
print(f"[*] Fetching Windows Server release info...")
html_content = fetch_page(SERVER_SOURCE["url"])
if html_content:
server_results = extract_server_builds(html_content)
total_server = sum(len(v) for v in server_results.values())
print(f" Found {total_server} build(s) across {len(server_results)} server edition(s)")
for section, builds in server_results.items():
existing = data.setdefault(section, {})
for build, version in builds.items():
if build not in existing:
added[f"{section}/{build}"] = version
existing[build] = version
print(f" [+] NEW: {section} build {build} → version {version}")
elif existing[build] != version:
print(f" [~] UPDATE: {section} build {build}: "
f"{existing[build]} → {version}")
existing[build] = version
added[f"{section}/{build}"] = version
print()
if not added:
print("[*] Dictionary is already up to date, no new builds found.")
else:
print(f"[+] {len(added)} new/updated build(s):")
for key, version in sorted(added.items()):
print(f" {key} → {version}")
if added and not args.dry_run:
save_json(data)
print(f"\n[*] Saved to {_BUILDS_JSON}")
elif added and args.dry_run:
print(f"\n[*] Dry run — changes NOT saved")
total = sum(len(v) for k, v in data.items()
if not k.startswith("_") and isinstance(v, dict))
families = sum(1 for k in data if not k.startswith("_") and isinstance(data[k], dict))
print(f"\n[*] Total: {total} builds / {families} OS families")
if __name__ == "__main__":
main()