Skip to main content

CI Status PyPI package version PyPI python versions PyPI downloads

testgres.os_ops

testgres.os_ops is a lightweight Python package that provides a unified, highly secure, and polymorphic interface (OsOperations) for interacting with the operating system environment either locally or on a remote machine.

While originally designed as an infrastructure core for the testgres PostgreSQL testing framework, this package is completely decoupled and can be installed and used independently in any automation pipeline.


Key Features

  • Unified API (OsOperations): Write your automation logic once using a high-level abstract interface and switch transparently between local execution and remote SSH sessions.
  • Stateless and Stateful Environments: Smooth handling of environment variables across distinct execution contexts (set_env, environ, and polymorphic reset_env with clean state rollback).
  • Industrial-Grade Shell Security: Safe execution pipeline featuring bulletproof shell argument escaping (join_command_arguments) and tilde tracking (~, ~root), fully eliminating code injection vulnerabilities.
  • Cross-Platform Consistency: Extensively verified via a robust multi-container matrix across 12+ Linux distributions (including Ubuntu, Rocky Linux, Alt Linux, Astra Linux, and Alpine).

Installation

pip install testgres.os-ops

Architecture

  • LocalOperations: Interacts with the host machine using native Python standard libraries (subprocess, psutil, os, pathlib).
  • RemoteOperations: Controls remote Linux-based nodes by proxying commands over secure SSH connections (utilizing binary stream polling and automated environment replication).

Supported operation groups include:

  • Safe shell command execution
  • Persistent environment variable tracking
  • File and directory tree management (stat, exclusive creation, copying)
  • Process lifecycle and signal management

Quick Start Example

The following example demonstrates how the exact same generic automation code executes seamlessly on both a local host and a remote RHEL server.

import uuid
from testgres.operations.os_ops import OsOperations
from testgres.operations.os_ops import ConnectionParams
from testgres.operations.local_ops import LocalOperations
from testgres.operations.remote_ops import RemoteOperations


def generic_pipeline(title: str, os_ops: OsOperations):
    """Polymorphic code that executes identically on any target platform."""
    cmd = [
        "sh",
        "-c",
        "echo whoami: $(whoami); "
        "echo \"------ os_info:\"; cat /etc/os-release; "
        "echo \"------ our env:\"; echo \"OS_OPS_ENV\": ${OS_OPS_ENV}; "
    ]
    print("[{}] -------------\n".format(title))

    # Set stateful environment variable
    os_ops.set_env("OS_OPS_ENV", "HELLO WORLD!")

    # Safe execution with precise encoding
    cout = os_ops.exec_command(cmd, encoding="utf-8")
    print(cout)

    # Polymorphic state cleanup
    os_ops.reset_env("OS_OPS_ENV", None)
    return


# 1. Run locally using Python API under the hood
local_ops = LocalOperations()
generic_pipeline("Local system", local_ops)

# 2. Run remotely over an isolated SSH session
remote_cn_params = ConnectionParams(
    host="192.168.122.85",
    port=22,
    username="test",
    ssh_key="./id_rsa_test",
)
remote_ops = RemoteOperations(remote_cn_params)
generic_pipeline("Remote system", remote_ops)

Expected Output

[Local system] -------------

whoami: dima
------ os_info:
PRETTY_NAME="Ubuntu 24.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.4 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://ubuntu.com"
SUPPORT_URL="https://ubuntu.com"
BUG_REPORT_URL="https://launchpad.net"
PRIVACY_POLICY_URL="https://ubuntu.comlegal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
------ our env:
OS_OPS_ENV: HELLO WORLD!

[Remote system] -------------

whoami: test
------ os_info:
NAME="Red Hat Enterprise Linux"
VERSION="8.9 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.9"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.9 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://redhat.com"
DOCUMENTATION_URL="https://redhat.com"
BUG_REPORT_URL="https://redhat.com"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.9
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.9"
------ our env:
OS_OPS_ENV: HELLO WORLD!

Testing

The project includes an intensive full-scale test suite ensuring absolute behavior consistency between local and remote providers. The validation matrix automatically deploys multi-container Docker environments to stress-test concurrent setups, edge-case quoting anomalies, and thread isolation across distinct architectures.

Authors

Postgres Professional

Metadata

Release files for testgres.os-ops 3.3.1

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

Source distribution (sdist)

Source distribution for testgres.os-ops 3.3.1
File Size Uploaded
testgres_os_ops-3.3.1.tar.gz 60.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for testgres.os-ops 3.3.1
File Interpreter ABI Platform
testgres_os_ops-3.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 93.8 kB

Release files / testgres_os_ops-3.3.1.tar.gz

Download URL testgres_os_ops-3.3.1.tar.gz
Size 60.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1fe7ecaedb6681e6f13345ce87c49a849d2e75207468bfb13ca4f8898c7c8225
BLAKE2b-256 checksum
How to use checksums
f74ddd6816e73391da7573a6db2e8e00156d873c3521376111f76f556c13b920
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.

Transparency log

Release files / testgres_os_ops-3.3.1-py3-none-any.whl

Download URL testgres_os_ops-3.3.1-py3-none-any.whl
Size 33.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5cc8d7682110a98870333e28852976519c4d86be1a908014cd23dade03db78d
BLAKE2b-256 checksum
How to use checksums
fcb2e589e03c9185b1529736a6119e08137c2cc09f13a567592946ff2b954391
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.

Transparency log

Release history Release notifications | RSS feed

3.3.3

2 release files

3.3.2

2 release files

This release

3.3.1 This release

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.2

2 release files

0.0.1

1 release file

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