forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_release_metadata.py
More file actions
83 lines (69 loc) · 2.74 KB
/
Copy pathdesktop_release_metadata.py
File metadata and controls
83 lines (69 loc) · 2.74 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
#!/usr/bin/env python3
"""Shared parsing helpers for desktop release KEY_VALUE metadata."""
from __future__ import annotations
from typing import NoReturn
def fail(message: str) -> NoReturn:
raise SystemExit(f"FAIL: {message}")
def normalize_metadata_line(line: str) -> str:
stripped = line.strip()
if stripped.startswith("<!--"):
stripped = stripped[4:].strip()
if stripped.endswith("-->"):
stripped = stripped[:-3].strip()
return stripped
def parse_metadata(body: str) -> dict[str, str]:
in_block = False
metadata: dict[str, str] = {}
for line in body.splitlines():
stripped = normalize_metadata_line(line)
if stripped == "KEY_VALUE_START":
in_block = True
continue
if stripped == "KEY_VALUE_END":
return metadata
if not in_block or not stripped or stripped.startswith("#"):
continue
if ":" not in stripped:
fail(f"invalid release metadata line: {stripped}")
key, value = stripped.split(":", 1)
metadata[key.strip()] = value.strip()
fail("release body is missing KEY_VALUE_START/KEY_VALUE_END metadata block")
def update_metadata(body: str, values: dict[str, str]) -> str:
"""Replace or append keys inside the release metadata block."""
if any("\n" in value or "\r" in value for value in values.values()):
fail("release metadata values must be single-line strings")
lines = body.splitlines()
output: list[str] = []
in_block = False
saw_block = False
seen: set[str] = set()
for line in lines:
stripped = normalize_metadata_line(line)
if stripped == "KEY_VALUE_START":
if in_block:
fail("release body has nested KEY_VALUE_START blocks")
in_block = True
saw_block = True
output.append(line)
continue
if stripped == "KEY_VALUE_END":
if not in_block:
fail("release body has KEY_VALUE_END without KEY_VALUE_START")
for key, value in values.items():
if key not in seen:
output.append(f"{key}: {value}")
in_block = False
output.append(line)
continue
if in_block and ":" in stripped:
key = stripped.split(":", 1)[0].strip()
if key in values:
output.append(f"{key}: {values[key]}")
seen.add(key)
continue
output.append(line)
if in_block:
fail("release body metadata block is missing KEY_VALUE_END")
if not saw_block:
fail("release body is missing KEY_VALUE_START/KEY_VALUE_END metadata block")
return "\n".join(output) + ("\n" if body.endswith("\n") else "")