Christoffer Quist Adamsen | bfe52fd | 2022-02-15 14:32:52 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Christoffer Quist Adamsen | 3d14bf2 | 2020-03-19 09:40:47 +0100 | [diff] [blame] | 2 | # Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
Christoffer Quist Adamsen | 4feb9a1 | 2023-10-16 11:12:36 +0200 | [diff] [blame] | 6 | import argparse |
Christoffer Quist Adamsen | 3d14bf2 | 2020-03-19 09:40:47 +0100 | [diff] [blame] | 7 | import os |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
| 11 | import utils |
| 12 | |
| 13 | from subprocess import Popen, PIPE |
| 14 | |
Christoffer Quist Adamsen | 4feb9a1 | 2023-10-16 11:12:36 +0200 | [diff] [blame] | 15 | GOOGLE_JAVA_FORMAT_DIFF = os.path.join(utils.THIRD_PARTY, 'google', |
Søren Gjesse | b557f92 | 2024-11-22 09:31:01 +0100 | [diff] [blame^] | 16 | 'google-java-format', '1.24.0', |
| 17 | 'google-java-format-1.24.0', 'scripts', |
Christoffer Quist Adamsen | 4feb9a1 | 2023-10-16 11:12:36 +0200 | [diff] [blame] | 18 | 'google-java-format-diff.py') |
| 19 | |
| 20 | GOOGLE_YAPF = os.path.join(utils.THIRD_PARTY, 'google/yapf/20231013') |
| 21 | |
| 22 | |
| 23 | def ParseOptions(): |
| 24 | result = argparse.ArgumentParser() |
| 25 | result.add_argument('--no-java', |
| 26 | help='Run google-java-format.', |
| 27 | action='store_true', |
| 28 | default=False) |
| 29 | result.add_argument('--python', |
| 30 | help='Run YAPF.', |
| 31 | action='store_true', |
| 32 | default=False) |
| 33 | return result.parse_known_args() |
| 34 | |
| 35 | |
| 36 | def FormatJava(upstream): |
| 37 | git_diff_process = Popen(['git', 'diff', '-U0', upstream], stdout=PIPE) |
| 38 | fmt_process = Popen([sys.executable, GOOGLE_JAVA_FORMAT_DIFF, '-p1', '-i'], |
| 39 | stdin=git_diff_process.stdout) |
| 40 | git_diff_process.stdout.close() |
| 41 | fmt_process.communicate() |
| 42 | |
| 43 | |
| 44 | def FormatPython(upstream): |
| 45 | changed_files_cmd = ['git', 'diff', '--name-only', upstream, '*.py'] |
| 46 | changed_files = subprocess.check_output(changed_files_cmd).decode( |
| 47 | 'utf-8').splitlines() |
| 48 | if not changed_files: |
| 49 | return |
| 50 | format_cmd = [ |
| 51 | sys.executable, |
| 52 | os.path.join(GOOGLE_YAPF, 'yapf'), '--in-place', '--style', 'google' |
| 53 | ] |
| 54 | format_cmd.extend(changed_files) |
| 55 | yapf_python_path = [GOOGLE_YAPF, os.path.join(GOOGLE_YAPF, 'third_party')] |
| 56 | subprocess.check_call(format_cmd, |
| 57 | env={'PYTHONPATH': ':'.join(yapf_python_path)}) |
| 58 | |
Christoffer Quist Adamsen | 3d14bf2 | 2020-03-19 09:40:47 +0100 | [diff] [blame] | 59 | |
| 60 | def main(): |
Christoffer Quist Adamsen | 4feb9a1 | 2023-10-16 11:12:36 +0200 | [diff] [blame] | 61 | (options, args) = ParseOptions() |
| 62 | upstream = subprocess.check_output(['git', 'cl', |
| 63 | 'upstream']).decode('utf-8').strip() |
| 64 | if not options.no_java: |
| 65 | FormatJava(upstream) |
| 66 | if options.python: |
| 67 | FormatPython(upstream) |
| 68 | |
Christoffer Quist Adamsen | 3d14bf2 | 2020-03-19 09:40:47 +0100 | [diff] [blame] | 69 | |
| 70 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 4feb9a1 | 2023-10-16 11:12:36 +0200 | [diff] [blame] | 71 | sys.exit(main()) |