| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
"""Build `debsigs` in a temporary directory, run the test.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import enum |
| 8 |
import os |
| 9 |
import pathlib |
| 10 |
import shlex |
| 11 |
import subprocess # noqa: S404 |
| 12 |
import sys |
| 13 |
import tempfile |
| 14 |
import typing |
| 15 |
|
| 16 |
import click |
| 17 |
|
| 18 |
|
| 19 |
if typing.TYPE_CHECKING: |
| 20 |
from typing import Final |
| 21 |
|
| 22 |
|
| 23 |
class Impl(str, enum.Enum): |
| 24 |
"""The supported `debsigs` implementations.""" |
| 25 |
|
| 26 |
PERL = "perl" |
| 27 |
|
| 28 |
def __str__(self) -> str: |
| 29 |
"""Use the value itself as a string representation.""" |
| 30 |
return self.value |
| 31 |
|
| 32 |
|
| 33 |
def copy_source(tempd: pathlib.Path, u8env: dict[str, str]) -> pathlib.Path: |
| 34 |
"""Copy the `debsigs` source files over to the temporary directory.""" |
| 35 |
srcdir: Final = tempd / "debsigs" |
| 36 |
srcdir.mkdir(mode=0o755) |
| 37 |
|
| 38 |
files: Final = sorted( |
| 39 |
set( |
| 40 |
subprocess.check_output( # noqa: S603 |
| 41 |
[ # noqa: S607 |
| 42 |
"git", |
| 43 |
"ls-files", |
| 44 |
], |
| 45 |
encoding="UTF-8", |
| 46 |
env=u8env, |
| 47 |
).splitlines(), |
| 48 |
), |
| 49 |
) |
| 50 |
|
| 51 |
srcball: Final = tempd / "debsigs.tar" |
| 52 |
subprocess.check_call( # noqa: S603 |
| 53 |
[ # noqa: S607 |
| 54 |
"tar", |
| 55 |
"-cf", |
| 56 |
srcball, |
| 57 |
"--", |
| 58 |
*files, |
| 59 |
], |
| 60 |
env=u8env, |
| 61 |
) |
| 62 |
subprocess.check_call( # noqa: S603 |
| 63 |
[ # noqa: S607 |
| 64 |
"tar", |
| 65 |
"-xf", |
| 66 |
srcball, |
| 67 |
], |
| 68 |
cwd=srcdir, |
| 69 |
env=u8env, |
| 70 |
) |
| 71 |
srcball.unlink() |
| 72 |
return srcdir |
| 73 |
|
| 74 |
|
| 75 |
def build_debsigs_perl( |
| 76 |
bindir: pathlib.Path, |
| 77 |
srcdir: pathlib.Path, |
| 78 |
u8env: dict[str, str], |
| 79 |
) -> pathlib.Path: |
| 80 |
"""Build the Perl implementation of `debsigs`, create a wrapper script.""" |
| 81 |
perldir: Final = srcdir / "perl" |
| 82 |
subprocess.check_call( # noqa: S603 |
| 83 |
[ # noqa: S607 |
| 84 |
"perl", |
| 85 |
"Makefile.PL", |
| 86 |
], |
| 87 |
cwd=perldir, |
| 88 |
env=u8env, |
| 89 |
) |
| 90 |
subprocess.check_call( # noqa: S603 |
| 91 |
[ # noqa: S607 |
| 92 |
"make", |
| 93 |
], |
| 94 |
cwd=perldir, |
| 95 |
env=u8env, |
| 96 |
) |
| 97 |
blibdir: Final = perldir / "blib/lib" |
| 98 |
if not blibdir.is_dir(): |
| 99 |
raise RuntimeError(repr((perldir, blibdir))) |
| 100 |
|
| 101 |
def create_wrapper(name: str) -> pathlib.Path: |
| 102 |
"""Create a single wrapper for one of the Perl command-line tools.""" |
| 103 |
wrapper: Final = bindir / name |
| 104 |
wrapper.write_text( |
| 105 |
f"""#!/bin/sh |
| 106 |
|
| 107 |
set -e |
| 108 |
|
| 109 |
exec perl -I{shlex.quote(str(blibdir))} -- {shlex.quote(str(perldir / name))} "$@" |
| 110 |
""", |
| 111 |
encoding="UTF-8", |
| 112 |
) |
| 113 |
wrapper.chmod(0o755) |
| 114 |
return wrapper |
| 115 |
|
| 116 |
wrapper: Final = create_wrapper("debsigs") |
| 117 |
# Make sure the wrapper script works. |
| 118 |
version: Final = subprocess.check_output( # noqa: S603 |
| 119 |
[wrapper, "--version"], |
| 120 |
encoding="UTF-8", |
| 121 |
env=u8env, |
| 122 |
) |
| 123 |
if not version.startswith("debsigs v0.2."): |
| 124 |
raise RuntimeError(repr(version)) |
| 125 |
|
| 126 |
for other in ("debsigs-autosign", "debsigs-signchanges"): |
| 127 |
create_wrapper(other) |
| 128 |
|
| 129 |
return wrapper |
| 130 |
|
| 131 |
|
| 132 |
def build_debsigs( |
| 133 |
impl: Impl, |
| 134 |
bindir: pathlib.Path, |
| 135 |
srcdir: pathlib.Path, |
| 136 |
u8env: dict[str, str], |
| 137 |
) -> pathlib.Path: |
| 138 |
"""Build the `debsigs` implementation, return the path to a wrapper script.""" |
| 139 |
match impl: |
| 140 |
case Impl.PERL: |
| 141 |
return build_debsigs_perl(bindir, srcdir, u8env) |
| 142 |
|
| 143 |
case _: |
| 144 |
raise NotImplementedError(repr(impl)) |
| 145 |
|
| 146 |
|
| 147 |
@click.command(name="build-and-test") |
| 148 |
@click.option( |
| 149 |
"--debsigs-impl", |
| 150 |
type=Impl, |
| 151 |
default=Impl.PERL, |
| 152 |
help="the debsigs implementation to test", |
| 153 |
) |
| 154 |
@click.option( |
| 155 |
"--debsig-verify-program", |
| 156 |
type=click.Path(exists=True, executable=True, path_type=pathlib.Path, resolve_path=True), |
| 157 |
default="/usr/bin/debsig-verify", |
| 158 |
help="the debsig-verify program to check the signed package files with", |
| 159 |
) |
| 160 |
@click.option( |
| 161 |
"--gnupg-program", |
| 162 |
type=click.Path(exists=True, executable=True, path_type=pathlib.Path, resolve_path=True), |
| 163 |
default="/usr/bin/gpg", |
| 164 |
help="the GnuPG program to test and sign with", |
| 165 |
) |
| 166 |
@click.option( |
| 167 |
"--sop-program", |
| 168 |
type=click.Path(exists=True, executable=True, path_type=pathlib.Path, resolve_path=True), |
| 169 |
default="/usr/bin/sqop", |
| 170 |
help="the stateless OpenPGP program to generate keys with", |
| 171 |
) |
| 172 |
def main( |
| 173 |
*, |
| 174 |
debsigs_impl: Impl, |
| 175 |
debsig_verify_program: pathlib.Path, |
| 176 |
gnupg_program: pathlib.Path, |
| 177 |
sop_program: pathlib.Path, |
| 178 |
) -> None: |
| 179 |
"""Parse command-line options, build `debsigs`, run the test.""" |
| 180 |
with tempfile.TemporaryDirectory(prefix="debsigs-build.") as tempd_obj: |
| 181 |
tempd: Final = pathlib.Path(tempd_obj) |
| 182 |
print(f"build-and-test: using {tempd} as a temporary directory") |
| 183 |
|
| 184 |
u8env: Final = dict(os.environ) |
| 185 |
u8env["LC_ALL"] = "C.UTF-8" |
| 186 |
if "LANGUAGE" in u8env: |
| 187 |
del u8env["LANGUAGE"] |
| 188 |
|
| 189 |
srcdir: Final = copy_source(tempd, u8env) |
| 190 |
print(f"build-and-test: copied the debsigs source to {srcdir}") |
| 191 |
|
| 192 |
bindir: Final = tempd / "bin" |
| 193 |
bindir.mkdir(mode=0o755) |
| 194 |
wrapper: Final = build_debsigs(debsigs_impl, bindir, srcdir, u8env) |
| 195 |
print(f"build-and-test: built the debsigs source, created {wrapper}") |
| 196 |
|
| 197 |
print("build-and-test: invoking testsigs") |
| 198 |
subprocess.check_call( # noqa: S603 |
| 199 |
[ |
| 200 |
sys.executable, |
| 201 |
"-B", |
| 202 |
"-u", |
| 203 |
"-m", |
| 204 |
"testsigs", |
| 205 |
"--debsigs-program", |
| 206 |
wrapper, |
| 207 |
"--debsig-verify-program", |
| 208 |
debsig_verify_program, |
| 209 |
"--gnupg-program", |
| 210 |
gnupg_program, |
| 211 |
"--sop-program", |
| 212 |
sop_program, |
| 213 |
"--test-datadir", |
| 214 |
srcdir / "test-data", |
| 215 |
], |
| 216 |
cwd=tempd, |
| 217 |
env=u8env, |
| 218 |
) |
| 219 |
|
| 220 |
print("build-and-test: seems fine") |
| 221 |
|
| 222 |
|
| 223 |
if __name__ == "__main__": |
| 224 |
main() |