]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ukify: move verb mangling to finalize_options()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 7 Jun 2023 07:10:49 +0000 (09:10 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 12 Jun 2023 09:12:02 +0000 (11:12 +0200)
This simplifies the logic: finalize_options() is the step that does the
checks and the mangling. The checks for consistency are done in more places,
so we need to pass a verb (we only have 'build', but once we add other verbs,
any would do).

src/kernel-install/60-ukify.install.in
src/ukify/test/test_ukify.py
src/ukify/ukify.py

index 0927bd7a2e84713ddbfb031bece14c2c9c28216d..96ca2482b061a769d508061eee34ff71eba434d8 100755 (executable)
@@ -186,7 +186,7 @@ def call_ukify(opts):
     # Create "empty" namespace. We want to override just a few settings, so it
     # doesn't make sense to configure everything. We pretend to parse an empty
     # argument set to prepopulate the namespace with the defaults.
-    opts2 = ukify['create_parser']().parse_args(())
+    opts2 = ukify['create_parser']().parse_args(['build'])
 
     opts2.config = config_file_location()
     opts2.uname = opts.kernel_version
index 5829bae6985e06b4200bdd242f32b7b4f36d2b83..3ca9b531c241613a8dcad153ec199a7e19c03734 100755 (executable)
@@ -87,7 +87,7 @@ def test_apply_config(tmp_path):
         Phases = {':'.join(ukify.KNOWN_PHASES)}
         '''))
 
-    ns = ukify.create_parser().parse_args(())
+    ns = ukify.create_parser().parse_args(['build'])
     ns.linux = None
     ns.initrd = []
     ukify.apply_config(ns, config)
index ee8a9029bc3464cbf65a56bea22836d94aaca5c8..9abaefec9aec928851075d8ebd65f1a0a7c07fbe 100755 (executable)
@@ -1187,6 +1187,31 @@ ukify [options…] VERB
 
 
 def finalize_options(opts):
+    # Figure out which syntax is being used, one of:
+    # ukify verb --arg --arg --arg
+    # ukify linux initrd…
+    if len(opts.positional) == 1 and opts.positional[0] in VERBS:
+        opts.verb = opts.positional[0]
+    elif opts.linux or opts.initrd:
+        raise ValueError('--linux/--initrd options cannot be used with positional arguments')
+    else:
+        print("Assuming obsolete commandline syntax with no verb. Please use 'build'.")
+        if opts.positional:
+            opts.linux = pathlib.Path(opts.positional[0])
+        # If we have initrds from parsing config files, append our positional args at the end
+        opts.initrd = (opts.initrd or []) + [pathlib.Path(arg) for arg in opts.positional[1:]]
+        opts.verb = 'build'
+
+    # Check that --pcr-public-key=, --pcr-private-key=, and --phases=
+    # have either the same number of arguments are are not specified at all.
+    n_pcr_pub = None if opts.pcr_public_keys is None else len(opts.pcr_public_keys)
+    n_pcr_priv = None if opts.pcr_private_keys is None else len(opts.pcr_private_keys)
+    n_phase_path_groups = None if opts.phase_path_groups is None else len(opts.phase_path_groups)
+    if n_pcr_pub is not None and n_pcr_pub != n_pcr_priv:
+        raise ValueError('--pcr-public-key= specifications must match --pcr-private-key=')
+    if n_phase_path_groups is not None and n_phase_path_groups != n_pcr_priv:
+        raise ValueError('--phases= specifications must match --pcr-private-key=')
+
     if opts.cmdline and opts.cmdline.startswith('@'):
         opts.cmdline = pathlib.Path(opts.cmdline[1:])
     elif opts.cmdline:
@@ -1244,37 +1269,9 @@ def finalize_options(opts):
 
 
 def parse_args(args=None):
-    p = create_parser()
-    opts = p.parse_args(args)
-
-    # Figure out which syntax is being used, one of:
-    # ukify verb --arg --arg --arg
-    # ukify linux initrd…
-    if len(opts.positional) == 1 and opts.positional[0] in VERBS:
-        opts.verb = opts.positional[0]
-    elif opts.linux or opts.initrd:
-        raise ValueError('--linux/--initrd options cannot be used with positional arguments')
-    else:
-        print("Assuming obsolete commandline syntax with no verb. Please use 'build'.")
-        if opts.positional:
-            opts.linux = pathlib.Path(opts.positional[0])
-        opts.initrd = [pathlib.Path(arg) for arg in opts.positional[1:]]
-        opts.verb = 'build'
-
-    # Check that --pcr-public-key=, --pcr-private-key=, and --phases=
-    # have either the same number of arguments are are not specified at all.
-    n_pcr_pub = None if opts.pcr_public_keys is None else len(opts.pcr_public_keys)
-    n_pcr_priv = None if opts.pcr_private_keys is None else len(opts.pcr_private_keys)
-    n_phase_path_groups = None if opts.phase_path_groups is None else len(opts.phase_path_groups)
-    if n_pcr_pub is not None and n_pcr_pub != n_pcr_priv:
-        raise ValueError('--pcr-public-key= specifications must match --pcr-private-key=')
-    if n_phase_path_groups is not None and n_phase_path_groups != n_pcr_priv:
-        raise ValueError('--phases= specifications must match --pcr-private-key=')
-
+    opts = create_parser().parse_args(args)
     apply_config(opts)
-
     finalize_options(opts)
-
     return opts