xref: /aosp_15_r20/external/pytorch/tools/bazel_tools/shellwrap.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#!/bin/bash
2
3# This script is helpful in entering an interactive shell from a bazel build
4# before running a given bazel executable.
5# This can provide a quick way to explore the sandbox directory and filesystem.
6# Typical use is with
7#
8#     bazel run --run_under=//tools/bazel:shell_wrapper //:target
9#     OR
10#     bazel run --config=shell //:target
11
12shell='/bin/bash'
13rcfile='/tmp/pytorch_bazel_tools_shellwrap'
14while [[ $# -gt 0 ]] ; do
15    case "$1" in
16        --shell_bin_path)
17            # path for the shell executable
18            shell="$2"
19            shift 2
20            ;;
21        --rcfile)
22            # path for the file used to write the environment
23            rcfile="$2"
24            shift 2
25            ;;
26        *)
27            # remaining arguments are part of the command for execution
28            break
29            ;;
30    esac
31done
32
33if ! tty -s; then
34    echo 'A tty is not available.'
35    echo "Use \`bazel run\`, not \`bazel test\`."
36    exit 1
37fi
38
39NOCOLOR='\033[0m'
40YELLOW='\033[1;33m'
41
42# store the environment in a file
43export PYTORCH_SHELL_COMMAND=$*
44echo "alias run=\"$*\"" > "$rcfile"
45echo "PS1='\s-\v\$ '" >> "$rcfile"
46
47echo =====
48# print the execution command (command is yellow)
49echo -e "alias run=${YELLOW}$PYTORCH_SHELL_COMMAND${NOCOLOR}"
50echo =====
51
52echo "Entering interactive shell at the execution root:"
53
54# quote escape all the arguments to use as a single input string
55cmd="'$shell' --noprofile --rcfile '$rcfile'"
56
57# run the command in a script psuedo terminal and dump to null
58/usr/bin/script -c "$cmd" -q /dev/null
59