blob: 21996662c2db6737216fc1a627ef114d1cca665e [file] [log] [blame]
mmoss@chromium.org375765c2015-10-30 15:39:24 +00001# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5## This file is designed to be sourced from a bash script whose name takes the
6## form 'command-name'. This script will then instead invoke
7## '[depot_tools]/command_name.py' correctly under mingw as well
8## as posix-ey systems, passing along all other command line flags.
9
10## Example:
11## echo ". python_runner.sh" > git-foo-command
12## ./git-foo-command #=> runs `python git_foo_command.py`
13
14## Constants
Gregory Nisbet6a5aa662020-01-22 02:03:33 +000015PYTHONDONTWRITEBYTECODE=1; export PYTHONDONTWRITEBYTECODE
mmoss@chromium.org375765c2015-10-30 15:39:24 +000016
17## "Input parameters".
18# If set before the script is sourced, then we'll use the pre-set values.
19#
20# SCRIPT defaults to the basename of $0, with dashes replaced with underscores
21
22# "$0" can have several different formats depending on how the script was called
23# and the environment being used, including having different formats even in the
24# same environment (e.g. in msys, 'git cl' causes $0 to have a Windows-style
25# path, but calling 'git-cl' results in a POSIX-style path), so don't assume a
26# particular format.
27# First try to split it using Windows format ...
28DEPOT_TOOLS="${0%\\*}"
29if [[ "$DEPOT_TOOLS" = "$0" ]]; then
30 # If that didn't work, try POSIX format ...
31 DEPOT_TOOLS="${0%/*}"
32 if [[ "$DEPOT_TOOLS" = "$0" ]]; then
33 # Sometimes commands will run with no path (e.g. a git command run from
34 # within the depot_tools dir itself). In that case, treat it as if run like:
35 # "./command"
36 DEPOT_TOOLS="."
37 BASENAME="$0"
38 else
39 BASENAME="${0##*/}"
40 fi
41else
42 BASENAME="${0##*\\}"
43fi
iannucci@chromium.org0703ea22016-04-01 01:02:42 +000044
mmoss@chromium.org375765c2015-10-30 15:39:24 +000045SCRIPT="${SCRIPT-${BASENAME//-/_}.py}"
46
Edward Lemur1f3bafb2019-10-08 17:56:33 +000047# Ensure that "depot_tools" is somewhere in PATH so this tool can be used
48# standalone, but allow other PATH manipulations to take priority.
49PATH=$PATH:$DEPOT_TOOLS
50
iannucci@chromium.org0703ea22016-04-01 01:02:42 +000051if [[ $PYTHON_DIRECT = 1 ]]; then
52 python.exe "$DEPOT_TOOLS\\$SCRIPT" "$@"
Edward Lemur4d7d53b2020-02-28 23:28:56 +000053elif [[ -e "$DEPOT_TOOLS/python.bat" && $OSTYPE = msys ]]; then
Edward Lemur1f3bafb2019-10-08 17:56:33 +000054 cmd.exe //c "$DEPOT_TOOLS\\vpython.bat" "$DEPOT_TOOLS\\$SCRIPT" "$@"
Edward Lemur4d7d53b2020-02-28 23:28:56 +000055elif [[ $GCLIENT_PY3 = 1 ]]; then
56 # Explicitly run on Python 3
57 vpython3 "$DEPOT_TOOLS/$SCRIPT" "$@"
58elif [[ $GCLIENT_PY3 = 0 ]]; then
59 # Explicitly run on Python 2
60 vpython "$DEPOT_TOOLS/$SCRIPT" "$@"
61else
62 # Run on Python 2 for now, allows default to be flipped.
63 vpython "$DEPOT_TOOLS/$SCRIPT" "$@"
mmoss@chromium.org375765c2015-10-30 15:39:24 +000064fi