OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Redirects to the version of clang-format checked into the Chrome tree. | 6 """Redirects to the version of clang-format checked into the Chrome tree. |
7 | 7 |
8 clang-format binaries are pulled down from Google Cloud Storage whenever you | 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 | 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.""" | 10 those tools, assuming the script is invoked from inside a Chromium checkout.""" |
11 | 11 |
12 import gclient_utils | 12 import gclient_utils |
13 import os | 13 import os |
14 import subprocess | 14 import subprocess |
15 import sys | 15 import sys |
16 | 16 |
17 | 17 |
18 class NotFoundError(Exception): | 18 class NotFoundError(Exception): |
19 """A file could not be found.""" | 19 """A file could not be found.""" |
20 def __init__(self, e): | 20 def __init__(self, e): |
21 Exception.__init__(self, | 21 Exception.__init__(self, |
22 'Problem while looking for clang-format in Chromium source tree:\n' | 22 'Problem while looking for clang-format in Chromium source tree:\n' |
23 ' %s' % e) | 23 ' %s' % e) |
24 | 24 |
25 | 25 |
26 def _FindChromiumSourceRoot(): | |
27 """Return the source root of the current chromium checkout, or die trying.""" | |
28 # The use of .gn is somewhat incongruous here, but we need a file uniquely | |
29 # existing at src/. GN does the same thing at least. | |
30 source_root = gclient_utils.FindFileUpwards('.gn') | |
31 if not source_root: | |
32 raise NotFoundError( | |
33 '.gn file not found in any parent of the current path.') | |
34 return source_root | |
35 | |
36 | |
37 def FindClangFormatToolInChromiumTree(): | 26 def FindClangFormatToolInChromiumTree(): |
38 """Return a path to the clang-format executable, or die trying.""" | 27 """Return a path to the clang-format executable, or die trying.""" |
39 tool_path = os.path.join(_FindChromiumSourceRoot(), 'third_party', | 28 bin_path = gclient_utils.GetBuildtoolsPlatformBinaryPath() |
40 'clang_format', 'bin', | 29 if not bin_path: |
41 gclient_utils.GetMacWinOrLinux(), | 30 raise NotFoundError( |
| 31 'Could not find checkout in any parent of the current path.') |
| 32 |
| 33 tool_path = os.path.join(bin_path, |
42 'clang-format' + gclient_utils.GetExeSuffix()) | 34 'clang-format' + gclient_utils.GetExeSuffix()) |
43 if not os.path.exists(tool_path): | 35 if not os.path.exists(tool_path): |
44 raise NotFoundError('File does not exist: %s' % tool_path) | 36 raise NotFoundError('File does not exist: %s' % tool_path) |
45 return tool_path | 37 return tool_path |
46 | 38 |
47 | 39 |
48 def FindClangFormatScriptInChromiumTree(script_name): | 40 def FindClangFormatScriptInChromiumTree(script_name): |
49 """Return a path to a clang-format helper script, or die trying.""" | 41 """Return a path to a clang-format helper script, or die trying.""" |
50 script_path = os.path.join(_FindChromiumSourceRoot(), 'third_party', | 42 tools_path = gclient_utils.GetBuildtoolsPath() |
51 'clang_format', 'script', script_name) | 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) |
52 if not os.path.exists(script_path): | 47 if not os.path.exists(script_path): |
53 raise NotFoundError('File does not exist: %s' % script_path) | 48 raise NotFoundError('File does not exist: %s' % script_path) |
54 return script_path | 49 return script_path |
55 | 50 |
56 | 51 |
57 def main(args): | 52 def main(args): |
58 try: | 53 try: |
59 tool = FindClangFormatToolInChromiumTree() | 54 tool = FindClangFormatToolInChromiumTree() |
60 except NotFoundError, e: | 55 except NotFoundError, e: |
61 print >> sys.stderr, e | 56 print >> sys.stderr, e |
62 sys.exit(1) | 57 sys.exit(1) |
63 | 58 |
64 # Add some visibility to --help showing where the tool lives, since this | 59 # Add some visibility to --help showing where the tool lives, since this |
65 # redirection can be a little opaque. | 60 # redirection can be a little opaque. |
66 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') | 61 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') |
67 if any(match in args for match in help_syntax): | 62 if any(match in args for match in help_syntax): |
68 print '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool | 63 print '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool |
69 | 64 |
70 return subprocess.call([tool] + sys.argv[1:]) | 65 return subprocess.call([tool] + sys.argv[1:]) |
71 | 66 |
72 | 67 |
73 if __name__ == '__main__': | 68 if __name__ == '__main__': |
74 sys.exit(main(sys.argv)) | 69 sys.exit(main(sys.argv)) |
OLD | NEW |