In order to further deduplicate the contents of the various unit
files, we need to be able to merge multiple additional units
into the initial one.

Luckily the merge logic is in no way constrained to working with
just two units, so achieving this is pretty much just a matter
of lifting the existing limitation on the number of arguments
that the script accepts.

As a special case, it's now also possible to call the script
with just the base unit as argument. No merging will be performed
in that case, obviously, but we'll still go through the basic
validation and cleanup steps.

This also fixes a bug in the check for the number of arguments:
sys.argv also contains the name of the script, so we should have
checked that its size was at least 3. The check is now written in
a way that's less prone to misunderstandings.

Signed-off-by: Andrea Bolognani <abolo...@redhat.com>
---
 scripts/merge-systemd-units.py | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/scripts/merge-systemd-units.py b/scripts/merge-systemd-units.py
index bc3321230d..30e8757544 100755
--- a/scripts/merge-systemd-units.py
+++ b/scripts/merge-systemd-units.py
@@ -82,13 +82,18 @@ def merge_units(base, extra):
     return merged
 
 
-if len(sys.argv) < 2:
-    print("usage: {} BASE EXTRA".format(sys.argv[0]))
+prog = sys.argv[0]
+args = sys.argv[1:]
+
+if len(args) < 1:
+    print("usage: {} BASE [EXTRA]...".format(prog))
     sys.exit(1)
 
-base = parse_unit(sys.argv[1])
-extra = parse_unit(sys.argv[2])
+merged = parse_unit(args[0])
+
+for arg in args[1:]:
+    extra = parse_unit(arg)
 
-merged = merge_units(base, extra)
+    merged = merge_units(merged, extra)
 
 sys.stdout.write(format_unit(merged))
-- 
2.41.0

Reply via email to