blob: 5bfeb1ae9e8a78a51adf0d1391a85b10e1fcd1a6 [file] [log] [blame]
[email protected]3ac1c4e2014-01-16 02:44:421#!/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
8clang-format binaries are pulled down from Google Cloud Storage whenever you
9sync Chrome, to platform-specific locations. This script knows how to locate
10those tools, assuming the script is invoked from inside a Chromium checkout."""
11
12import gclient_utils
13import os
14import subprocess
15import sys
16
17
18class 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]3ac1c4e2014-01-16 02:44:4226def FindClangFormatToolInChromiumTree():
27 """Return a path to the clang-format executable, or die trying."""
[email protected]aaee92f2014-07-02 07:35:3128 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]3ac1c4e2014-01-16 02:44:4234 'clang-format' + gclient_utils.GetExeSuffix())
35 if not os.path.exists(tool_path):
[email protected]8ca1aa32014-02-25 23:57:0336 raise NotFoundError('File does not exist: %s' % tool_path)
[email protected]3ac1c4e2014-01-16 02:44:4237 return tool_path
38
39
40def FindClangFormatScriptInChromiumTree(script_name):
41 """Return a path to a clang-format helper script, or die trying."""
[email protected]aaee92f2014-07-02 07:35:3142 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]3ac1c4e2014-01-16 02:44:4247 if not os.path.exists(script_path):
[email protected]8ca1aa32014-02-25 23:57:0348 raise NotFoundError('File does not exist: %s' % script_path)
[email protected]3ac1c4e2014-01-16 02:44:4249 return script_path
50
51
52def 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]013731e2015-02-26 18:28:4365 return subprocess.call([tool] + args)
[email protected]3ac1c4e2014-01-16 02:44:4266
67
68if __name__ == '__main__':
[email protected]013731e2015-02-26 18:28:4369 try:
[email protected]c432a7f2015-02-28 19:20:2270 sys.exit(main(sys.argv[1:]))
[email protected]013731e2015-02-26 18:28:4371 except KeyboardInterrupt:
72 sys.stderr.write('interrupted\n')
73 sys.exit(1)