diff mbox series

[unstable,linux-signed,v2,8/9] UBUNTU: [Packaging] generate_control.py: pass everything by argument

Message ID 20241030132153.482139-9-agathe.porte@canonical.com
State New
Headers show
Series Refactor and test generate-control | expand

Commit Message

Agathe Porte Oct. 30, 2024, 1:19 p.m. UTC
If we want to be able to write unit tests for this script,
first we need to get rid of global variables and pass everything
by argument.

Signed-off-by: Agathe Porte <agathe.porte@canonical.com>
---
 debian/scripts/generate_control.py | 109 ++++++++++++++++-------------
 1 file changed, 59 insertions(+), 50 deletions(-)
diff mbox series

Patch

diff --git a/debian/scripts/generate_control.py b/debian/scripts/generate_control.py
index 556f327..8e64ff3 100755
--- a/debian/scripts/generate_control.py
+++ b/debian/scripts/generate_control.py
@@ -1,24 +1,14 @@ 
 #!/usr/bin/python3 -B
 
+
+from argparse import ArgumentParser, Namespace
 import sys
 from textwrap import dedent
 
 from config import Signing
 
-(
-    series,
-    source_name,
-    generate_name,
-    source_version,
-    unsigned_name,
-    unsigned_version,
-    abi_version,
-) = sys.argv[1:]
-
-signing = Signing.load("debian/package.config")
 
-
-def gen_depends_uci():
+def gen_depends_uci(signing: Signing):
     uci_archs = set()
     for _, archs in signing.package_flavour_archs("cvm"):
         uci_archs.update(archs)
@@ -30,7 +20,7 @@  def gen_depends_uci():
         )
 
 
-def gen_depends_efi():
+def gen_depends_efi(signing: Signing):
     efi_archs = set()
     for (arch, flavour), (stype, binary) in signing.arch_flavour_data:
         if stype == "efi":
@@ -39,52 +29,52 @@  def gen_depends_efi():
         print(f' sbsigntool [{" ".join(sorted(efi_archs))}],')
 
 
-def gen_depends():
+def gen_depends(signing: Signing, args: Namespace):
     for flavour, archs in signing.flavour_archs:
         print(
-            f' linux-image-unsigned-{abi_version}-{flavour} (= {unsigned_version}) [{" ".join(archs)}],'
+            f' linux-image-unsigned-{args.abi_version}-{flavour} (= {args.unsigned_version}) [{" ".join(archs)}],'
         )
         print(
-            f' linux-buildinfo-{abi_version}-{flavour} (= {unsigned_version}) [{" ".join(archs)}],'
+            f' linux-buildinfo-{args.abi_version}-{flavour} (= {args.unsigned_version}) [{" ".join(archs)}],'
         )
     # generate-only build-depends with a profile
     # (activated by debian/scripts/parameterise-ancillaries)
-    gen_depends_uci()
-    print(f" {generate_name} (= {source_version}) <!pkg.linux-generate>,")
+    gen_depends_uci(signing)
+    print(f" {args.generate_name} (= {args.source_version}) <!pkg.linux-generate>,")
     for flavour, archs in signing.package_flavour_archs("extra"):
         # ubuntu-core-initramfs sometime needs extra modules https://bugs.launchpad.net/bugs/2032760
         print(
-            f' linux-modules-extra-{abi_version}-{flavour} (= {unsigned_version}) [{" ".join(sorted(archs))}]  <pkg.linux-generate>,'
+            f' linux-modules-extra-{args.abi_version}-{flavour} (= {args.unsigned_version}) [{" ".join(sorted(archs))}]  <pkg.linux-generate>,'
         )
-    gen_depends_efi()
+    gen_depends_efi(signing)
     # For HMACs
     print(f" openssl,")
 
 
-def replace_placeholders():
+def replace_placeholders(signing: Signing, args: Namespace):
     for line in sys.stdin.readlines():
