Skip to main content

a PDM build hook for Polylith

Project description

PDM Build Hook for Polylith

A plugin for PDM and the Polylith Architecture.

This build hook will look for Polylith bricks in pyproject.toml and optionally re-write the imports made in the source code.

Installation

[build-system]
requires = ["pdm-backend", "pdm-polylith-bricks"]
build-backend = "pdm.backend"

The main usage of the build hook is to identify the included Polylith bricks from the pyproject.toml, and hand them over to the PDM build process. Bricks are added to a project with relative paths, from the bases and components folders in a Polylith Workspace. The hook will copy the bricks into the temporary .pdm-build folder that is created by the pdm build command. This will make the built wheel and sdist include proper paths to the source code.

Polylith Bricks are defined in the tool.polylith.bricks section of the pyproject.toml:

[tool.polylith.bricks]
"../../bases/my_namespace/my_base" = "my_namespace/my_base"
"../../components/my_namespace/my_component" = "my_namespace/my_component

Polylith documentation

the Python tools for the Polylith Architecture

The build hook also add support for building Python libraries by re-writing source code with a custom top namespace.

Why re-write code?

Building libraries is supported in the Python tools for the Polylith Architecture, but you will need to consider that code will share the same top namespace with any other library built from the same monorepo.

This can be a problem when more than one of your libraries are installed into the same virtual environment. Python libraries by default are installed in a "flat" folder structure, two libraries with the same top namespace will collide.

A Solution: add a custom top namespace during packaging of the library with PDM and this build hook.

How is this done?

The code in this repo uses AST (Abstract Syntax Tree) parsing to modify source code. The Python built-in ast module is used to parse and un-parse Python code.

What's the output from this plugin?

Without any custom namespace in the configuration: no changes in the code. Building and packaging as-is.

With a Top Namespace configuration

[tool.polylith.build]
top-namespace = "my_custom_namespace"
my_custom_namespace/
    my_namespace/
        /my_package
           __init__.py
           my_module.py

Before:

from my_namespace.my_package import my_function

After:

from my_custom_namespace.my_namespace.my_package import my_function

Polylith Documentation

the Python tools for the Polylith Architecture

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

pdm_polylith_bricks-1.0.9.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

pdm_polylith_bricks-1.0.9-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file pdm_polylith_bricks-1.0.9.tar.gz.

File metadata

  • Download URL: pdm_polylith_bricks-1.0.9.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for pdm_polylith_bricks-1.0.9.tar.gz
Algorithm Hash digest
SHA256 9d2946fb7ee7736d3b7f2b36869f1e021c7416377e31cb00619a060993c70e23
MD5 2333782f4f2cbe04f528e2bdf70e0c65
BLAKE2b-256 91b2429298ceaa27194d27f79357e4c01809ec2369e9b0410fce2422419f5988

See more details on using hashes here.

File details

Details for the file pdm_polylith_bricks-1.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for pdm_polylith_bricks-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 f7c3e98ed1f27abef8aa322849bf27d041e8377b9612bc1f481c6b16f4a7eb83
MD5 145347c74def5de132ac212d85a210ae
BLAKE2b-256 f968d794e57d86a942f6fca221a601470f67a16093c1b6da7f79a8d2e6f71378

See more details on using hashes here.

Supported by

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