Skip to main content

Utilities for task management by creation and execution OS-specific scripts.

Project description

CommandScript

License Python Version

CommandScript is a Python library that provides utilities for task management through the creation and execution of OS-specific scripts. It simplifies cross-platform command execution by generating shell scripts (.sh for Unix-like systems, .bat for Windows) and handling logging, timing, and error checking automatically.

Based on the invoke library, it allows you to create collections of tasks running along the following pipeline:

Features

  • Cross-platform script execution: Automatically generates and executes OS-specific scripts (Bash for Linux/macOS, Batch for Windows)
    • ⚠️ MacOS (NOT TESTED)
    • ✅ Linux (POSIX)
      • Generates .sh Bash scripts
      • Uses bash for execution
      • Measures execution time with nanosecond precision
    • ✅ Windows (NT)
      • Generates .bat Batch scripts
      • Uses cmd.exe for execution
      • Handles Windows path separators and quoting
      • Supports delayed variable expansion
  • Task management: Built-in support for Invoke tasks with decorators
    • each task you launch:
      • will collect its log twice: in console and special file (defined for the task unique)
      • if managed via ScriptExecutor will has two files for yourself:
        1. script file: path/to/.generated/your-script-task.(sh|bat)
        2. script logs: path/to/.generated/your-script-task.log
    • when task is finished:
      • its script is saved and could be launch via terminal directly
      • its execution log is saved and could be open in IDE for analytics
  • Environment context: Global environment variable management for script execution
  • Integrated logging: Colored console output and file logging with timestamps
  • Error handling: Automatic return code checking and error logging on failures
  • Timing: Execution duration measurement for performance monitoring

Installation

Install CommandScript using pip:

pip install commandcript

Dependencies

CommandScript requires the following dependencies (automatically installed):

  • invoke>=2.2.0 - Task execution framework
  • colorama>=0.4.6 - Cross-platform colored terminal text

Quick Start

✅ Good practice example you can see the tasks.py in this repository.

Basic Usage

import os
import commandcript

# Set up environment context
commandcript.ENV_CONTEXT \
    .add_env_var('PROJECT_GIT_DIR', '/path/to/project') \
    .add_env_var('COMMANDSCRIPT_SCRIPT_DIR', '${PROJECT_GIT_DIR}/scripts')

# Create and execute a simple command
executor = commandcript.ScriptExecutor(
    log_dir=commandcript.ENV_CONTEXT.COMMANDSCRIPT_SCRIPT_DIR,
    execute_created_script=True
)

executor.add_cwd('/path/to/working/directory') \
        .add_command(['echo', 'Hello, World!']) \
        .execute(log='hello_world')

Using with Invoke Tasks

import invoke
import commandcript

commandcript.ENV_CONTEXT.add_env_var('COMMANDSCRIPT_SCRIPT_DIR', '/path/to/folder/with/generated/scripts/.generated')

# Define a task
@commandcript.script_task()
def build(ctx):
    """Build the project"""
    commandcript.ScriptExecutor(ctx.script_dir, ctx.launch) \
        .add_cwd(commandcript.ENV_CONTEXT.PROJECT_GIT_DIR.hld) \
        .add_command(['python', 'setup.py', 'build']) \
        .execute(log='build')

# Create namespace and run
namespace = invoke.Collection()
namespace.add_task(build)

Environment Setup

commandcript.ENV_CONTEXT is a global instance of the EnvContext class, which is a specialized dictionary for storing environment variables as EnvVariable objects used across @commandcript.script_task() instances.

The EnvContext class provides additional functionality for managing environment variables, including the add_env_var method for retrieving OS environment variables with defaults and automatic variable substitution.

Variable substitution: You can use ${VAR} syntax in default values. The value will be stored in two forms:

  • .hld — hold value with OS-specific substitution placeholders (e.g., $VAR on POSIX, %VAR% on Windows)
  • .exp — fully expanded value with all substitutions resolved

Before using CommandScript, set up the environment context:

from src.commandcript import ENV_CONTEXT

ENV_CONTEXT\
    .add_env_var('PROJECT_GIT_DIR', f'{__file__}/../')\
    .add_env_var('COMMANDSCRIPT_SCRIPT_DIR', '${PROJECT_GIT_DIR}/.generated')\
    .add_env_var('PROJECT_DIST_DIR', '${PROJECT_GIT_DIR}/dist')

