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.1.tar.gz (29.0 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.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: shell_lib-2.0.1.tar.gz
  • Upload date:
  • Size: 29.0 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.1.tar.gz
Algorithm Hash digest
SHA256 129748a08579ecee4fa64b513484301910c97185c5b6f205a901b3f738f73651
MD5 dcd46e56beda2d3e4c8f1ef332690474
BLAKE2b-256 40734eeb27481a671b5ad10ccc1fd9b02d5c78b80ef76d8be0e3f3fb8ff4ab84

See more details on using hashes here.

File details

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

File metadata

  • Download URL: shell_lib-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 70cab3742c582a1b20b6d998fb4bfc9238190566269191ed1163a5630f27fab4
MD5 89bdfe27e817fc9f02e6a68d48dec9be
BLAKE2b-256 4e0396cc83341c32a5c93c9bda0221ded8b238f0bf3cb0af62f7550e8fd57bb8

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