Skip to main content

runj

I just want to run a shell command from Python!

Version MIT License Publish

░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░       ░▒▓█▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░      ░▒▓█▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░      ░▒▓█▓▒░
░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░      ░▒▓█▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░

Abstract

The accepted method for running underlying system or shell commands via python is using the subprocess module and specifically the Popen() function. While this method exposes considerable flexibility, it can be complex, requiring some tedious boilerplate for capturing stdout and stderr, return values, etc. Moreover, depending on context (foreground vs background for example), some of the Popen() semantics and supporting boilerplate differ.

If the need is simply to have a straightforward pythonic way to just call a CLI string on the underlying system that uses Popen(), can handle real time stdout echoing, can easily background tasks, does stderr capturing, etc, runj can help.

runj offers a same-named script, and a RunJ() class. Using the RunJ() class provides a much simpler and cleaner API compared to Popen() and boilerplate. Additionally, the runj script can run CLI commands and return a JSON formatted result by default.

Installation

Using PyPI

The best method of installing runj and all of its dependencies is by fetching it from PyPI

pip3 install python-runj

Usage

A good set of exemplars is provided in the test_core.py test code.

Python API

In the simplest case where some CLI string, say ls /tmp | wc -l needs to be evaluated:

from runj.runj  import RunJ, json_representation
from runj.models.data import ShellRet

shell:RunJ      = RunJ()
ret:ShellRet    = shell('ls /tmp | wc -l')
print(json_representation(ret))

script

The equivalent from the runj script would be

runj --exec 'ls /tmp | wc -l'

While it might be debatable as to the utility of running a CLI string from the CLI via some intermediary like runj, one use case is simply that it captures results in a useful JSON return, so would be suited as a possible interface to communications over web, for example.

CLI Synopsis

        --exec <CLIcmdToExec>
        The command line expression to exeute.

        [--spawnScript]
        If specified, create a script around the <CLIcmdToExec> and execute
        the script. This is particularly useful for jobs that need to be run
        in the background.

        [--scriptDir <dir>]
        If specified, write any spawnedScripts to <dir>. If not specified, will
        autogenerate the <dir> given current date, typically in
        `/tmp/runj-history`

        [--background]
        If specified, and in conjunction with --spawnScript, will open the
        subprocess and not wait/block on stdout/stderr.

        IMPORTANT: Even if the <CLIcmdToExec> ends with a "background" '&'
        character, this script will still block until the child has completed.
        To detach and not wait, for the child, you MUST specify this flag.

        In fact, the "&" is not needed in the <CLIcmdToExec>.

        [--log <dir>]
        If specified, create in <dir> a log snapshot of various env/exec
        values (uid, pid, etc).

        [--logPrefix <prefix>]
        If specified, prepend log snapshots with <prefix>.

        [--version]
        If specified, print the version and exit.

        [--man]
        If specified, print a detail man page and exit.

        [--synopsis]
        If specified, print only an overview synposis and exit.

Examples

Watch this space!

-30-

Metadata

Release files for python-runj 1.0.2

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

Source distribution (sdist)

Source distribution for python-runj 1.0.2
File Size Uploaded Provenance
python_runj-1.0.2.tar.gz 9.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for python-runj 1.0.2
File Interpreter ABI Platform
python_runj-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / python_runj-1.0.2.tar.gz

Download URL python_runj-1.0.2.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
cfa0c91031664145b2f060d0efc36a4e1e845f663a04527af54325c99bf810c3
BLAKE2b-256 checksum
How to use checksums
68395e8d3d1c9c200f4b7fdd1f858a73f49d1dda4c9e2304506933c14ca31c0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / python_runj-1.0.2-py3-none-any.whl

Download URL python_runj-1.0.2-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c895e2ef2284a3998587b60a4a5d25694af7cc58fcedd87ef80a9a8d010f065d
BLAKE2b-256 checksum
How to use checksums
c27f54d50f979540a6be9e9b281eb1e462d3f8747db40e03f8d89787a5e838bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.8

Security for python-runj 1.0.2

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