Skip to main content

A Python utility to simplify the writing of shell-like scripts.

Project description

Introduction

Documentation | Changelog | Repository

shell-lib is designed to simplify the writing of shell-like scripts:

  • Python syntax: Write scripts in readable Python, freeing from complex shell command syntax.
  • Reliable error handling: Use Python's exception to manage command failure. If a command fails, by default, it raises a subprocess.CalledProcessError exception. For commands that may fail, user can also only check the exit-code.
  • Cross-platform compatibility: Write a single script that works across Linux, macOS, and Windows platforms.
  • Rich ecosystem integration: Easily integrate with both the CLI tool and Python library ecosystems.
  • Lightweight and portable: Only use Python standard library.
  • Well tested: Consistent and reliable behavior on different platforms and Python versions.
  • Friendly: Readers can understand the code without consulting the documentation. If writers use modern IDEs, then almost no need to consult the documentation.

It provides some functions that make writing shell-like scripts easy:

  1. Executing external command: Conveniently invoke external commands.
  2. File and directory operations: Provide a consistent and intuitive file system operations API, that clearly distinguish between file and directory operations.
  3. User interactions: Do common interactions with one line of code.
  4. Obtaining system information: Get common system information for shell-like scripts.

This module was co-created with Google Gemini.

Usage

#!/usr/bin/python3
from shell_lib import sh

PROJECT_PATH = "my_project"
FILE = "hello.txt"

sh.create_dir(PROJECT_PATH)
# sh.cd() context manager restores the previous working directory when
# exiting the code block, even if an exception raised within the code block.
with sh.cd(PROJECT_PATH):
    sh(f"echo 'Hello, World!' > {FILE}")
    print(f"File size: {sh.get_file_size(FILE)} bytes")
sh.remove_dir(PROJECT_PATH)

There is a demo script at the documentation page.

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

shell_lib-2.0.0.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

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

shell_lib-2.0.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file shell_lib-2.0.0.tar.gz.

File metadata

  • Download URL: shell_lib-2.0.0.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.8

File hashes

Hashes for shell_lib-2.0.0.tar.gz
Algorithm Hash digest
SHA256 51505d172f515636d0f48dd0c341f4fc24837a5fb070fc1fc089ddcd5bb70bd6
MD5 ba83fcb2cd5bc70799160f6b7e36e9cc
BLAKE2b-256 1a611b191b5c51b79713d5d3cc8a151f2a5cff86bfd9d7bf4d9827fe3f26f80c

See more details on using hashes here.

File details

Details for the file shell_lib-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: shell_lib-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.8

File hashes

Hashes for shell_lib-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 59218f2ceea2c66df8a5ea75734998309a78e1e9e844f2f19e4dd934f2ef53e8
MD5 338a436b82a027d9f135e691d9f2356e
BLAKE2b-256 e5b0426fc51aaa70917c1ee7541b5bbe7e6471ba0064eb30e8668092250a06ff

See more details on using hashes here.

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