2019-01-24 21:02:28 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-01-18 13:47:49 +00:00
|
|
|
|
|
|
|
import csv
|
|
|
|
import sys
|
2019-04-21 21:05:19 +00:00
|
|
|
import re
|
2019-01-18 13:47:49 +00:00
|
|
|
|
|
|
|
# We have some duplicates
|
|
|
|
ALIAS_TABLE = [
|
|
|
|
{"ipfs", "p2p"},
|
|
|
|
]
|
|
|
|
|
|
|
|
def check(fname='table.csv'):
|
|
|
|
class CheckError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
aliases = {}
|
|
|
|
for nameset in ALIAS_TABLE:
|
|
|
|
for name in nameset:
|
|
|
|
aliases[name] = nameset
|
|
|
|
|
|
|
|
success = True
|
|
|
|
with open(fname) as table:
|
|
|
|
tablereader = csv.reader(table, skipinitialspace=True)
|
|
|
|
codes = {}
|
|
|
|
names = {}
|
|
|
|
for line, row in enumerate(tablereader):
|
|
|
|
# Skip the header
|
|
|
|
if line == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Check for invalid rows
|
2019-02-13 15:34:21 +00:00
|
|
|
if len(row) != 4:
|
|
|
|
raise CheckError(f"expected 4 items, got {len(row)}")
|
2019-01-18 13:47:49 +00:00
|
|
|
|
2019-02-13 15:34:21 +00:00
|
|
|
[name, _, code, _] = row
|
2019-01-18 13:47:49 +00:00
|
|
|
|
2019-02-13 15:34:21 +00:00
|
|
|
# Check for a name
|
2019-01-18 13:47:49 +00:00
|
|
|
if not name:
|
2019-02-13 15:34:21 +00:00
|
|
|
raise CheckError(f"empty protocol name for code '{code}'")
|
|
|
|
|
|
|
|
# Check code format
|
2019-04-21 21:05:19 +00:00
|
|
|
if not re.match(r"^0x([0-9a-f][0-9a-f])+$", code):
|
|
|
|
raise CheckError(f"code for '{name}' does not look like a byte sequence: '{code}'")
|
2019-01-18 13:47:49 +00:00
|
|
|
|
2020-07-20 03:59:24 +00:00
|
|
|
# Check name format
|
|
|
|
if not re.match(r"^[a-z][a-z0-9_-]+$", name):
|
|
|
|
raise CheckError(f"name '{name}' violates naming restrictions")
|
|
|
|
|
2019-01-18 13:47:49 +00:00
|
|
|
# Parse the code
|
|
|
|
try:
|
2019-02-13 15:34:21 +00:00
|
|
|
code = int(code, 16)
|
2019-01-18 13:47:49 +00:00
|
|
|
except Exception as e:
|
2019-02-13 15:34:21 +00:00
|
|
|
raise CheckError(f"failed to parse code '{code}' for '{name}': {e}")
|
2019-01-18 13:47:49 +00:00
|
|
|
|
|
|
|
# Finally, check for duplicates
|
|
|
|
|
|
|
|
if name in names:
|
|
|
|
raise CheckError(f"found duplicate {name}: {code} and {names[name]}")
|
|
|
|
else:
|
|
|
|
names[name] = code
|
|
|
|
|
|
|
|
if code in codes:
|
|
|
|
dup = codes[code]
|
|
|
|
if name in aliases:
|
|
|
|
if dup in aliases[name]:
|
|
|
|
# Skip aliased names
|
|
|
|
continue
|
|
|
|
raise CheckError(
|
|
|
|
f"found duplicate for code {hex(code)} "
|
|
|
|
f"for '{codes[code]}' and '{name}'"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
codes[code] = name
|
|
|
|
except CheckError as e:
|
|
|
|
success = False
|
|
|
|
print(f"row {line}: {e}", file=sys.stderr)
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if not check():
|
|
|
|
sys.exit(1)
|