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.3.tar.gz (29.2 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.3-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for shell_lib-2.0.3.tar.gz
Algorithm Hash digest
SHA256 245a4f1c76c2d0c523a2f005e681e75d284831f1c95209dd0132e6fd9d83d080
MD5 1b90e4b4534530131c62616831ccc233
BLAKE2b-256 c05ab64a1da10b65b895cf2622b2763bb8188cfb60fcd5d525f53abee415a38a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for shell_lib-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6843d1b97cb4dec87c5385536f437dddbbb7da396c0d0e8733074b0fc8076bb1
MD5 000dedd3ffc0d1196c74bfc399b1a54d
BLAKE2b-256 b48672bf9ab84b1bc1599bdd40a4905b71d3c35af32dc9f1b47b94f3ad9b59cb

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