Skip to main content

Install rust into a temporary directory for boostrapping a rust-based build backend

Project description

puccinialin

Install rust into a temporary directory to support rust-based builds.

Cargo and rustc are installed into a cache directory, to avoid modifying the host's environment, and activated using a set of environment variables.

The difficulty is mapping one of the various sources python platform information (sys, platform, sysconfig, etc.) to a rustc target triple (https://doc.rust-lang.org/nightly/rustc/platform-support.html). This project mainly uses SOABI for detection, but can be extended to use other APIs for other platforms. By using rustup, it is currently limited to the platforms listed under https://rust-lang.github.io/rustup/installation/other.html#manual-installation. SOABI, which contains the file extensions of native modules.

Contributing

The platform specific logic is in src/puccinialin/_target.py, specifically get_triple.

Usage: Python

There is a single setup_rust function that takes the installation directory, or uses the user cache directory by default. It returns a dict of environment variables to be used when calling rust.

import os
from subprocess import check_call

from puccinialin import setup_rust

extra_env = setup_rust("path/to/installation/dir")
check_call(["cargo", "build"], env={**os.environ, **extra_env})

With setuptools-rust:

import os
import shutil

from setuptools import setup
from setuptools_rust import RustBin

if not shutil.which("cargo"):
    from puccinialin import setup_rust

    print("Rust not found, installing into a temporary directory")
    extra_env = setup_rust()
    env = {**os.environ, **extra_env}
else:
    env = None

setup(
    ...,
    rust_extensions=[RustBin(..., env=env)],
)

You can use it as custom build backend to avoid the dependency when not needed, as e.g. in maturin:

"""Support installing rust before compiling maturin.

Installing a package that uses maturin as build backend on a platform without maturin
binaries, we install rust in a cache directory if the user doesn't have a rust
installation already. Since this bootstrapping requires more dependencies but is only
required if rust is missing, we check if cargo is present before requesting those
dependencies.

https://setuptools.pypa.io/en/stable/build_meta.html#dynamic-build-dependencies-and-other-build-meta-tweaks
"""

from __future__ import annotations

import os
import shutil
from typing import Any

# noinspection PyUnresolvedReferences
from setuptools.build_meta import *


def get_requires_for_build_wheel(_config_settings: dict[str, Any] = None) -> list[str]:
    if not os.environ.get("MATURIN_NO_INSTALL_RUST") and not shutil.which("cargo"):
        return ["puccinialin"]
    return []


def get_requires_for_build_sdist(_config_settings: dict[str, Any] = None) -> list[str]:
    if not os.environ.get("MATURIN_NO_INSTALL_RUST") and not shutil.which("cargo"):
        return ["puccinialin"]
    return []

Usage: CLI

$ python -m puccinialin --help
usage: __main__.py [-h] [--location LOCATION] [--program PROGRAM] [--info-json INFO_JSON]

options:
  -h, --help            show this help message and exit
  --location LOCATION   The directory for installing rust to
  --program PROGRAM     The name of the installation directory in the cache, if `--location` was not used. Defaults to 'puccinialin'.
  --info-json INFO_JSON
                        Write the new environment variables as JSON to this file

Implementation

Setting up rust consists of 4 steps:

  • Determine the platform in terms of a rust target triple
  • Download rustup-init for this target triple
  • Use rustup to install rust and cargo for this target
  • Report the environment variables to use this rust installation

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

puccinialin-0.1.9.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

puccinialin-0.1.9-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file puccinialin-0.1.9.tar.gz.

File metadata

  • Download URL: puccinialin-0.1.9.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for puccinialin-0.1.9.tar.gz
Algorithm Hash digest
SHA256 4352220f48cea6f4029dfa7782a7836dbc7ac3400043d199436b16276e4dd23f
MD5 90ae173898d99d596dfa576b7a75f6c3
BLAKE2b-256 29103bb8b3c73e6104545df9c17cb83ea60fc3bb307975c738d4d2e2716fea37

See more details on using hashes here.

File details

Details for the file puccinialin-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: puccinialin-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for puccinialin-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 2b48c1c419109e4c40ab69bc090cb3bc8918e752ac248254dd2c9ed4380f9f0c
MD5 aba3a3025eec955c5b56194e4f5ff966
BLAKE2b-256 f21a9f34a606602cb4149ec313d6ebf2c22d49e9d35264dae1659386fcabd3a7

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