62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Copy only non-sensitive DockDeck metadata from Unraid templates."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from pathlib import Path
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
ALLOWED_FIELDS = ("Name", "WebUI", "Icon")
|
|
|
|
|
|
def sanitize(source_dir: Path, destination_dir: Path) -> int:
|
|
destination_dir.mkdir(parents=True, exist_ok=True)
|
|
written = 0
|
|
|
|
for source in sorted(source_dir.glob("*.xml")):
|
|
try:
|
|
root = ET.parse(source).getroot()
|
|
except (ET.ParseError, OSError):
|
|
continue
|
|
|
|
output = ET.Element("Container")
|
|
for field in ALLOWED_FIELDS:
|
|
value = root.findtext(field)
|
|
if value:
|
|
ET.SubElement(output, field).text = value.strip()
|
|
|
|
if output.findtext("Name"):
|
|
destination = destination_dir / source.name
|
|
ET.ElementTree(output).write(
|
|
destination,
|
|
encoding="utf-8",
|
|
xml_declaration=True,
|
|
)
|
|
destination.chmod(0o644)
|
|
written_tags = {
|
|
child.tag for child in ET.parse(destination).getroot()
|
|
}
|
|
if not written_tags.issubset(ALLOWED_FIELDS):
|
|
destination.unlink(missing_ok=True)
|
|
raise RuntimeError(
|
|
f"Unexpected field in sanitized template: {source.name}"
|
|
)
|
|
written += 1
|
|
|
|
return written
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("source", type=Path)
|
|
parser.add_argument("destination", type=Path)
|
|
args = parser.parse_args()
|
|
count = sanitize(args.source, args.destination)
|
|
print(f"Sanitized and verified {count} Unraid templates")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|