[email protected] | 3ac1c4e | 2014-01-16 02:44:42 | [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 | |
[email protected] | 3ac1c4e | 2014-01-16 02:44:42 | [diff] [blame] | 26 | def FindClangFormatToolInChromiumTree(): |
| 27 | """Return a path to the clang-format executable, or die trying.""" |
[email protected] | aaee92f | 2014-07-02 07:35:31 | [diff] [blame] | 28 | bin_path = gclient_utils.GetBuildtoolsPlatformBinaryPath() |
| 29 | if not bin_path: |
| 30 | raise NotFoundError( |
| 31 | 'Could not find checkout in any parent of the current path.') |
| 32 | |
| 33 | tool_path = os.path.join(bin_path, |
[email protected] | 3ac1c4e | 2014-01-16 02:44:42 | [diff] [blame] | 34 | 'clang-format' + gclient_utils.GetExeSuffix()) |
| 35 | if not os.path.exists(tool_path): |
[email protected] | 8ca1aa3 | 2014-02-25 23:57:03 | [diff] [blame] | 36 | raise NotFoundError('File does not exist: %s' % tool_path) |
[email protected] | 3ac1c4e | 2014-01-16 02:44:42 | [diff] [blame] | 37 | return tool_path |
| 38 | |
| 39 | |
| 40 | def FindClangFormatScriptInChromiumTree(script_name): |
| 41 | """Return a path to a clang-format helper script, or die trying.""" |
[email protected] | aaee92f | 2014-07-02 07:35:31 | [diff] [blame] | 42 | tools_path = gclient_utils.GetBuildtoolsPath() |
| 43 | if not tools_path: |
| 44 | raise NotFoundError( |
| 45 | 'Could not find checkout in any parent of the current path.') |
| 46 | script_path = os.path.join(tools_path, 'clang_format', 'script', script_name) |
[email protected] | 3ac1c4e | 2014-01-16 02:44:42 | [diff] [blame] | 47 | if not os.path.exists(script_path): |
[email protected] | 8ca1aa3 | 2014-02-25 23:57:03 | [diff] [blame] | 48 | raise NotFoundError('File does not exist: %s' % script_path) |
[email protected] | 3ac1c4e | 2014-01-16 02:44:42 | [diff] [blame] | 49 | return script_path |
| 50 | |
| 51 | |
| 52 | def main(args): |
| 53 | try: |
| 54 | tool = FindClangFormatToolInChromiumTree() |
| 55 | except NotFoundError, e: |
| 56 | print >> sys.stderr, e |
| 57 | sys.exit(1) |
| 58 | |
| 59 | # Add some visibility to --help showing where the tool lives, since this |
| 60 | # redirection can be a little opaque. |
| 61 | help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') |
| 62 | if any(match in args for match in help_syntax): |
| 63 | print '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool |
| 64 | |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 65 | return subprocess.call([tool] + args) |
[email protected] | 3ac1c4e | 2014-01-16 02:44:42 | [diff] [blame] | 66 | |
| 67 | |
| 68 | if __name__ == '__main__': |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 69 | try: |
[email protected] | c432a7f | 2015-02-28 19:20:22 | [diff] [blame] | 70 | sys.exit(main(sys.argv[1:])) |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 71 | except KeyboardInterrupt: |
| 72 | sys.stderr.write('interrupted\n') |
| 73 | sys.exit(1) |