Skip to main content

Pack charms with local Python package dependencies

Project description

charmcraftlocal

Pack charms with local Python package dependencies

Installation

Install pipx: https://pipx.pypa.io/stable/installation/

pipx install charmcraftlocal

Usage

At the moment, only charms that manage Python dependencies with Poetry are supported.

Example directory layout

common/
    # Local Python package with shared code
    pyproject.toml
    common/
        __init__.py
kubernetes/
    charmcraft.yaml
    pyproject.toml
    poetry.lock
    # [...]
machines/
    charmcraft.yaml
    pyproject.toml
    poetry.lock
    # [...]
Example common/pyproject.toml
[project]
name = "common"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
]

[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"

Step 1: Add local package to charm dependencies

Repeat this step for each charm that depends on the local package.

poetry add ../common --editable

Example pyproject.toml

[tool.poetry.dependencies]
common = {path = "../common", develop = true}

Step 2: Install charmcraftlocal in charmcraft.yaml

Install charmcraftlocal in the poetry-deps charmcraft.yaml part.

Example charmcraft.yaml

parts:
  poetry-deps:
    plugin: nil
    override-build: |
      # [...]
      "$HOME/.local/bin/uv" tool install --no-python-downloads charmcraftlocal

To install with pipx instead of uv, use pipx install charmcraftlocal

Step 3: Call charmcraftlocal in charmcraft.yaml

Run charmcraftlocal update-lock before poetry export runs in the charmcraft.yaml part with plugin: poetry

Example charmcraft.yaml

parts:
  # [...]
  charm-poetry:
    plugin: poetry
    after:
      - poetry-deps
    override-build: |
      # [...]
      "$HOME/.local/bin/charmcraftlocal" update-lock -v

      craftctl default
      # [...]

Step 4: Pack charm

ccl pack

How it works

Currently, during charmcraft pack, charmcraft can only access files in the directory that contains charmcraft.yaml.

charmcraftlocal pack

  • searches (the charm's) pyproject.toml for local Python dependencies,
  • copies them to the charm directory,
  • and calls charmcraft pack

charmcraftlocal update-lock (called by charmcraft pack)

  • searches (the charm's) pyproject.toml for local Python dependencies
  • and calls Poetry to update pyproject.toml and poetry.lock to reference the copied package(s)

Why does charmcraftlocal need to be called in charmcraft.yaml?

In the first prototype of charmcraftlocal

  • only ccl pack needed to be called,
  • ccl pack called Poetry to update pyproject.toml and poetry.lock,
  • and ccl update-lock did not exist

The design was changed to maintain compatibility with tooling that expects

  • one charm per git repository,
  • charmcraft.yaml located at the root of the git repository,
  • and charmcraft pack to successfully build the charm

To maintain compatibility with that tooling, the ccl mirror command automates the creation and updates of "mirror" git repositories that meet the above requirements.

For example, for a git repository with this directory layout

common/
    # Local Python package with shared code
    pyproject.toml
    common/
        __init__.py
kubernetes/
    charmcraft.yaml
    pyproject.toml
    poetry.lock
    # [...]
machines/
    charmcraft.yaml
    pyproject.toml
    poetry.lock
    # [...]

ccl mirror can be used to create two "mirror" repositories—

one for kubernetes

common/
    # Local Python package with shared code
    pyproject.toml
    common/
        __init__.py
charmcraft.yaml
pyproject.toml
poetry.lock
# [...]

and one for machines

common/
    # Local Python package with shared code
    pyproject.toml
    common/
        __init__.py
charmcraft.yaml
pyproject.toml
poetry.lock
# [...]

git-filter-repo is used to create these "mirror" repositories. In order to preserve commit history while avoiding merge conflicts, pyproject.toml and poetry.lock must not be modified between the source repository and the "mirror" repositories.

Therefore, pyproject.toml and poetry.lock are modified at build time by calling ccl update-lock in charmcraft.yaml. (This approach enables the "mirror" repositories to be successfully built with charmcraft pack.)

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

charmcraftlocal-0.3.6.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

charmcraftlocal-0.3.6-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file charmcraftlocal-0.3.6.tar.gz.

File metadata

  • Download URL: charmcraftlocal-0.3.6.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for charmcraftlocal-0.3.6.tar.gz
Algorithm Hash digest
SHA256 4be1c87f1538c158d3efd8675c6a979dbac52bc88e9e45551ce80ad5f9b05edd
MD5 4eedfbaf3bb65bd8beae0f08fcaca5aa
BLAKE2b-256 f35c36ba66be98f9cde7c8aca6213469a702da3c009187cd8455ec733e81a48f

See more details on using hashes here.

Provenance

The following attestation bundles were made for charmcraftlocal-0.3.6.tar.gz:

Publisher: release.yaml on canonical/charmcraftlocal

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file charmcraftlocal-0.3.6-py3-none-any.whl.

File metadata

File hashes

Hashes for charmcraftlocal-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7831265d0683b211f4a775d38d980bf1f91a970ea18f718fc68275a434c9be7f
MD5 69daf79b24e0b24db277f1ee465f8edb
BLAKE2b-256 a8a17a0b404d999ac53f4fe75c321b09276aa441e2ae576bb15cd8a43215b7b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for charmcraftlocal-0.3.6-py3-none-any.whl:

Publisher: release.yaml on canonical/charmcraftlocal

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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