Shell Executable API Reference¶
The Shell class is the core component of hands-scaphoid, providing a secure context manager for shell command execution.
Context Manager
The Shell class implements the context manager protocol, ensuring proper cleanup of resources.
Platform Compatibility
Different shell types have varying levels of support across platforms. See the Windows Shells documentation for platform-specific details.
API documentation¶
            hands_scaphoid.objects.ShellExecutable
¶
    Shell execution module with security and environment management.
File
name: Shell.py uuid: 52319d84-4784-4df7-8752-64967f3716f8 date: 2025-09-12
Description
Provides secure shell command execution with allowlisting, environment management, and Docker integration capabilities.
Project
name: hands/palm/trapezium uuid: 2945ba3b-2d66-4dff-b898-672c386f03f4
Authors: ["Andreas Häberle"] Projects: ["hands/palm/trapezium"]
Classes¶
            ShellExecutable
¶
    
              Bases: ExecutableFile
A secure shell command executor with environment management.
This class provides a secure way to execute shell commands with features like: - Command allowlisting for security - Environment variable management - Docker container command execution - Working directory management
Source code in src/hands_scaphoid/objects/ShellExecutable.py
                37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406  |  | 
Functions¶
            __init__(cwd=None, env=None, env_file='~/.env')
¶
    Initialize the Shell instance.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                cwd
             | 
            
                  Optional[Union[str, PathLike]]
             | 
            
               Working directory for command execution. Defaults to current directory.  | 
            
                  None
             | 
          
                env
             | 
            
                  Optional[dict[str, str]]
             | 
            
               Environment variables dictionary. Defaults to copy of os.environ.  | 
            
                  None
             | 
          
                env_file
             | 
            
                  str
             | 
            
               Path to environment file to load variables from.  | 
            
                  '~/.env'
             | 
          
Raises:
| Type | Description | 
|---|---|
                  FileNotFoundError
             | 
            
               If the specified working directory doesn't exist.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            allow(command)
¶
    Allow a command to be executed.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                command
             | 
            
                  Union[str, List[str]]
             | 
            
               The command or lost of commands to allow (first word will be extracted).  | 
            required | 
Returns:
| Type | Description | 
|---|---|
                  bool
             | 
            
               True if command was successfully allowed, False if command doesn't exist.  | 
          
Raises:
| Type | Description | 
|---|---|
                  ValueError
             | 
            
               If command is empty or invalid.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            run(command_with_args, timeout=None, capture_output=True, text=True, check=True)
¶
    Execute a shell command with security checks.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                command_with_args
             | 
            
                  str
             | 
            
               The shell command to execute including arguments.  | 
            required | 
                timeout
             | 
            
                  Optional[int]
             | 
            
               Maximum seconds to wait for command completion.  | 
            
                  None
             | 
          
                capture_output
             | 
            
                  bool
             | 
            
               Whether to capture stdout and stderr.  | 
            
                  True
             | 
          
                text
             | 
            
                  bool
             | 
            
               Whether to return output as text (str) or bytes.  | 
            
                  True
             | 
          
                check
             | 
            
                  bool
             | 
            
               Whether to raise exception on non-zero exit codes.  | 
            
                  True
             | 
          
Returns:
| Type | Description | 
|---|---|
                  CompletedProcess
             | 
            
               CompletedProcess object with execution results.  | 
          
Raises:
| Type | Description | 
|---|---|
                  PermissionError
             | 
            
               If the command is not in the allow list.  | 
          
                  CalledProcessError
             | 
            
               If check=True and command fails.  | 
          
                  TimeoutExpired
             | 
            
               If command times out.  | 
          
                  ValueError
             | 
            
               If command is empty or invalid.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            run_in(container_name, command_with_args, timeout=None, capture_output=True, text=True, check=True)
¶
    Execute a command inside a Docker container.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                container_name
             | 
            
                  str
             | 
            
               Name of the Docker container.  | 
            required | 
                command_with_args
             | 
            
                  list[str] | str
             | 
            
               Command to execute inside the container.  | 
            required | 
                timeout
             | 
            
                  Optional[int]
             | 
            
               Maximum seconds to wait for command completion.  | 
            
                  None
             | 
          
                capture_output
             | 
            
                  bool
             | 
            
               Whether to capture stdout and stderr.  | 
            
                  True
             | 
          
                text
             | 
            
                  bool
             | 
            
               Whether to return output as text (str) or bytes.  | 
            
                  True
             | 
          
                check
             | 
            
                  bool
             | 
            
               Whether to raise exception on non-zero exit codes.  | 
            
                  True
             | 
          
Returns:
| Type | Description | 
|---|---|
                  CompletedProcess
             | 
            
               CompletedProcess object with execution results.  | 
          
Raises:
| Type | Description | 
|---|---|
                  PermissionError
             | 
            
               If docker command is not allowed.  | 
          
                  CalledProcessError
             | 
            
               If check=True and command fails.  | 
          
                  ValueError
             | 
            
               If container_name or command is empty.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            cd(path)
¶
    Change the current working directory.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                path
             | 
            
                  str
             | 
            
               Path to change to (relative or absolute).  | 
            required | 
Raises:
| Type | Description | 
|---|---|
                  NotADirectoryError
             | 
            
               If the path is not a valid directory.  | 
          
                  ValueError
             | 
            
               If path is empty.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            get_env_var(var_name)
¶
    Get the value of an environment variable.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                var_name
             | 
            
                  str
             | 
            
               Name of the environment variable.  | 
            required | 
Returns:
| Type | Description | 
|---|---|
                  Optional[str]
             | 
            
               Value of the environment variable or None if not found.  | 
          
Raises:
| Type | Description | 
|---|---|
                  ValueError
             | 
            
               If var_name is empty.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            set_env_var(var_name, value)
¶
    Set an environment variable.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                var_name
             | 
            
                  str
             | 
            
               Name of the environment variable.  | 
            required | 
                value
             | 
            
                  str
             | 
            
               Value to set.  | 
            required | 
Raises:
| Type | Description | 
|---|---|
                  ValueError
             | 
            
               If var_name is empty.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            sleep(seconds)
¶
    Sleep for the specified number of seconds.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                seconds
             | 
            
                  Union[int, float]
             | 
            
               Number of seconds to sleep.  | 
            required | 
Raises:
| Type | Description | 
|---|---|
                  ValueError
             | 
            
               If seconds is negative.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            depends_on(names)
¶
    Check if Docker containers are running.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                names
             | 
            
                  Union[str, List[str]]
             | 
            
               Container name(s) to check.  | 
            required | 
Returns:
| Type | Description | 
|---|---|
                  bool
             | 
            
               True if all containers are running.  | 
          
Raises:
| Type | Description | 
|---|---|
                  SystemExit
             | 
            
               If any container is not running.  | 
          
                  RuntimeError
             | 
            
               If docker command fails.  | 
          
Source code in src/hands_scaphoid/objects/ShellExecutable.py
              
            get_allowed_commands()
¶
    Get the list of currently allowed commands.
Returns:
| Type | Description | 
|---|---|
                  List[str]
             | 
            
               List of allowed command names.  | 
          
            is_command_allowed(command)
¶
    Check if a command is in the allow list.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                command
             | 
            
                  str
             | 
            
               Command name to check.  | 
            required | 
Returns:
| Type | Description | 
|---|---|
                  bool
             | 
            
               True if command is allowed, False otherwise.  |