-        line = line.replace("@SRCPKGNAME@", source_name)
-        line = line.replace("@SERIES@", series)
+        line = line.replace("@SRCPKGNAME@", args.source_name)
+        line = line.replace("@SERIES@", args.series)
         if "@DEPENDS@" in line:
-            gen_depends()
+            gen_depends(signing, args)
         else:
             print(line, end="")
 
 
-def gen_binpkgs():
+def gen_binpkgs(signing: Signing, args: Namespace):
     for flavour, archs in signing.package_flavour_archs("image"):
         print(
             dedent(
                 f"""\
 
-            Package: linux-image-{abi_version}-{flavour}
+            Package: linux-image-{args.abi_version}-{flavour}
             Architecture: {" ".join(archs)}
             Depends: ${{unsigned:Depends}}
             Recommends: ${{unsigned:Recommends}}
             Suggests: ${{unsigned:Suggests}}
             Conflicts: ${{unsigned:Conflicts}}
             Provides: ${{unsigned:Provides}}
-            Built-Using: {unsigned_name} (= {unsigned_version})
+            Built-Using: {args.unsigned_name} (= {args.unsigned_version})
             Description: Signed kernel image {flavour}
              A kernel image for {flavour}.  This version of it is signed with
              Canonical's signing key.
@@ -96,13 +86,13 @@  def gen_binpkgs():
             dedent(
                 f"""\
 
-            Package: kernel-signed-image-{abi_version}-{flavour}-di
+            Package: kernel-signed-image-{args.abi_version}-{flavour}-di
             Package-Type: udeb
             Section: debian-installer
             Priority: extra
             Provides: kernel-signed-image
             Architecture: {" ".join(archs)}
-            Built-Using: {unsigned_name} (= {unsigned_version})
+            Built-Using: {args.unsigned_name} (= {args.unsigned_version})
             Description: Signed kernel image {flavour} for the Debian installer
              A kernel image for {flavour}.  This version of it is signed with
              Canonical's UEFI signing key.  It is intended for the Debian installer,
@@ -115,16 +105,16 @@  def gen_binpkgs():
             dedent(
                 f"""\
 
-            Package: linux-image-hmac-{abi_version}-{flavour}
+            Package: linux-image-hmac-{args.abi_version}-{flavour}
             Build-Profiles: <!stage1>
             Architecture: {" ".join(archs)}
             Section: kernel
             Priority: optional
-            Depends: ${{misc:Depends}}, ${{shlibs:Depends}}, linux-image-{abi_version}-{flavour}
+            Depends: ${{misc:Depends}}, ${{shlibs:Depends}}, linux-image-{args.abi_version}-{flavour}
             Suggests: fips-initramfs-generic
-            Description: HMAC file for linux kernel image {abi_version}-{flavour}
+            Description: HMAC file for linux kernel image {args.abi_version}-{flavour}
              This package contains the HMAC file for Linux kernel image for version
-             {abi_version}-{flavour}
+             {args.abi_version}-{flavour}
             """
             ).rstrip()
         )
@@ -134,14 +124,14 @@  def gen_binpkgs():
             dedent(
                 f"""\
 
-            Package: linux-image-{abi_version}-{flavour}-fde
+            Package: linux-image-{args.abi_version}-{flavour}-fde
             Architecture: {" ".join(archs)}
             Depends: ${{unsigned:Depends}}
             Recommends: ${{cvm:Recommends}}
             Suggests: ${{unsigned:Suggests}}
             Conflicts: ${{cvm:Conflicts}}
             Provides: ${{unsigned:Provides}}
-            Built-Using: {unsigned_name} (= {unsigned_version})
+            Built-Using: {args.unsigned_name} (= {args.unsigned_version})
             Description: Signed kernel image {flavour} for CVM
              A kernel image for {flavour}.  This version of it is signed with
              Canonical's signing key.
@@ -149,19 +139,19 @@  def gen_binpkgs():
             ).rstrip()
         )
     for flavour, archs in signing.package_flavour_archs("uc"):
-        depends = f"linux-modules-{abi_version}-{flavour}"
+        depends = f"linux-modules-{args.abi_version}-{flavour}"
         for extra_flavour, extra_archs in signing.package_flavour_archs("extra"):
             if extra_flavour == flavour:
-                depends += f", linux-modules-extra-{abi_version}-{flavour} [{' '.join(sorted(extra_archs))}]"
+                depends += f", linux-modules-extra-{args.abi_version}-{flavour} [{' '.join(sorted(extra_archs))}]"
         # Mostly similar to image, but we don't have recommands nor conflicts
         print(
             dedent(
                 f"""\
 
-            Package: linux-image-uc-{abi_version}-{flavour}
+            Package: linux-image-uc-{args.abi_version}-{flavour}
             Architecture: {" ".join(archs)}
             Depends: {depends}
-            Built-Using: {unsigned_name} (= {unsigned_version})
+            Built-Using: {args.unsigned_name} (= {args.unsigned_version})
             Description: Signed kernel image {flavour} for Ubuntu Core
              A kernel image for {flavour}.  This version of it is signed with
              Canonical's signing key.
@@ -170,16 +160,16 @@  def gen_binpkgs():
         )
 
 
-def gen_dbgsym_binpkgs():
+def gen_dbgsym_binpkgs(signing: Signing, args: Namespace):
     for flavour, archs in signing.package_flavour_archs("image"):
         print(
             dedent(
                 f"""\
 
-            Package: linux-image-{abi_version}-{flavour}-dbgsym
+            Package: linux-image-{args.abi_version}-{flavour}-dbgsym
             Section: devel
             Architecture: {" ".join(archs)}
-            Depends: linux-image-unsigned-{abi_version}-{flavour}-dbgsym
+            Depends: linux-image-unsigned-{args.abi_version}-{flavour}-dbgsym
             Description: Signed kernel image {flavour}
              A link to the debugging symbols for the {flavour} signed kernel.
             """
@@ -190,10 +180,10 @@  def gen_dbgsym_binpkgs():
             dedent(
                 f"""\
 
-            Package: linux-image-{abi_version}-{flavour}-fde-dbgsym
+            Package: linux-image-{args.abi_version}-{flavour}-fde-dbgsym
             Section: devel
             Architecture: {" ".join(archs)}
-            Depends: linux-image-unsigned-{abi_version}-{flavour}-dbgsym
+            Depends: linux-image-unsigned-{args.abi_version}-{flavour}-dbgsym
             Description: Signed kernel image {flavour} for CVM (debug)
              A link to the debugging symbols for the {flavour} signed kernel.
             """
@@ -201,14 +191,33 @@  def gen_dbgsym_binpkgs():
         )
 
 
-def main():
-    replace_placeholders()
-    gen_binpkgs()
+def main(args: Namespace):
+    signing = Signing.load("debian/package.config")
+
+    replace_placeholders(signing, args)
+    gen_binpkgs(signing, args)
 
     # XXX: all dbgsym packages _must_ be at the end of debian/control else the
     # build will hang forever on the builder.
-    gen_dbgsym_binpkgs()
+    gen_dbgsym_binpkgs(signing, args)
 
 
 if __name__ == "__main__":
-    main()
+
+    parser = ArgumentParser(
+        prog="generate_control.py",
+        description="generate debian/control file from debian/control.stub",
+        epilog="pass debian/control.stub as stdin and get debian/control as stdout",
+    )
+
+    parser.add_argument("series")
+    parser.add_argument("source_name")
+    parser.add_argument("generate_name")
+    parser.add_argument("source_version")
+    parser.add_argument("unsigned_name")
+    parser.add_argument("unsigned_version")
+    parser.add_argument("abi_version")
+
+    args = parser.parse_args()
+
+    main(args)