Skip to main content

Python utilities that wrap bash commands

Project description

Basher

Basher is a Python library that provides a convenient wrapper around common bash commands, making it easier to perform file system operations, package management, and archive handling in Python scripts. Basher also can be used to build Doker images and used in the Dokerfile.

Why Python for Bash

Python is a great choice for tasks that you might otherwise do in a Bash script for several reasons:

Readability and Maintainability

Python’s syntax is often more readable and less prone to subtle errors compared to complex Bash one-liners. It’s easier for teams (especially those less familiar with shell scripting) to understand and maintain Python scripts.

Large Standard Library

Python has built-in support for common tasks like working with files, directories, CSV or JSON data, HTTP requests, etc. In Bash, you often need external utilities (like awk, sed, curl, etc.) or rely on multiple commands piped together.

Portability

Python is available on most modern systems, including Linux, macOS, and Windows. Complex Bash scripts can run into portability issues when you use commands or shell features that differ across Unix-like systems.

Error Handling and Debugging

Python’s exception handling is more robust and easier to manage compared to handling exit codes and conditional branching in Bash. Python’s built-in debugger (pdb) can step through code, making it simpler to diagnose and fix problems.

Scaling Complexity

As your script grows, Python can handle more advanced logic, data structures, and third-party libraries with ease. Bash scripts become harder to read and maintain once they get beyond trivial tasks.

Better for String Manipulation and Data Parsing

Python excels at parsing logs, JSON, XML, CSV, and other data formats with minimal hassle. In Bash, such tasks quickly get messy, often requiring multiple external tools.

Easier Interfacing with Other Systems

Whether you need to interact with databases, perform network calls, or handle complex file I/O, Python provides a straightforward approach. While Bash can call these tools, chaining them together can be more cumbersome.

Features

  • File Operations: Read, write, copy, move, and find files
  • System Operations: Install packages, create directories, change permissions
  • Archive Operations: Create and extract archives, compress and decompress files
  • Command Execution: Run bash commands with proper error handling
  • Colorful Output: Display messages in different colors for better visibility
  • Interactive Mode: Interactive mode for easy configuration of the script
  • No Interaction Mode: No interaction mode for easy installation of the script
  • Detect Package Manager: Detect the system's package manager

Compilation to bash script

By design, Basher is a Python library; however, it can be compiled into a Bash script, with each command acting as a straightforward Python wrapper in pure Bash.

Methodology

One Core Basher method is to use bash.cmd to execute commands. This method is a wrapper around the bash command and is the most straightforward way to execute commands.

In our implementation we wraping one logical server configuration into one method using basher core commands.

Forexample: Install git and configure it

def install_git():
    bash.echo("Installing git")
    bash.install("git")
    bash.cmd("git config --global user.name 'John Doe'")
    bash.cmd("git config --global user.email 'john.doe@example.com'")

Install nginx

def install_nginx():
    bash.echo("Installing nginx")
    bash.install("nginx")

Install php

def install_php():
    bash.echo("Installing php")
    bash.install("php")

Install mysql

def install_mysql():
    bash.echo("Installing mysql")
    bash.install("mysql")

Install redis

def install_redis():
    bash.echo("Installing redis")
    bash.install("redis")

Install postgresql

def install_postgresql():
    bash.echo("Installing postgresql")
    bash.install("postgresql")

Install elasticsearch

def install_elasticsearch():
    bash.echo("Installing elasticsearch")
    bash.install("elasticsearch")

Install node.js

def install_nodejs():
    bash.echo("Installing node.js")
    bash.install("nodejs")
    bash.install("npm")
    bash.cmd("npm install -g yarn")

Install docker

def install_docker():
    bash.echo("Installing docker")
    bash.install("docker")
    bash.cmd("docker --version")
    bash.echo("Installing docker compose")
    bash.install("docker-compose")

Install elasticsearch

def install_elasticsearch():
    bash.echo("Installing elasticsearch")
    bash.install("elasticsearch")

Install composer for php

def install_composer():
    bash.cmd("php -r \"copy('https://getcomposer.org/installer', 'composer-setup.php');\"")
    bash.cmd("php composer-setup.php")
    bash.cmd("php -r \"unlink('composer-setup.php');\"")
    bash.cmd("mv composer.phar /usr/bin/composer")
    bash.cmd("composer --version")
    bash.echo("Composer installed")

Complex Installation Example

Here's an example of a complex installation using Basher, divided into logical methods:

