|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +import argparse |
| 6 | +import hashlib |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import shutil |
| 10 | +import tempfile |
| 11 | +import yaml |
| 12 | + |
| 13 | +# Parse command-line arguments |
| 14 | +parser = argparse.ArgumentParser( |
| 15 | + description="Build a Debian source package with a debdiff." |
| 16 | +) |
| 17 | +parser.add_argument( |
| 18 | + '--config', |
| 19 | + type=str, |
| 20 | + help='Path to the YAML configuration file' |
| 21 | +) |
| 22 | +args = parser.parse_args() |
| 23 | + |
| 24 | +# Load configuration from YAML file |
| 25 | +with open(args.config, 'r') as f: |
| 26 | + config = yaml.safe_load(f) |
| 27 | + |
| 28 | +# Create a temporary directory |
| 29 | +temp_dir = tempfile.mkdtemp() |
| 30 | + |
| 31 | +# Download the original Debian source package using dget |
| 32 | +dsc_url = config['dsc_url'] |
| 33 | +subprocess.run( |
| 34 | + ['dget', '-d', dsc_url], |
| 35 | + cwd=temp_dir, |
| 36 | + check=True |
| 37 | +) |
| 38 | +print("✅ Original source package downloaded successfully.") |
| 39 | + |
| 40 | +# Determine the .dsc file name |
| 41 | +dsc_file = os.path.join(temp_dir, os.path.basename(dsc_url)) |
| 42 | + |
| 43 | +# Verify the SHA256 checksum of the .dsc file |
| 44 | +with open(dsc_file, 'rb') as f: |
| 45 | + file_data = f.read() |
| 46 | + sha256sum = hashlib.sha256(file_data).hexdigest() |
| 47 | + |
| 48 | +expected_sha256 = config['dsc_sha256sum'] |
| 49 | +if sha256sum != expected_sha256: |
| 50 | + raise ValueError( |
| 51 | + f"SHA256 checksum does not match!\n" |
| 52 | + f"Expected: {expected_sha256}\n" |
| 53 | + f"Actual: {sha256sum}" |
| 54 | + ) |
| 55 | +print("✅ Checksum of original source package matched.") |
| 56 | + |
| 57 | +# Unpack the source package |
| 58 | +subprocess.run( |
| 59 | + ['dpkg-source', '-x', dsc_file], |
| 60 | + cwd=temp_dir, |
| 61 | + check=True |
| 62 | +) |
| 63 | + |
| 64 | +# Find the unpacked directory (assumes it's the only directory created) |
| 65 | +unpacked_dirs = [ |
| 66 | + d for d in os.listdir(temp_dir) |
| 67 | + if os.path.isdir(os.path.join(temp_dir, d)) |
| 68 | +] |
| 69 | +if not unpacked_dirs: |
| 70 | + raise RuntimeError("No unpacked source directory found.") |
| 71 | +unpacked_dir = os.path.join(temp_dir, unpacked_dirs[0]) |
| 72 | + |
| 73 | +# Apply the debdiff |
| 74 | +debdiff_path = config.get('debdiff_file') |
| 75 | +if debdiff_path: |
| 76 | + if not os.path.isabs(debdiff_path): |
| 77 | + config_dir = os.path.dirname(args.config) |
| 78 | + debdiff_path = os.path.abspath(os.path.join(config_dir, debdiff_path)) |
| 79 | + subprocess.run( |
| 80 | + ['patch', '-p1', '-i', debdiff_path], |
| 81 | + cwd=unpacked_dir, |
| 82 | + check=True |
| 83 | + ) |
| 84 | + print("✅ Debdiff applied successfully.") |
| 85 | +else: |
| 86 | + print("⚠️ No debdiff provided.") |
| 87 | + |
| 88 | +# Build the resulting source package using sbuild |
| 89 | +suite = config['suite'] |
| 90 | +subprocess.run( |
| 91 | + ['sbuild', '--verbose', '-d', suite, '--no-clean-source', |
| 92 | + '--dpkg-source-opt=--no-check'], |
| 93 | + cwd=unpacked_dir, |
| 94 | + check=True |
| 95 | +) |
| 96 | + |
| 97 | +print("✅ Source package built successfully.") |
| 98 | + |
0 commit comments