# Access values
print(ENV_CONTEXT.PROJECT_DIST_DIR.hld)  # ${PROJECT_GIT_DIR}/dist  (or %PROJECT_GIT_DIR%/dist on Windows)
print(ENV_CONTEXT.PROJECT_DIST_DIR.exp)  # /path/to/project/dist

Multi-command execution

import commandcript

commandcript.ENV_CONTEXT.add_env_var('LOG_DIR', '/path/to/logs')

executor = commandcript.ScriptExecutor(commandcript.ENV_CONTEXT.LOG_DIR, True)

executor.add_cwd('/tmp') \
        .add_env({'MY_VAR': 'value'}) \
        .add_command(['export', 'MY_VAR=override']) \
        .add_command(['echo', '$MY_VAR']) \
        .add_command(['ls', '-la']) \
        .execute(log='multi_command')

Task with Parameters

@commandcript.script_task(
    help={
        'target': 'Build target (default: all)',
        'clean': 'Clean before build'
    }
)
def build(ctx, target='all', clean=False):
    executor = commandcript.ScriptExecutor(ctx.script_dir, ctx.launch)

    if clean:
        executor.add_command(['make', 'clean'])

    executor.add_command(['make', target]) \
            .execute(log=f'build_{target}')

API Reference

ScriptExecutor

The main class for executing commands via OS-specific scripts.

  • Constructor:
    ScriptExecutor(log_dir: EnvVariable, execute_created_script: bool)
    
    • log_dir: EnvVariable object pointing to the directory where log files and scripts will be stored (uses .exp internally)
    • execute_created_script: If True, executes the generated script; if False, only generates it
  • Methods
    • add_cwd(cwd: str): Set working directory for script execution
    • add_env(env: dict): Add environment variables
    • add_command(command: list, enter=True, offset=True): Add a single command
    • add_commands(commands: List[list], enter=True, offset=True): Add multiple commands
    • execute(log: str = None): Execute the script and log output. On failure, logs the error without raising an exception.

EnvVariable

Represents a single environment variable with support for cross-platform substitution syntax.

  • Properties
    • name: The variable name
    • hld: Hold value with OS-specific substitution placeholders ($VAR on POSIX, %VAR% on Windows)
    • exp: Fully expanded value with all ${VAR} substitutions resolved
    • __str__(): Returns the hold value (.hld)

EnvContext

Specialized dictionary (Dict[str, EnvVariable]) for managing environment variables.

  • Methods
    • add_env_var(env_var_name: str, default_value: str = None) -> EnvContext: Add an environment variable from OS environment with fallback to default value. Supports ${VAR} substitution in default values.

@script_task()

Decorator for defining Invoke tasks with CommandScript integration.

@commandcript.script_task(
    help={'param': 'Description'},
    iterable=['param']
)
def my_task(ctx, param=None):
    # Task implementation
    pass

Logging

CommandScript provides colored logging utilities:

  • INFO: General information (blue)
  • SUCCESS: Success messages (green)
  • STATUS: Status updates (cyan)
  • ERROR: Error messages (red)
import commandcript

commandcript.INFO.log_line("This is an info message")
commandcript.SUCCESS.log_line("Operation completed successfully")
commandcript.ERROR.log_line("An error occurred")

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

commandscript-1.2.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

commandscript-1.2.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file commandscript-1.2.0.tar.gz.

File metadata

  • Download URL: commandscript-1.2.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for commandscript-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d81ef3f20fc2821839c93baf5be7e589b8c796397fa578b6ab59795564f6844f
MD5 ae6ca14af894110bd31e56bc9c6e99a0
BLAKE2b-256 11480a590fbbce1599a673dba2da33f9f5dffee182508d2cd647418121de4a33

See more details on using hashes here.

File details

Details for the file commandscript-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: commandscript-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for commandscript-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b6f4c9cf55b1f01cb08508bf24ad2150adf79a55bb5c3c54a719e5575cb2faa
MD5 204dcd393632417126bbf7f881ec2ce1
BLAKE2b-256 59d714042c7a691fa74c498e908f7d02401192f2d66494d0de9e68f85ab93d6b

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