forked from google/conscrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_format.py
More file actions
126 lines (97 loc) · 3.98 KB
/
Copy pathfix_format.py
File metadata and controls
126 lines (97 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
import shutil
import subprocess
import sys
from pathlib import Path
from typing import List, FrozenSet
CLANG_FORMAT_BIN: str = "clang-format"
PROJECT_PREFIX: str = "//depot/google3/third_party/java/conscrypt/main_src/"
VALID_EXTENSIONS: FrozenSet[str] = frozenset({".java", ".cc", ".h", ".cpp", ".c"})
def get_g4_output() -> str:
"""Runs g4 opened and returns the stdout."""
try:
return subprocess.check_output(["g4", "opened"], text=True)
except subprocess.CalledProcessError as e:
sys.exit(f"ERROR: Failed to run 'g4 opened'.\nDetails: {e}")
def parse_files(g4_output: str, project_prefix: str, root_dir: Path) -> List[str]:
"""Parses g4 output and returns a list of absolute file paths to format."""
files_to_format: List[str] = []
for line in g4_output.splitlines():
line = line.strip()
if not line:
continue
depot_path = line.split('#')[0]
if not depot_path.endswith(tuple(VALID_EXTENSIONS)):
continue
if not depot_path.startswith(project_prefix):
continue
relative_path = depot_path[len(project_prefix):]
abs_path = root_dir / relative_path
files_to_format.append(str(abs_path))
return files_to_format
def get_jj_output() -> str:
"""Runs jj diff and returns the stdout."""
try:
return subprocess.check_output(["jj", "diff", "--name-only", "-r", "@"], text=True)
except subprocess.CalledProcessError as e:
sys.exit(f"ERROR: Failed to run 'jj diff'.\nDetails: {e}")
def parse_files_jj(jj_output: str, project_prefix: str, root_dir: Path) -> List[str]:
"""Parses jj output and returns a list of absolute file paths to format."""
files_to_format: List[str] = []
for line in jj_output.splitlines():
line = line.strip()
if not line:
continue
if not line.endswith(tuple(VALID_EXTENSIONS)):
continue
prefix = project_prefix.removeprefix("//depot/google3/")
if prefix and not line.startswith(prefix):
continue
relative_path = line[len(prefix):]
abs_path = root_dir / relative_path
files_to_format.append(str(abs_path))
return files_to_format
def is_jj_workspace() -> bool:
if not shutil.which("jj"):
return False
return subprocess.run(["jj", "root"], capture_output=True).returncode == 0
def is_g4_workspace() -> bool:
if not shutil.which("g4"):
return False
proc = subprocess.run(["g4", "opened"], capture_output=True, text=True)
return "unknown" not in proc.stderr and "unknown" not in proc.stdout
def main() -> None:
script_dir = Path(__file__).resolve().parent
config_path = script_dir / ".clang-format"
try:
if not config_path.is_file():
raise FileNotFoundError(f"Config file missing at {config_path}")
except OSError as e:
sys.exit(f"ERROR: Could not access config file.\nDetails: {e}")
if not shutil.which(CLANG_FORMAT_BIN):
sys.exit(f"ERROR: '{CLANG_FORMAT_BIN}' not found in PATH.")
print("Querying opened files...")
if is_g4_workspace():
g4_output = get_g4_output()
files_to_format = parse_files(g4_output, PROJECT_PREFIX, script_dir)
elif is_jj_workspace():
jj_output = get_jj_output()
files_to_format = parse_files_jj(jj_output, PROJECT_PREFIX, script_dir)
else:
sys.exit("ERROR: Could not determine if we are in a g4 or jj workspace.")
if not files_to_format:
print(f"No source files under {PROJECT_PREFIX} are currently open.")
sys.exit(0)
print(f"Formatting {len(files_to_format)} file(s) with config: {config_path}")
cmd = [
CLANG_FORMAT_BIN,
"-i",
f"-style=file:{config_path}",
] + files_to_format
try:
subprocess.run(cmd, check=True)
print("Done.")
except subprocess.CalledProcessError as e:
sys.exit(f"ERROR: clang-format failed.\nDetails: {e}")
if __name__ == "__main__":
main()