Skip to main content

Minimal Python library to programmatically construct Debian .deb packages

Project description

Coverage Status tests PyPI - Version PyPI - Types PyPI - License

debx

debx logo

Pronounced "deb-ex", debx is a Python library for creating, reading, and manipulating Debian package files. This package includes the debx command-line tool for packing, unpacking, and inspecting any .deb packages.

Features

  • Cross-platform support for creating and unpacking .deb packages. Yes you can create .deb packages on Windows!
  • Read and extract content from Debian packages
  • Create custom Debian packages programmatically
  • Parse and manipulate Debian control files (RFC822-style format)
  • Low-level AR archive manipulation
  • No external dependencies - uses only Python standard library
  • Command-line interface for creating and unpacking .deb packages

Installation

pip install debx

Quick Start

Reading a Debian Package

from debx import DebReader

# Open a .deb file
with open("package.deb", "rb") as f:
    reader = DebReader(f)

    # Extract control file
    control_file = reader.control.extractfile("control")
    control_content = control_file.read().decode("utf-8")
    print(control_content)
    
    # List files in the data archive
    print(reader.data.getnames())
    
    # Extract a file from the data archive
    file_data = reader.data.extractfile("usr/bin/example").read()

Creating a Debian Package

from debx import DebBuilder, Deb822

# Initialize the builder
builder = DebBuilder()

# Create control information
control = Deb822({
    "Package": "example",
    "Version": "1.0.0",
    "Architecture": "all",
    "Maintainer": "Example Maintainer <maintainer@example.com>",
    "Description": "Example package\n This is an example package created with debx.",
    "Section": "utils",
    "Priority": "optional"
})

# Add control file
builder.add_control_entry("control", control.dump())

# Add files to the package
builder.add_data_entry(b"#!/bin/sh\necho 'Hello, world!'\n", "/usr/bin/example", mode=0o755)

# Add a symlink
builder.add_data_entry(b"", "/usr/bin/example-link", symlink_to="/usr/bin/example")

# Build the package
with open("example.deb", "wb") as f:
    f.write(builder.pack())

Working with Debian Control Files

from debx import Deb822

# Parse a control file
control = Deb822.parse("""
Package: example
Version: 1.0.0
Description: Example package
 This is a multi-line description
 with several paragraphs.
""")

print(control["Package"])  # "example"
print(control["Description"])  # Contains the full multi-line description

# Modify a field
control["Version"] = "1.0.1"

# Add a new field
control["Priority"] = "optional"

# Write back to string
print(control.dump())

Command-Line Interface

debx includes a command-line interface for packing and unpacking Debian packages.

Packing a Debian Package

The pack command allows you to create a .deb package from files on your system:

debx pack \
    --control control:control \
              preinst:preinst:mode=0755 \
    --data src/binary:/usr/bin/example:mode=0755 \
           src/config:/etc/example/config \
           src/directory:/opt/example \
    --output example.deb

The format for specifying files is:

source_path:destination_path[:modifiers]

Available modifiers:

  • mode=0755 - Set file permissions
  • uid=1000 - Set file owner ID
  • gid=1000 - Set file group ID
  • mtime=1234567890 - Set file modification time

When specifying a directory, all files within that directory will be included in the package while preserving the directory structure.

Unpacking a Debian Package

The unpack command extracts a .deb package into a directory:

debx unpack package.deb --directory output_dir

This will extract the internal AR archive members and tar archives (debian-binary, control/, data/) into the specified directory.

Inspecting a Debian Package

The inspect command allows you to view the contents of a .deb package in different formats:

debx inspect package.deb  # --format=ls (default)

This will display the contents of the control file and the list of files in the data archive.

You can also specify the format to view the control file in different formats:

debx inspect --format=json package.deb 

See the --help option for more details on available formats.

Package signing

The sign command allows you to sign a .deb package using GPG:

debx sign --extract mypackage.deb | \
  gpg --armor --detach-sign --output - | \
  debx sign --update mypackage.deb -o mypackage.signed.deb

This will extract the package, sign it with GPG, and update the package with the signature. The --extract option extracts the package payload and streams it to stdout, which is then signed with GPG. The --update option updates the package with the signature.

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

debx-0.2.6.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

debx-0.2.6-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file debx-0.2.6.tar.gz.

File metadata

  • Download URL: debx-0.2.6.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for debx-0.2.6.tar.gz
Algorithm Hash digest
SHA256 e9b32040e707e20bc6f1ae3c5c4dfafa8611a8e5b25ba6f0629e59edf6e1f2c7
MD5 71373a6b5a66677d7a193ea5d5e3867a
BLAKE2b-256 59452cdabb89e43a684723402989e570582896e82d8129967e1d24bd248fe66f

See more details on using hashes here.

File details

Details for the file debx-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: debx-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.2

File hashes

Hashes for debx-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 68c8c108cfb4006d98ad0bbdd52f6be5b38cbaf4193b61336a151f8534739be8
MD5 b6d8dafa4502b2f712ee7ab355cae796
BLAKE2b-256 f1e123f19086a9bdc09dd1ca0f89d258fa95f163f3f9f61a35959679ebe836ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page