python_runner.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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; export PYTHONDONTWRITEBYTECODE
  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. vpython3 "$DEPOT_TOOLS/$SCRIPT" "$@"