nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2014 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 | """Redirects to the version of clang-format checked into the Chrome tree. |
| 7 | |
| 8 | clang-format binaries are pulled down from Google Cloud Storage whenever you |
| 9 | sync Chrome, to platform-specific locations. This script knows how to locate |
| 10 | those tools, assuming the script is invoked from inside a Chromium checkout.""" |
| 11 | |
| 12 | import gclient_utils |
| 13 | import os |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | |
| 18 | class NotFoundError(Exception): |
| 19 | """A file could not be found.""" |
| 20 | def __init__(self, e): |
| 21 | Exception.__init__(self, |
| 22 | 'Problem while looking for clang-format in Chromium source tree:\n' |
| 23 | ' %s' % e) |
| 24 | |
| 25 | |
| 26 | def _FindChromiumTree(): |
| 27 | """Return the root of the current chromium checkout, or die trying.""" |
| 28 | source_root = gclient_utils.FindFileUpwards('.gclient') |
| 29 | if not source_root: |
| 30 | raise NotFoundError( |
| 31 | '.gclient file not found in any parent of the current path.') |
| 32 | return source_root |
| 33 | |
| 34 | |
| 35 | def FindClangFormatToolInChromiumTree(): |
| 36 | """Return a path to the clang-format executable, or die trying.""" |
| 37 | # The binaries in platform-specific subdirectories in src/tools/gn/bin. |
| 38 | tool_path = os.path.join(_FindChromiumTree(), 'src', 'third_party', |
| 39 | 'clang_format', 'bin', |
| 40 | gclient_utils.GetMacWinOrLinux(), |
| 41 | 'clang-format' + gclient_utils.GetExeSuffix()) |
| 42 | if not os.path.exists(tool_path): |
| 43 | # TODO(nick): After March 2014, eliminate the following advisory. |
| 44 | error_text = '''\n GIT CL FORMAT - WINTER WEATHER ADVISORY |
| 45 | |
| 46 | clang-format binaries now come with every Chrome checkout! |
| 47 | |
| 48 | Unfortunately, your depot_tools scripts tried to find clang-format binaries |
| 49 | in your Chrome checkout, but failed. This is expected if you haven't synced |
| 50 | since the binaries were added. |
| 51 | |
| 52 | 'git cl format' will probably not work until you sync your Chrome tree. |
| 53 | Sorry about that. |
| 54 | |
| 55 | Contact nick@chromium.org if you have any additional questions.\n\n''' |
| 56 | |
| 57 | error_text += 'File does not exist: %s' % tool_path |
| 58 | |
| 59 | raise NotFoundError(error_text) |
| 60 | return tool_path |
| 61 | |
| 62 | |
| 63 | def FindClangFormatScriptInChromiumTree(script_name): |
| 64 | """Return a path to a clang-format helper script, or die trying.""" |
| 65 | # The binaries in platform-specific subdirectories in src/tools/gn/bin. |
| 66 | script_path = os.path.join(_FindChromiumTree(), 'src', 'third_party', |
| 67 | 'clang_format', 'scripts', script_name) |
| 68 | if not os.path.exists(script_path): |
| 69 | raise NotFoundError('File does not exist: %s' % script_path) |
| 70 | return script_path |
| 71 | |
| 72 | |
| 73 | def main(args): |
| 74 | try: |
| 75 | tool = FindClangFormatToolInChromiumTree() |
| 76 | except NotFoundError, e: |
| 77 | print >> sys.stderr, e |
| 78 | sys.exit(1) |
| 79 | |
| 80 | # Add some visibility to --help showing where the tool lives, since this |
| 81 | # redirection can be a little opaque. |
| 82 | help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') |
| 83 | if any(match in args for match in help_syntax): |
| 84 | print '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool |
| 85 | |
| 86 | return subprocess.call([tool] + sys.argv[1:]) |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | sys.exit(main(sys.argv)) |