git-cl-upload-hook 1.78 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/python
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import sys
8
from subprocess import Popen, PIPE
9

10 11
# Try locating depot_tools from the user's PATH.
depot_tools_path = None
12 13

# First parse PATH if there's a "depot_tools" inside
14
for path in os.environ.get("PATH").split(os.pathsep):
15
  if not path.endswith("depot_tools") and not path.endswith("depot_tools/"):
16
    continue
17
  depot_tools_path = path
18 19
  break

20 21 22 23
# If depot_tools dir is not called depot_tools, or other weirdness
if not depot_tools_path:
  # Grab a `which gclient', which gives first match
  # `which' also uses PATH, but is not restricted to specific directory name
24
  path = Popen(["which", "gclient"], stdout=PIPE).communicate()[0].strip()
25 26 27
  if path:
    depot_tools_path = path.replace("/gclient","")

28
# If we found depot_tools, add it to the script's import path.
29
# Use realpath to normalize the actual path
30
if depot_tools_path:
31
  sys.path.insert(0, os.path.realpath(depot_tools_path))
32 33 34 35 36 37 38 39 40
else:
  print "ERROR: Could not find depot_tools in your PATH."
  print "ERROR: Please add it to your PATH and try again."
  sys.exit(1)

# Try importing git_cl_hooks from depot_tools.
try:
  import git_cl_hooks
except ImportError:
41
  print "ERROR: Could not import git_cl_hooks from depot_tools in your PATH."
42 43
  print "ERROR: Make sure %s is up-to-date and try again." % depot_tools_path
  sys.exit(1)
44 45 46

# Ensure we were called with the necessary number of arguments.
program_name = os.path.basename(sys.argv[0])
47
if len(sys.argv) != 2:
48 49
  print "usage: %s [upstream branch]" % program_name
  sys.exit(1)
50

51 52
# Run the hooks library with our arguments.
exec git_cl_hooks.RunHooks(hook_name=program_name, upstream_branch=sys.argv[1])