Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 1 | #!/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 | |
| 7 | Swift format binaries are pulled down from CIPD whenever you sync Chrome. |
| 8 | This script knows how to locate those tools, assuming the script is |
| 9 | invoked from inside a Chromium checkout.""" |
| 10 | |
| 11 | import gclient_paths |
| 12 | import os |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | class NotFoundError(Exception): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 18 | """A file could not be found.""" |
| 19 | def __init__(self, e): |
| 20 | Exception.__init__( |
| 21 | self, |
| 22 | 'Problem while looking for swift-format in Chromium source tree:\n' |
| 23 | '%s' % e) |
Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def FindSwiftFormatToolInChromiumTree(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 27 | """Return a path to the rustfmt executable, or die trying.""" |
| 28 | chromium_src_path = gclient_paths.GetPrimarySolutionPath() |
| 29 | if not chromium_src_path: |
| 30 | raise NotFoundError( |
| 31 | 'Could not find checkout in any parent of the current path.\n' |
| 32 | 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium ' |
| 33 | 'checkout.') |
Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 34 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 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 |
Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def IsSwiftFormatSupported(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 43 | if sys.platform != 'darwin': |
| 44 | return False |
| 45 | try: |
| 46 | FindSwiftFormatToolInChromiumTree() |
| 47 | return True |
| 48 | except NotFoundError: |
| 49 | return False |
Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def main(args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 53 | try: |
| 54 | tool = FindSwiftFormatToolInChromiumTree() |
| 55 | except NotFoundError as e: |
| 56 | sys.stderr.write("%s\n" % str(e)) |
| 57 | return 1 |
Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 58 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 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 swift-format at:\n %s\n' % |
| 64 | tool) |
Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 65 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 66 | return subprocess.call([tool] + args) |
Olivier Robin | 0a6b544 | 2022-04-07 07:25:04 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame^] | 70 | try: |
| 71 | sys.exit(main(sys.argv[1:])) |
| 72 | except KeyboardInterrupt: |
| 73 | sys.stderr.write('interrupted\n') |
| 74 | sys.exit(1) |