Skip to main content

Record calls of Python CLI commands into a Research Object Crate

Project description

rocrate-action-recorder

github repo badge github license badge CI readthedocs

Python package to record calls of Python CLI commands into a Research Object Crate (RO-Crate).

Supports RO-Crate 1.1 specification. Specifically the Process Run Crate profile.

Install

pip install rocrate-action-recorder

Usage

Shown is an example of recording a CLI command (example-cli input.txt output.txt) implemented with argparse.

import argparse
from datetime import datetime
from pathlib import Path
from rocrate_action_recorder import record_with_argparse, IOArgumentNames

# Create an argparse parser
parser = argparse.ArgumentParser(prog="example-cli", description="Example CLI")
parser.add_argument("--version", action="version", version="1.2.3")
parser.add_argument("input", type=Path, help="Input file")
parser.add_argument("output", type=Path, help="Output file")

# Prepare input
Path("input.txt").write_text("hello")

# Parse arguments
args = ['input.txt', 'output.txt']
ns = parser.parse_args(args)

# Do handling of the CLI command here
start_time = datetime.now()
# For demonstration, just upper case input to output
Path(ns.output).write_text(ns.input.read_text().upper())

record_with_argparse(
    parser, 
    ns, 
    # Tell recorder which arguments are for input and output files
    IOArgumentNames(input_files=["input"], output_files=["output"]),
    start_time, 
    dataset_license="CC-BY-4.0",
    # argv argument is optional, in real usage you can omit it
    argv=['example-cli'] + args,
    # current_user argument is optional, in real usage you can omit it
    current_user="someuser"
)
Will generate a `ro-crate-metadata.json` file in the current working directory describing the execution of the CLI command. (Click me to see crate content)
{
    "@context": [
        "https://w3id.org/ro/crate/1.1/context",
        "https://w3id.org/ro/terms/workflow-run/context"
    ],
    "@graph": [
        {
            "@id": "./",
            "@type": "Dataset",
            "conformsTo": {
                "@id": "https://w3id.org/ro/wfrun/process/0.5"
            },
            "datePublished": "2026-01-28T15:07:18.600135",
            "description": "An RO-Crate recording the files and directories that were used as input or output by example-cli.",
            "hasPart": [
                {
                    "@id": "input.txt"
                },
                {
                    "@id": "output.txt"
                }
            ],
            "license": "CC-BY-4.0",
            "name": "Files used by example-cli"
        },
        {
            "@id": "ro-crate-metadata.json",
            "@type": "CreativeWork",
            "about": {
                "@id": "./"
            },
            "conformsTo": {
                "@id": "https://w3id.org/ro/crate/1.1"
            }
        },
        {
            "@id": "https://w3id.org/ro/wfrun/process/0.5",
            "@type": "CreativeWork",
            "name": "Process Run Crate",
            "version": "0.5"
        },
        {
            "@id": "example-cli@1.2.3",
            "@type": "SoftwareApplication",
            "description": "Example CLI",
            "name": "example-cli",
            "version": "1.2.3"
        },
        {
            "@id": "input.txt",
            "@type": "File",
            "contentSize": 5,
            "description": "Input file",
            "encodingFormat": "text/plain",
            "name": "input.txt"
        },
        {
            "@id": "output.txt",
            "@type": "File",
            "contentSize": 5,
            "description": "Output file",
            "encodingFormat": "text/plain",
            "name": "output.txt"
        },
        {
            "@id": "someuser",
            "@type": "Person",
            "name": "someuser"
        },
        {
            "@id": "example-cli input.txt output.txt",
            "@type": "CreateAction",
            "agent": {
                "@id": "someuser"
            },
            "endTime": "2026-01-28T15:07:18.600135",
            "instrument": {
                "@id": "example-cli@1.2.3"
            },
            "name": "example-cli input.txt output.txt",
            "object": [
                {
                    "@id": "input.txt"
                }
            ],
            "result": [
                {
                    "@id": "output.txt"
                }
            ],
            "startTime": "2026-01-28T15:07:18.599714"
        }
    ]
}
You can also call the argument parser agnostic version of the recorder directly. (Click me to see code)
from datetime import datetime, UTC
from pathlib import Path

from rocrate_action_recorder import (
    IOArgumentPath,
    IOArgumentPaths,
    Program,
    record,
)

crate_dir = Path()
input_path = crate_dir / "input.txt"
output_path = crate_dir / "output.txt"
input_path.write_text("Hello World\n")
argv = [
    "myscript",
    "--input",
    str(input_path),
    "--output",
    str(output_path),
]
start_time = datetime(2026, 1, 16, 12, 0, 0, tzinfo=UTC)
# Simulate the script's main operation
output_path.write_text(input_path.read_text().upper())
end_time = datetime(2026, 1, 16, 12, 0, 5, tzinfo=UTC)

crate_meta = record(
    program=Program(
        name="myscript", description="My test script", version="1.2.3"
    ),
    ioargs=IOArgumentPaths(
        input_files=[
            IOArgumentPath(name="input", path=input_path, help="Input file")
        ],
        output_files=[
            IOArgumentPath(name="output", path=output_path, help="Output file")
        ],
    ),
    argv=argv,
    current_user="tester",
    start_time=start_time,
    end_time=end_time,
    crate_dir=crate_dir,
    dataset_license="CC-BY-4.0",
)
# crate_meta == Path("ro-crate-metadata.json")

Example

See the example folder for a minimal example.

Contributions

See AGENTS.md for commands and hints for contributions.

Citation

See CITATION.cff for citation information.

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

rocrate_action_recorder-0.1.0.tar.gz (63.3 kB view details)

Uploaded Source

Built Distribution

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

rocrate_action_recorder-0.1.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file rocrate_action_recorder-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for rocrate_action_recorder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 685767f4d2ae1662b3e9a8378a82c02e62d9fda5256f2d6fc760764d51776cac
MD5 b798d6644897cbd451990d77aece0fe2
BLAKE2b-256 b4d7e34230888efee32ee3df4cfd483c8d72577160e43a3d7216fe0f7772efc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for rocrate_action_recorder-0.1.0.tar.gz:

Publisher: publish.yml on i-VRESSE/rocrate-action-recorder

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

File details

Details for the file rocrate_action_recorder-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rocrate_action_recorder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c12f766bbeb802aed5b5085ac13ec0fca039fbe3739f7f95f598ba310547e51
MD5 0448711b5f160a9584697af0e7f06b9c
BLAKE2b-256 ed60d1a8015d75d37e19a29d837d41964cca6bf58a3e588427cefdbac7f4bf7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rocrate_action_recorder-0.1.0-py3-none-any.whl:

Publisher: publish.yml on i-VRESSE/rocrate-action-recorder

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