blob: 50536fe5e931ddbd13958629575843fa95803e44 [file] [log] [blame]
Olivier Robin0a6b5442022-04-07 07:25:04 +00001#!/usr/bin/env python3
2# Copyright 2022 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"""Redirects to the version of swift-format present in the Chrome tree.
6
7Swift format binaries are pulled down from CIPD whenever you sync Chrome.
8This script knows how to locate those tools, assuming the script is
9invoked from inside a Chromium checkout."""
10
11import gclient_paths
12import os
13import subprocess
14import sys
15
16
17class NotFoundError(Exception):
18 """A file could not be found."""
19
20 def __init__(self, e):
21 Exception.__init__(
22 self,
23 'Problem while looking for swift-format in Chromium source tree:\n'
24 '%s' % e)
25
26
27def FindSwiftFormatToolInChromiumTree():
28 """Return a path to the rustfmt executable, or die trying."""
29 chromium_src_path = gclient_paths.GetPrimarySolutionPath()
30 if not chromium_src_path:
31 raise NotFoundError(
32 'Could not find checkout in any parent of the current path.\n'
33 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
34
35 tool_path = os.path.join(chromium_src_path, 'third_party', 'swift-format',
36 'swift-format')
37 if not os.path.exists(tool_path):
38 raise NotFoundError('File does not exist: %s' % tool_path)
39 return tool_path
40
41
42def IsSwiftFormatSupported():
43 try:
44 FindSwiftFormatToolInChromiumTree()
45 return True
46 except NotFoundError:
47 return False
48
49
50def main(args):
51 try:
52 tool = FindSwiftFormatToolInChromiumTree()
53 except NotFoundError as e:
54 sys.stderr.write("%s\n" % str(e))
55 return 1
56
57 # Add some visibility to --help showing where the tool lives, since this
58 # redirection can be a little opaque.
59 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
60 if any(match in args for match in help_syntax):
61 print('\nDepot tools redirects you to the swift-format at:\n %s\n' %
62 tool)
63
64 return subprocess.call([tool] + args)
65
66
67if __name__ == '__main__':
68 try:
69 sys.exit(main(sys.argv[1:]))
70 except KeyboardInterrupt:
71 sys.stderr.write('interrupted\n')
72 sys.exit(1)