[email protected] | 4131006 | 2009-10-06 18:03:47 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import os |
[email protected] | 4131006 | 2009-10-06 18:03:47 | [diff] [blame] | 7 | import sys |
[email protected] | f6613be | 2010-06-30 01:14:05 | [diff] [blame] | 8 | from subprocess import Popen, PIPE |
[email protected] | 4131006 | 2009-10-06 18:03:47 | [diff] [blame] | 9 | |
[email protected] | f3c9280 | 2009-10-15 18:41:54 | [diff] [blame] | 10 | # Try locating depot_tools from the user's PATH. |
| 11 | depot_tools_path = None |
[email protected] | f6613be | 2010-06-30 01:14:05 | [diff] [blame] | 12 | |
| 13 | # First parse PATH if there's a "depot_tools" inside |
[email protected] | 94ba024 | 2009-10-15 21:35:41 | [diff] [blame] | 14 | for path in os.environ.get("PATH").split(os.pathsep): |
[email protected] | f6613be | 2010-06-30 01:14:05 | [diff] [blame] | 15 | if not path.endswith("depot_tools") and not path.endswith("depot_tools/"): |
[email protected] | 4131006 | 2009-10-06 18:03:47 | [diff] [blame] | 16 | continue |
[email protected] | f3c9280 | 2009-10-15 18:41:54 | [diff] [blame] | 17 | depot_tools_path = path |
[email protected] | 4131006 | 2009-10-06 18:03:47 | [diff] [blame] | 18 | break |
| 19 | |
[email protected] | f6613be | 2010-06-30 01:14:05 | [diff] [blame] | 20 | # If depot_tools dir is not called depot_tools, or other weirdness |
| 21 | if not depot_tools_path: |
| 22 | # Grab a `which gclient', which gives first match |
| 23 | # `which' also uses PATH, but is not restricted to specific directory name |
[email protected] | c98b047 | 2010-09-24 18:37:42 | [diff] [blame] | 24 | path = Popen(["which", "gclient"], stdout=PIPE).communicate()[0].strip() |
[email protected] | f6613be | 2010-06-30 01:14:05 | [diff] [blame] | 25 | if path: |
| 26 | depot_tools_path = path.replace("/gclient","") |
| 27 | |
[email protected] | f3c9280 | 2009-10-15 18:41:54 | [diff] [blame] | 28 | # If we found depot_tools, add it to the script's import path. |
[email protected] | f6613be | 2010-06-30 01:14:05 | [diff] [blame] | 29 | # Use realpath to normalize the actual path |
[email protected] | f3c9280 | 2009-10-15 18:41:54 | [diff] [blame] | 30 | if depot_tools_path: |
[email protected] | 075c639 | 2011-01-31 18:35:59 | [diff] [blame] | 31 | sys.path.insert(0, os.path.realpath(depot_tools_path)) |
[email protected] | f3c9280 | 2009-10-15 18:41:54 | [diff] [blame] | 32 | else: |
| 33 | print "ERROR: Could not find depot_tools in your PATH." |
| 34 | print "ERROR: Please add it to your PATH and try again." |
| 35 | sys.exit(1) |
| 36 | |
| 37 | # Try importing git_cl_hooks from depot_tools. |
| 38 | try: |
| 39 | import git_cl_hooks |
| 40 | except ImportError: |
[email protected] | 94ba024 | 2009-10-15 21:35:41 | [diff] [blame] | 41 | print "ERROR: Could not import git_cl_hooks from depot_tools in your PATH." |
[email protected] | f3c9280 | 2009-10-15 18:41:54 | [diff] [blame] | 42 | print "ERROR: Make sure %s is up-to-date and try again." % depot_tools_path |
| 43 | sys.exit(1) |
[email protected] | 4131006 | 2009-10-06 18:03:47 | [diff] [blame] | 44 | |
| 45 | # Ensure we were called with the necessary number of arguments. |
| 46 | program_name = os.path.basename(sys.argv[0]) |
[email protected] | 7658eb3 | 2009-10-08 18:00:31 | [diff] [blame] | 47 | if len(sys.argv) != 2: |
[email protected] | f3c9280 | 2009-10-15 18:41:54 | [diff] [blame] | 48 | print "usage: %s [upstream branch]" % program_name |
| 49 | sys.exit(1) |
[email protected] | 4131006 | 2009-10-06 18:03:47 | [diff] [blame] | 50 | |
[email protected] | 7658eb3 | 2009-10-08 18:00:31 | [diff] [blame] | 51 | # Run the hooks library with our arguments. |
| 52 | exec git_cl_hooks.RunHooks(hook_name=program_name, upstream_branch=sys.argv[1]) |