Skip to content

Instantly share code, notes, and snippets.

@yeggor
Created July 19, 2023 00:38
Show Gist options
  • Save yeggor/9a28660a83121e52cff7416f643e9570 to your computer and use it in GitHub Desktop.
Save yeggor/9a28660a83121e52cff7416f643e9570 to your computer and use it in GitHub Desktop.
Populating the guids.json with GUIDs from the LVFS public database
# Populating the guids.json with GUIDs from the LVFS public database
# https://github.com/fwupd/fwupd/issues/5869
import json
import re
import struct
import uuid
import requests
def get_guiddb_guid(guid: str) -> list:
guid_b = uuid.UUID(guid).bytes_le
guiddb_guid = list()
guiddb_guid.append(struct.unpack("<I", guid_b[:4])[0])
guiddb_guid.append(struct.unpack("<H", guid_b[4:6])[0])
guiddb_guid.append(struct.unpack("<H", guid_b[6:8])[0])
for b in guid_b[8:]:
guiddb_guid.append(b)
return guiddb_guid
def get_lvfs_guids(remote: bool = False) -> list:
if remote:
r = requests.get("https://fwupd.org/lvfs/shards/export/csv")
if r.status_code != 200:
exit("could not download GUID database from LVFS")
return r.text.splitlines()
with open("lvfs.csv", "r") as f:
lvfs_guids = f.read().splitlines()
return lvfs_guids
def camel2snake(name: str) -> str:
if name.startswith("g"):
name = name[1:]
snake = name[0].lower() + re.sub(
r"(?!^)[A-Z]", lambda x: "_" + x.group(0).lower(), name[1:]
)
return snake.upper()
def inc_name(name: str) -> str:
try:
num = name[-1]
intnum = int(num)
return name[:-1] + str(intnum + 1)
except ValueError as _:
pass
return f"{name}2"
def main() -> None:
with open("guids.json", "r") as f:
guids = json.load(f)
print(f"initial size of guids.json: {len(guids)}")
lvfs_guids = get_lvfs_guids(remote=False)
guids_values = list(guids.values())
for l in lvfs_guids:
guid, name = l.split(",")
guiddb_guid = get_guiddb_guid(guid)
if guiddb_guid in guids_values:
continue
if "Protocol" not in name:
continue # add GUIDs related to protocols only
name = camel2snake(name)
while name in guids:
print(f"name {name} already present ({guiddb_guid})")
name = inc_name(name)
print(f"new name: {name}")
guids[name] = guiddb_guid
print(f"updated size of guids.json: {len(guids)}")
with open("guids_updated.json", "w") as f:
json.dump(guids, f, indent=2)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment