Skip to main content

porifera

PHP AST instrumentation for collecting runtime values at supported expression sites. The name comes from the sponge phylum Porifera.

Install

pip install porifera

Requires Python >=3.10.

Usage

The example requires a php executable on PATH; it is the target, and its php -l is the compile check of the staged bytes.

import subprocess
from collections.abc import Mapping
from pathlib import Path
from tempfile import TemporaryDirectory

from php_parser_py import Parser, PHPVersion

import porifera

VERSION = "echo PHP_MAJOR_VERSION, ' ', PHP_MINOR_VERSION;"


def php_version() -> PHPVersion:
    found = subprocess.run(
        ["php", "-r", VERSION], capture_output=True, text=True, check=True
    )
    major, minor = found.stdout.split()
    return PHPVersion(int(major), int(minor))


def compile_check(target: PHPVersion, staged: Mapping[Path, bytes]) -> Mapping[Path, str]:
    if php_version() != target:
        raise RuntimeError(f"php on PATH is not PHP {target}")
    refused = {}
    for path, content in staged.items():
        checked = subprocess.run(["php", "-l"], input=content, capture_output=True)
        if checked.returncode:
            refused[path] = (checked.stderr + checked.stdout).decode("utf-8", errors="replace")
    return refused


with TemporaryDirectory() as directory:
    source = Path(directory) / "example.php"
    source.write_text("<?php\n$price = 40 + 2;\n", encoding="utf-8")
    target = php_version()
    ast = Parser(target_php=target).parse_file(str(source))
    node = ast.first_node(lambda item: item.node_type == "Expr_BinaryOp_Plus")
    assert node is not None
    with porifera.session(
        {node.id: "price"}, ast, target_php=target, compile_check=compile_check
    ) as active:
        subprocess.run(["php", str(source)], check=True)
        print(active.collect()["price"].values)

See session states and exit cleanup.

API

  • session(targets, project_ast, *, target_php, compile_check, record_tag_expression=None, ...) enters an instrumentation session and yields a Session.
  • Session.collect() returns the exact values of ticketless requests by key; Session.collect_unassigned() returns every record of those requests with its status and Record Tag as UnassignedRecords. Both consume the same output.
  • Session.register_attempt(), Session.collect(attempt) and Session.ack(batch) collect ticketed requests as a CaptureBatch of CaptureRecord.
  • plan_targets(), recover(), recover_capture() and recorded_session() plan targets and recover a root.

Signatures, result types and errors: session API and capture API.

Probe strategies, outcome types and deployment callbacks are specified in the public API. Target admission selects Expression Wrap, Wrap-Assign, Statement Probe or a refusal.

Documentation

For Agents

Start at the agent usage index.

License

GPL-2.0 — see LICENSE.

Metadata

Release files for porifera 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for porifera 0.4.0
File Size Uploaded Provenance Actions
porifera-0.4.0.tar.gz 97.0 kB Without provenance Details for porifera-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for porifera 0.4.0
File Interpreter ABI Platform Actions
porifera-0.4.0-py3-none-any.whl Python 3 none any Details for porifera-0.4.0-py3-none-any.whl

Total release size: 220.8 kB

Release files / porifera-0.4.0.tar.gz

Download URL porifera-0.4.0.tar.gz
Size 97.0 kB
Tags Source
SHA-256 checksum
How to use checksums
226a3c03b280984e046f133ec18a49cbc735ae989297a6e321d2045d44181abe
BLAKE2b-256 checksum
How to use checksums
64673ef1cb263e8b48e0c8540ecc74c9689799cbe8e9595968b2a8079cb0c567
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / porifera-0.4.0-py3-none-any.whl

Download URL porifera-0.4.0-py3-none-any.whl
Size 123.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ac3597e624c4ed13898f407083afdcd7b2b6aead8f464aec600cde5ec2a014c
BLAKE2b-256 checksum
How to use checksums
6efcb40553b4bbe9be58902b562600ee799031dfd8b6f6c12d47af2b7ebb0676
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for porifera 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page