West (Zephyr RTOS Meta-Tool)

1.5.0 · active · verified Sat Apr 11

West is the Zephyr RTOS project's meta-tool, designed to manage multiple Git repositories under a single directory using a manifest file. It provides commands for initializing workspaces, updating repositories, building, flashing, and debugging Zephyr applications. While primarily a command-line interface tool, `west` also exposes Python APIs for advanced programmatic interaction and extending its functionality. It maintains an active development cycle with frequent releases, often including alpha versions before stable major/minor updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `west` from a Python script to initialize and update a Zephyr RTOS workspace. It uses `subprocess` to execute `west` CLI commands, which is the most common way to automate `west` operations in Python. Ensure `git` is installed and available in your system's PATH before running.

import subprocess
import os
import shutil

# Ensure a clean state for the example
if os.path.exists('zephyrproject'):
    shutil.rmtree('zephyrproject')

try:
    # Initialize a west workspace with the default Zephyr manifest
    print("Initializing west workspace...")
    subprocess.run(['west', 'init', 'zephyrproject'], check=True, capture_output=True)
    print("\nWest workspace initialized in 'zephyrproject'.")

    # Change into the workspace directory
    os.chdir('zephyrproject')

    # Update the repositories defined in the manifest
    print("\nUpdating repositories (this may take some time)...")
    subprocess.run(['west', 'update'], check=True, capture_output=True)
    print("\nRepositories updated successfully.")

    # List the projects in the workspace
    print("\nListing projects in the workspace:")
    result = subprocess.run(['west', 'list'], check=True, capture_output=True, text=True)
    print(result.stdout)

except subprocess.CalledProcessError as e:
    print(f"An error occurred during west command execution: {e}")
    print(f"Stdout: {e.stdout.decode()}")
    print(f"Stderr: {e.stderr.decode()}")
except FileNotFoundError:
    print("Error: 'west' command not found. Please ensure west is installed and in your PATH.")
finally:
    # Clean up the created directory for re-runnability
    os.chdir('..') # Go back up from zephyrproject
    if os.path.exists('zephyrproject'):
        shutil.rmtree('zephyrproject')
        print("\nCleaned up 'zephyrproject' directory.")

view raw JSON →