Skip to content

Commit 3ee6e2a

Browse files
java-team-github-botgoogle-java-format Team
authored and
google-java-format Team
committed
Java-format: format multiples files in parallel to improve speed
PiperOrigin-RevId: 620100015
1 parent 71a755b commit 3ee6e2a

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

scripts/google-java-format-diff.py

+49-33
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,34 @@
3333
import subprocess
3434
import io
3535
import sys
36+
from concurrent.futures import ThreadPoolExecutor,wait,FIRST_EXCEPTION
3637
from shutil import which
3738

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+
3864
def main():
3965
parser = argparse.ArgumentParser(description=
4066
'Reformat changed lines in diff. Without -i '
@@ -108,39 +134,29 @@ def main():
108134
binary = which('google-java-format') or '/usr/bin/google-java-format'
109135
base_command = [binary]
110136

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])
144160

145161
if __name__ == '__main__':
146162
main()

0 commit comments

Comments
 (0)