python_runner.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # 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. ## This file is designed to be sourced from a bash script whose name takes the
  5. ## form 'command-name'. This script will then instead invoke
  6. ## '[depot_tools]/command_name.py' correctly under mingw as well
  7. ## as posix-ey systems, passing along all other command line flags.
  8. ## Example:
  9. ## echo ". python_runner.sh" > git-foo-command
  10. ## ./git-foo-command #=> runs `python git_foo_command.py`
  11. ## Constants
  12. PYTHONDONTWRITEBYTECODE=1
  13. ## "Input parameters".
  14. # If set before the script is sourced, then we'll use the pre-set values.
  15. #
  16. # SCRIPT defaults to the basename of $0, with dashes replaced with underscores
  17. # "$0" can have several different formats depending on how the script was called
  18. # and the environment being used, including having different formats even in the
  19. # same environment (e.g. in msys, 'git cl' causes $0 to have a Windows-style
  20. # path, but calling 'git-cl' results in a POSIX-style path), so don't assume a
  21. # particular format.
  22. # First try to split it using Windows format ...
  23. DEPOT_TOOLS="${0%\\*}"
  24. if [[ "$DEPOT_TOOLS" = "$0" ]]; then
  25. # If that didn't work, try POSIX format ...
  26. DEPOT_TOOLS="${0%/*}"
  27. if [[ "$DEPOT_TOOLS" = "$0" ]]; then
  28. # Sometimes commands will run with no path (e.g. a git command run from
  29. # within the depot_tools dir itself). In that case, treat it as if run like:
  30. # "./command"
  31. DEPOT_TOOLS="."
  32. BASENAME="$0"
  33. else
  34. BASENAME="${0##*/}"
  35. fi
  36. else
  37. BASENAME="${0##*\\}"
  38. fi
  39. SCRIPT="${SCRIPT-${BASENAME//-/_}.py}"
  40. # Ensure that "depot_tools" is somewhere in PATH so this tool can be used
  41. # standalone, but allow other PATH manipulations to take priority.
  42. PATH=$PATH:$DEPOT_TOOLS
  43. if [[ $PYTHON_DIRECT = 1 ]]; then
  44. python.exe "$DEPOT_TOOLS\\$SCRIPT" "$@"
  45. else
  46. if [[ -e "$DEPOT_TOOLS/python.bat" && $OSTYPE = msys ]]; then
  47. cmd.exe //c "$DEPOT_TOOLS\\vpython.bat" "$DEPOT_TOOLS\\$SCRIPT" "$@"
  48. else
  49. vpython "$DEPOT_TOOLS/$SCRIPT" "$@"
  50. fi
  51. fi