|
33 | 33 | import subprocess
|
34 | 34 | import io
|
35 | 35 | import sys
|
| 36 | +from concurrent.futures import ThreadPoolExecutor,wait,FIRST_EXCEPTION |
36 | 37 | from shutil import which
|
37 | 38 |
|
| 39 | +def _apply_format(filename, lines, base_command, args): |
| 40 | + """Apply format on filename.""" |
| 41 | + if args.i and args.verbose: |
| 42 | + print('Formatting', filename) |
| 43 | + |
| 44 | + command = base_command[:] |
| 45 | + command.extend(lines) |
| 46 | + command.append(filename) |
| 47 | + p = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 48 | + stderr=None, stdin=subprocess.PIPE) |
| 49 | + stdout, _ = p.communicate() |
| 50 | + if p.returncode != 0: |
| 51 | + sys.exit(p.returncode) |
| 52 | + |
| 53 | + if not args.i: |
| 54 | + with open(filename) as f: |
| 55 | + code = f.readlines() |
| 56 | + formatted_code = io.StringIO(stdout.decode('utf-8')).readlines() |
| 57 | + diff = difflib.unified_diff(code, formatted_code, |
| 58 | + filename, filename, |
| 59 | + '(before formatting)', '(after formatting)') |
| 60 | + diff_string = ''.join(diff) |
| 61 | + if len(diff_string) > 0: |
| 62 | + sys.stdout.write(diff_string) |
| 63 | + |
38 | 64 | def main():
|
39 | 65 | parser = argparse.ArgumentParser(description=
|
40 | 66 | 'Reformat changed lines in diff. Without -i '
|
@@ -108,39 +134,29 @@ def main():
|
108 | 134 | binary = which('google-java-format') or '/usr/bin/google-java-format'
|
109 | 135 | base_command = [binary]
|
110 | 136 |
|
111 |
| - # Reformat files containing changes in place. |
112 |
| - for filename, lines in lines_by_file.items(): |
113 |
| - if args.i and args.verbose: |
114 |
| - print('Formatting', filename) |
115 |
| - command = base_command[:] |
116 |
| - if args.i: |
117 |
| - command.append('-i') |
118 |
| - if args.aosp: |
119 |
| - command.append('--aosp') |
120 |
| - if args.skip_sorting_imports: |
121 |
| - command.append('--skip-sorting-imports') |
122 |
| - if args.skip_removing_unused_imports: |
123 |
| - command.append('--skip-removing-unused-imports') |
124 |
| - if args.skip_javadoc_formatting: |
125 |
| - command.append('--skip-javadoc-formatting') |
126 |
| - command.extend(lines) |
127 |
| - command.append(filename) |
128 |
| - p = subprocess.Popen(command, stdout=subprocess.PIPE, |
129 |
| - stderr=None, stdin=subprocess.PIPE) |
130 |
| - stdout, stderr = p.communicate() |
131 |
| - if p.returncode != 0: |
132 |
| - sys.exit(p.returncode); |
133 |
| - |
134 |
| - if not args.i: |
135 |
| - with open(filename) as f: |
136 |
| - code = f.readlines() |
137 |
| - formatted_code = io.StringIO(stdout.decode('utf-8')).readlines() |
138 |
| - diff = difflib.unified_diff(code, formatted_code, |
139 |
| - filename, filename, |
140 |
| - '(before formatting)', '(after formatting)') |
141 |
| - diff_string = ''.join(diff) |
142 |
| - if len(diff_string) > 0: |
143 |
| - sys.stdout.write(diff_string) |
| 137 | + if args.i: |
| 138 | + base_command.append('-i') |
| 139 | + if args.aosp: |
| 140 | + base_command.append('--aosp') |
| 141 | + if args.skip_sorting_imports: |
| 142 | + base_command.append('--skip-sorting-imports') |
| 143 | + if args.skip_removing_unused_imports: |
| 144 | + base_command.append('--skip-removing-unused-imports') |
| 145 | + if args.skip_javadoc_formatting: |
| 146 | + base_command.append('--skip-javadoc-formatting') |
| 147 | + |
| 148 | + with ThreadPoolExecutor() as executor: |
| 149 | + format_futures = [] |
| 150 | + for filename, lines in lines_by_file.items(): |
| 151 | + format_futures.append( |
| 152 | + executor.submit(_apply_format, filename, lines, base_command, args) |
| 153 | + ) |
| 154 | + |
| 155 | + done, _ = wait(format_futures, return_when=FIRST_EXCEPTION) |
| 156 | + for future in done: |
| 157 | + if exception := future.exception(): |
| 158 | + executor.shutdown(wait=True, cancel_futures=True) |
| 159 | + sys.exit(exception.args[0]) |
144 | 160 |
|
145 | 161 | if __name__ == '__main__':
|
146 | 162 | main()
|
0 commit comments