def setup_web_server():
    bash.echo("Setting up web server environment")
    install_nginx()
    install_php()
    install_mysql()
    configure_nginx_for_php()
    bash.echo("Web server environment setup complete")

def install_nginx():
    bash.echo("Installing Nginx")
    bash.install("nginx")
    bash.cmd("systemctl start nginx")
    bash.cmd("systemctl enable nginx")

def install_php():
    bash.echo("Installing PHP")
    bash.install("php-fpm")
    bash.install("php-mysql")

def install_mysql():
    bash.echo("Installing MySQL")
    bash.install("mysql-server")
    bash.cmd("systemctl start mysql")
    bash.cmd("systemctl enable mysql")

def configure_nginx_for_php():
    bash.echo("Configuring Nginx to use PHP")
    bash.write_to_file("/etc/nginx/sites-available/default", """
    server {
        listen 80;
        server_name example.com;
        root /var/www/html;

        index index.php index.html index.htm;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\.ht {
            deny all;
        }
    }
    """)
    bash.cmd("systemctl restart nginx")

Usage

Basic Usage

from basher import Basher

# Create a Basher instance
bash = Basher()

# Execute a shell command
output = bash.cmd("ls -la")

# Write to a file
bash.write_to_file("/path/to/file.txt", "Hello, world!")

# Read from a file
content = bash.read_file("/path/to/file.txt")

# Check if a file exists
if bash.exists("/path/to/file.txt"):
    print("File exists!")

File Operations

# Copy a file
bash.copy("/source/file.txt", "/destination/file.txt")

# Move a file
bash.mv("/source/file.txt", "/destination/file.txt")

# Find files matching a pattern
files = bash.find("/search/directory", "*.txt")

# Check if a string exists in a file
if bash.string_in_file("/path/to/file.txt", "search string"):
    print("String found!")

# Replace content in a file
bash.replace_in_file("/path/to/file.txt", "old_pattern", "new_content")

# Change file permissions
bash.chmod("/path/to/file.txt", "755")

# Change file ownership
bash.chown("/path/to/file.txt", "user", "group")

System Operations

# Detect the system's package manager
package_manager = bash.detect_package_manager()
print(f"Using package manager: {package_manager}")

# Install packages
bash.install(["git", "curl", "wget"])

# Create a directory
bash.mkdir("/path/to/directory")

# Change directory
bash.cd("/path/to/directory")

# Remove a file or directory
bash.rm("/path/to/file.txt")
bash.rm("/path/to/directory", recursive=True)

# Ensure sudo is available
if bash.ensure_sudo():
    print("Sudo is available")

Archive Operations

# Create an archive
bash.archive("/source/directory", "/path/to/archive.tar.gz", format="tar.gz")

# Extract an archive
bash.extract("/path/to/archive.tar.gz", "/destination/directory")

# Compress a file with gzip
bash.gzip("/path/to/file.txt", keep_original=True)

# Decompress a gzipped file
bash.gunzip("/path/to/file.txt.gz", keep_original=False)

# Download a file
bash.download("https://example.com/file.txt", "/path/to/save/file.txt")

Colorful Output

# Display messages in different colors
bash.error("This is an error message")  # Red
bash.warning("This is a warning message")  # Yellow
bash.success("This is a success message")  # Green
bash.info("This is an info message")  # Blue

# Use the echo method with colors
bash.echo("This is a colored message", color="cyan")

Command Execution in a Directory

# Execute a command in a specific directory
output = bash.execute_in_directory("ls -la", "/path/to/directory")

Error Handling

Most methods return True if successful and False if they fail, making it easy to check for errors:

if not bash.mkdir("/path/to/directory"):
    print("Failed to create directory")

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

basher2-0.1.0.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

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

basher2-0.1.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file basher2-0.1.0.tar.gz.

File metadata

  • Download URL: basher2-0.1.0.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for basher2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 83f7e6d5b8c7d84e4066d1b57531c9ff1061c561eb1c910118a6ae880bbd7305
MD5 d6fbf24b9a8a55e2e20a2fa60a81977c
BLAKE2b-256 b55cb7432ce99c4546584d99350e6331d54a8963ef4a2533772f7083a67a7b76

See more details on using hashes here.

File details

Details for the file basher2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: basher2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for basher2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fca0fb55dac9218ad2534e2e02569b8ff317b8360c52b8a65c4f24c8027578fa
MD5 1fec9301c9b5a4bd9f56cbb17246f0a9
BLAKE2b-256 b1a18c3f6a2d86afcc864efef72e7c4534bbc557f32b4ebbe71429ce85433f19

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