Skip to content

Commit db7b283

Browse files
committed
Add script to package to debian with docker #341
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent e999471 commit db7b283

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

build_deb_docker.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Use Docker container to build a Debian package.
4+
Using Docker approach to ensure a consistent and isolated build environment.
5+
6+
Requirement: The `toml` Python package
7+
8+
pip install toml
9+
10+
To run the script:
11+
12+
python build_deb_docker.py
13+
14+
This script will generate the Debian package files and place them in the
15+
dist/ directory.
16+
"""
17+
18+
import os
19+
import shutil
20+
import subprocess
21+
import sys
22+
from pathlib import Path
23+
24+
import toml
25+
26+
27+
def build_deb_with_docker():
28+
# Load the pyproject.toml file
29+
with open('pyproject.toml') as f:
30+
project = toml.load(f)['project']
31+
32+
pkg_name = project['name']
33+
# Insert "python3-"" prefix that follows a common convention
34+
deb_name = f"python3-{pkg_name.lower()}"
35+
36+
# Debian version conventions replace hyphens with tildes
37+
deb_version = project['version'].replace("-dev", "~dev")
38+
39+
docker_cmd = [
40+
'docker', 'run', '--rm',
41+
'-v', f"{os.getcwd()}:/workspace",
42+
'-w', '/workspace',
43+
'ubuntu:22.04',
44+
'/bin/bash', '-c',
45+
f"""set -ex
46+
# Install build dependencies
47+
apt-get update
48+
apt-get install -y python3-dev python3-pip python3-venv debhelper dh-python devscripts
49+
50+
# Install build tool
51+
pip install build
52+
53+
# Build the wheel
54+
python3 -m build --wheel
55+
56+
# Get the wheel file name
57+
WHEEL_FILE=$(ls dist/*.whl)
58+
if [ -z "$WHEEL_FILE" ]; then
59+
echo "Error: No wheel file found in dist/." >&2
60+
exit 1
61+
fi
62+
63+
# Create temporary directory for package building
64+
TEMP_DIR=$(mktemp -d)
65+
PKG_DIR="$TEMP_DIR/{deb_name}-{deb_version}"
66+
mkdir -p "$PKG_DIR"
67+
68+
# Extract wheel contents to package directory
69+
unzip "$WHEEL_FILE" -d "$PKG_DIR"
70+
71+
# Create DEBIAN control file
72+
mkdir -p "$PKG_DIR/DEBIAN"
73+
cat > "$PKG_DIR/DEBIAN/control" << EOF
74+
Package: {deb_name}
75+
Version: {deb_version}
76+
Architecture: all
77+
Maintainer: {project.get('authors', [{}])[0].get('name', 'nexB Inc.')}
78+
Description: {project.get('description', 'Automate open source license compliance and ensure supply chain integrity')}
79+
Depends: python3
80+
Section: python
81+
Priority: optional
82+
Homepage: {project.get('urls', '').get('Homepage', 'https://github.com/aboutcode-org/dejacode')}
83+
EOF
84+
85+
# Build the .deb package
86+
dpkg-deb --build "$PKG_DIR" dist/
87+
88+
# Clean up
89+
rm -rf "$TEMP_DIR"
90+
91+
# Fix permissions for Windows host
92+
chmod -R u+rwX dist
93+
"""
94+
]
95+
96+
try:
97+
subprocess.run(docker_cmd, check=True)
98+
# Verify the existence of the .deb
99+
deb_file = next(Path('dist').glob('*.deb'), None)
100+
if deb_file:
101+
print(f"\nSuccess! Debian package built: {deb_file}")
102+
else:
103+
print("Error: Debian package not found in dist/", file=sys.stderr)
104+
sys.exit(1)
105+
except subprocess.CalledProcessError as e:
106+
print(f"Build failed: {e}", file=sys.stderr)
107+
sys.exit(1)
108+
109+
110+
if __name__ == '__main__':
111+
# Check if "docker" is available
112+
if not shutil.which('docker'):
113+
print("Error: Docker not found. Please install Docker first.", file=sys.stderr)
114+
sys.exit(1)
115+
116+
build_deb_with_docker()

0 commit comments

Comments
 (0)