stages/systemd.unit.create: validation function

Cleaner and makes the linter happy (too-many-branches).
This commit is contained in:
Achilleas Koutsou 2024-05-14 14:31:43 +02:00
parent e92b683e11
commit 88974ab052

View file

@ -5,14 +5,11 @@ import sys
import osbuild.api
def main(tree, options):
filename = options["filename"]
def validate(filename, cfg):
# ensure the service name does not exceed maximum filename length
if len(filename) > 255:
raise ValueError(f"Error: the {filename} service exceeds the maximum filename length.")
cfg = options["config"]
# Filename extension must match the config:
# .service requires a Service section
# .mount requires a Mount section
@ -21,6 +18,12 @@ def main(tree, options):
if filename.endswith(".mount") and "Mount" not in cfg:
raise ValueError(f"Error: {filename} unit requires Mount section")
def main(tree, options):
filename = options["filename"]
cfg = options["config"]
validate(filename, cfg)
# We trick configparser into letting us write multiple instances of the same option by writing them as keys with no
# value, so we enable allow_no_value
config = configparser.ConfigParser(allow_no_value=True)