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