Skip to main content

CommandLib

CommandLib is a pythonic wrapper around subprocess that lets you pass around command objects and daisy-chain:

  • Arguments

  • Paths

  • Other environment variables

  • Runtime directory

  • Other runtime properties (run in shell, conceal stdout/stderr, ignore error codes, etc.)

It is somewhat inspired by amoffat’s sh, Kenneth Reitz’s requests, jaraco’s path.py and SQLAlchemy.

To install:

$ pip install commandlib

Example:

>>> from commandlib import Command
>>> ls = Command("ls")
>>> ls("-t").in_dir("/").with_shell().run()
sys  tmp  run  dev  proc  etc  boot  sbin  root  vmlinuz  initrd.img  bin  lib  opt  vmlinuz.old  initrd.img.old  media  home  cdrom  lost+found  var  srv  usr  mnt

CommandPath example:

>>> from commandlib import CommandPath
>>> bin = CommandPath("/bin")
>>> bin.ls("-t").in_dir("/").run()
sys  tmp  run  dev  proc  etc  boot  sbin  root  vmlinuz  initrd.img  bin  lib  opt  vmlinuz.old  initrd.img.old  media  home  cdrom  lost+found  var  srv  usr  mnt

API

>>> from commandlib import Command, run

# Create command object
>>> py = Command("/usr/bin/python")

# Run with *additional* environment variable PYTHONPATH (*added* to global environment when command is run)
>>> py = py.with_env(PYTHONPATH="/home/user/pythondirectory")

# Run with additional path (appended to existing PATH environment variable when command is run)
>>> py = py.with_path("/home/user/bin")

# Run in specified directory (default is current directory)
>>> py = py.in_dir("/home/user/mydir")

# Run in shell
>>> py = py.with_shell()

# Suppress stderr
>>> py = py.silently() # Suppress stdout and stderr

# Finally run command explicitly with all of the above
>>> run(py)
>>> py.run() # alternative syntax

Why?

Commandlib is a library to make it easier to pass around immutable (sort of) command objects between different modules and classes and incrementally modify the command’s behavior in a readable way - adding environment variables, paths, etc.

  • call, check_call and Popen do not have the friendliest of APIs and code that uses them a lot can get ugly fast.

  • sh does a similar thing but has a lot of magic (e.g. overriding import).

  • envoy and sarge are more focused on chaining commands rather than arguments, environment variables, etc.

Advanced API

Add trailing arguments:

>>> from commandlib import Command, run
>>> manage = Command("/usr/bin/python", "manage.py").with_trailing_arguments("--settings", "local_settings.py").in_dir("projectdir")
>>> run(manage("runserver"))
[ Runs "/usr/bin/python manage.py runserver --settings local_settings.py" inside projectdir ]

Dynamically generate command bundles from directories with executables in them:

>>> from commandlib import CommandPath, Command, run
>>> postgres94 = CommandPath("/usr/lib/postgresql/9.4/bin/")
>>> run(postgres94.postgres)
[ Runs postgres ]

>>> run(postgres94.createdb)
[ Runs createdb ]

Use with path.py (or any other library where str(object) resolves to a string:

>>> from path import Path
>>> postgres94 = CommandPath(Path("/usr/lib/postgresql/9.4/bin/"))
>>> run(postgres94.postgres)

Metadata

Release files for commandlib 0.3.5

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

Source distribution (sdist)

Source distribution for commandlib 0.3.5
File Size Uploaded Provenance Actions
commandlib-0.3.5.tar.gz 6.9 kB Without provenance Details for commandlib-0.3.5.tar.gz

Release files / commandlib-0.3.5.tar.gz

Download URL commandlib-0.3.5.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
07b4d2a64a6858e36b185e3512d24ea2f34267d035450035d9bd8437c0ad7d38
BLAKE2b-256 checksum
How to use checksums
dea6900f45a700bc66b00dc1deaa2ca2dbad5c1b634c48012f649b3ae088b514
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Security for commandlib 0.3.5

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