Skip to content

Instantly share code, notes, and snippets.

@carstenandrich
Created April 30, 2020 15:48
Show Gist options
  • Save carstenandrich/c331d557520b8a0e7f44689ca257f805 to your computer and use it in GitHub Desktop.
Save carstenandrich/c331d557520b8a0e7f44689ca257f805 to your computer and use it in GitHub Desktop.
Extract list of Windows system error codes from docs.microsoft.com
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright 2020 Carsten Andrich <base64.b64decode("Y2Fyc3RlbiBhdCBhbmRyaWNoIGRvdCBuYW1l")>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import csv
import re
import requests
import sys
urls = [
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--500-999-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--1000-1299-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--1300-1699-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--1700-3999-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--4000-5999-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--6000-8199-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--8200-8999-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--9000-11999-",
"https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--12000-15999-"
]
# compile regex to match that:
# <dl> <dt>
# <p><span id="ERROR_SUCCESS"></span><span id="error_success"></span><strong>ERROR_SUCCESS</strong></p>
# </dt> <dd> <dl> <dt>
# <p>0 (0x0)</p>
# </dt> <dt>
# <p>The operation completed successfully.</p>
# </dt> </dl> </dd> <dt>
# <p><span id="ERROR_INVALID_FUNCTION"></span><span id="error_invalid_function"></span><strong>ERROR_INVALID_FUNCTION</strong></p>
# </dt> <dd> <dl> <dt>
# <p>1 (0x1)</p>
# </dt> <dt>
# <p>Incorrect function.</p>
# </dt> </dl> </dd> <dt>
# ... </dl>
# FIXME: this regex worked on 2020-04-30, but will probably fail at some point
# in the future
re_error = re.compile(r'<dt>\s*<p>\s*(?:<span id="[^"]*">\s*</span>\s*)*\s*<strong>\s*([A-Z][0-9A-Za-z_]+)\s*</strong>\s*</p>\s*</dt>\s*<dd>\s*<dl>\s+<dt>\s*<p>\s*(\d+)\s+\(0x[0-9A-Fa-f]+\)\s*</p>\s*</dt>\s*<dt>\s*<p>\s*(.+?)\s*</p>\s*</dt>\s*</dl>\s*</dd>', re.MULTILINE)
# retrieve error codes
errors = []
session = requests.Session()
for url in urls:
offset = None
text = session.get(url).text
for match in re_error.finditer(text):
# errors are listed in a continuous block, so warn if the distance from
# the previous match is suspicious
# FIXME: this does not detect a batch of non-matching entries at the
# beginning/end of the list
match_begin, match_end = match.span()
if offset and match_begin > offset + 1:
print("skipping unrecognized data: " + text[offset:match_begin] + "\n",
file=sys.stderr)
# add name, code and, message to list of errors
errors.append((match.group(1), int(match.group(2)), match.group(3)))
offset = match_end
# dump as CSV
with open("windows_system_error_codes.csv", "wt") as fh:
csvwriter = csv.writer(fh, delimiter=";")
for row in errors:
csvwriter.writerow(row)
# dump as markdown table
with open("windows_system_error_codes.md", "wt") as fh:
fh.write("Name | Code | Message\n")
fh.write("--- | --- | ---\n")
for name, code, mesg in errors:
fh.write("{:60s} | {:5d} | {:s}\n".format(name, code, mesg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment