rules_python

repository·main·Indexed 20 days ago

https://github.com/bazel-contrib/rules_python

Essential Python rules for Bazel, providing py_library, py_binary, and py_test. It enables scalable Python builds, PyPI integration, and dependency management via requirements.txt or PEP621 compliant pyproject.toml. Features include a Gazelle extension for automatic target generation, support for hermetic toolchains, and the bazel-runfiles library for accessing runfile paths.

Tokens
54.1K
Snippets
161
Records
235
Agent score
71%

What's inside rules_python

  1. Overview of sphinxdocs capabilities

    main

    The sphinxdocs project enables documentation generation using Sphinx within a Bazel environment. While it is optimized for documenting Starlark code, the core functionality is agnostic to the content being documented.

    Key features include:

    • Rules for running Sphinx.
    • Rules for generating documentation specifically for Starlark code.
    • A Sphinx plugin designed for documenting Starlark and Bazel objects.
    • Rules for integrating builds with Read the Docs.
  2. Overview of PyPI integration capabilities

    main

    The rules_python repository contains integration code for interacting with PyPI and other compatible indexes. The current capabilities include:

    • Package Downloading: Support for downloading packages using pip or repository_ctx.download.
    • Index Interaction: Interacting with PyPI-compatible indexes that follow the [SimpleAPI] specification.
    • Dependency Locking: Support for locking dependencies from requirements.in files or [PEP621] compliant pyproject.toml files.
  3. Overview of rules_python

    main

    The rules_python repository provides the core Python rules for Bazel, enabling Python development within a Bazel workspace. It includes standard rules for defining Python components and tools for managing package dependencies from PyPI or other indices.

    Key components include:

    • Core Rules: py_library, py_binary, and py_test.
    • Dependency Management: Rules for integrating with PyPI and other package indices.
    • Standards Support: Implementation of PEP440 and PEP509 via Starlark.
  4. Understand the rules_python support policy

    main
    As a community-maintained project run by volunteers, support for rules_python is provided on a best-effort basis. Responses to issues and pull requests depend on volunteer availability. If you require specific functionality or support, the project encourages users to contribute directly via pull requests.
  5. Understand the components of rules_python

    main

    rules_python consists of four main components, each with different maturity levels and support policies:

    1. Core rules: Provides py_library, py_binary, py_test, and related symbols. On Bazel 7+, these use a separate Starlark implementation. These follow semantic versioning.
    2. PyPI integration: Rules for integrating with PyPI and Simple API-compatible indexes. Cross-platform building (pulling dependencies for a target platform different from the host) is currently in beta, and APIs marked experimental are subject to change.
    3. Sphinxdocs: Rules for generating documentation using Sphinx. Note that semantic versioning and compatibility policies of rules_python do not apply to this component.
    4. Gazelle plugin: A plugin for generating BUILD.bazel files from Python source code. This is provided "as is" and does not follow rules_python's semantic versioning.
  6. What is a simple label?

    main

    A simple label is a str or Label object that is not a direct select object.

    Because select objects represent configuration-dependent values, they cannot undergo standard string manipulation. If an attribute requires a simple label, you cannot pass a select directly to it. However, if an alias is used, you can often pass a reference to that alias to maintain configurability.

  7. What is the Gazelle Plugin for Python

    main

    The Gazelle plugin for rules_python is a build file generator for Bazel projects. It automates the creation of new BUILD or BUILD.bazel files and updates existing ones to include new sources, dependencies, and options based on Python language conventions.

    Key Behaviors

    • Non-destructive updates: Gazelle attempts to leave your manual edits to BUILD files intact, focusing updates specifically on py_* targets.
    • Dependency management: Gazelle will remove dependencies that it identifies as unused. It is recommended to commit your work before running Gazelle so you can revert changes if necessary.
    • Commands: While the gazelle program supports multiple commands, currently only the update command (which is the default) performs actions for Python code.
  8. What is precompiling in rules_python

    main

    Precompiling is the process of compiling Python source files (.py) into bytecode (.pyc) at build time rather than at runtime.

    Benefits:

    • Improves runtime performance by skipping the compilation step when the application starts.

    Costs/Trade-offs:

    • Increased Runfiles Size: It approximately doubles the count and disk usage of runfiles because every .py file is accompanied by a .pyc file.
    • Build Overhead: Requires an extra action during the build process, which can increase build times as the number of files grows.
  9. What is a rule callable?

    main

    A rule callable is a function that behaves like a Bazel rule. To be considered a rule callable, a function must:

    • Accept a name argument and other common attributes.
    • Return None (no return value).
    • Create at least one target named after the provided name.

    Note that there is usually an implicit interface regarding which specific attributes and values are accepted; you should refer to the specific API documentation for the rule you are using.

  10. Handle advanced PyPI dependency scenarios

    main

    When managing PyPI dependencies, you may encounter complex scenarios that require specific handling:

    • Circular Dependencies: Use specialized strategies if your Python dependency graph contains cycles.
    • Multi-platform Dependencies: Handle packages that require different dependencies depending on the target operating system or architecture.
    • Patching: Apply patches to third-party packages if they require fixes to work within the Bazel environment.
  11. How Gazelle generates Python targets

    main

    Gazelle identifies Python source files as *.py files, excluding those matching the # gazelle:python_test_file_pattern directive (defaults to *_test.py and test_*.py).

    Libraries

    Depending on the # gazelle:python_generation_mode directive:

    • package mode: Creates a py_library named after the package in the BUILD file, collecting all source files in that package.
    • project mode: Collects source files from subdirectories that lack their own BUILD files.
    • file mode: Creates a unique py_library target for every individual Python source file.

    Tests

    Gazelle creates py_test targets when it encounters:

    • Files named __test__.py.
    • Files matching the # gazelle:python_test_file_pattern directive.

    You can customize test naming using the # gazelle:python_test_naming_convention directive.

    Binaries

    Gazelle creates py_binary targets in two scenarios:

    1. Entry point file: If a __main__.py file is found, it creates a target named [package]_bin.
    2. Main block: If no __main__.py exists, it looks for if __name__ == "__main__": in modules. The target name matches the module name.

    Note: In file generation mode, Gazelle creates one py_binary per file containing a main block.