1
+ #!/usr/bin/env python3
2
+ """
3
+ Format fix script for tritonparse project.
4
+
5
+ This script runs all linter tools to format and fix code issues:
6
+ - usort: Import sorting
7
+ - ruff: Code formatting and linting
8
+ - black: Code formatting (fallback)
9
+
10
+ Usage:
11
+ python -m tritonparse.tools.format_fix [options]
12
+
13
+ Options:
14
+ --check-only Only check for issues, don't fix them
15
+ --verbose Verbose output
16
+ --help Show this help message
17
+ """
18
+
19
+ import argparse
20
+ import subprocess
21
+ import sys
22
+ from pathlib import Path
23
+ from typing import List
24
+
25
+
26
+ def run_command (cmd : List [str ], verbose : bool = False ) -> bool :
27
+ """Run a command and return success status."""
28
+ if verbose :
29
+ print (f"Running: { ' ' .join (cmd )} " )
30
+
31
+ try :
32
+ result = subprocess .run (
33
+ cmd ,
34
+ capture_output = True ,
35
+ text = True ,
36
+ check = False
37
+ )
38
+
39
+ if result .returncode != 0 :
40
+ if verbose :
41
+ print (f"Command failed with return code { result .returncode } " )
42
+ if result .stdout :
43
+ print ("STDOUT:" , result .stdout )
44
+ if result .stderr :
45
+ print ("STDERR:" , result .stderr )
46
+ return False
47
+
48
+ if verbose and result .stdout :
49
+ print (result .stdout )
50
+
51
+ return True
52
+ except Exception as e :
53
+ if verbose :
54
+ print (f"Error running command: { e } " )
55
+ return False
56
+
57
+
58
+ def get_python_files () -> List [str ]:
59
+ """Get all Python files to format."""
60
+ project_root = Path (__file__ ).parent .parent .parent
61
+ python_files = []
62
+
63
+ # Add main package files
64
+ for pattern in ["*.py" , "tests/*.py" ]:
65
+ python_files .extend (str (f ) for f in project_root .glob (pattern ))
66
+
67
+ # Filter out __pycache__ and other unwanted directories
68
+ python_files = [
69
+ f for f in python_files
70
+ if "__pycache__" not in f and ".git" not in f
71
+ ]
72
+
73
+ return python_files
74
+
75
+
76
+ def run_usort (files : List [str ], check_only : bool = False ,
77
+ verbose : bool = False ) -> bool :
78
+ """Run usort for import sorting."""
79
+ cmd = ["usort" ]
80
+
81
+ if check_only :
82
+ cmd .append ("check" )
83
+ else :
84
+ cmd .append ("format" )
85
+
86
+ cmd .extend (files )
87
+
88
+ return run_command (cmd , verbose )
89
+
90
+
91
+ def run_ruff (files : List [str ], check_only : bool = False ,
92
+ verbose : bool = False ) -> bool :
93
+ """Run ruff for code formatting and linting."""
94
+ cmd = ["ruff" ]
95
+
96
+ if check_only :
97
+ cmd .extend (["check" , "--diff" ])
98
+ else :
99
+ cmd .extend (["format" ])
100
+
101
+ cmd .extend (files )
102
+
103
+ return run_command (cmd , verbose )
104
+
105
+
106
+ def run_black (files : List [str ], check_only : bool = False ,
107
+ verbose : bool = False ) -> bool :
108
+ """Run black for code formatting (fallback)."""
109
+ cmd = ["black" ]
110
+
111
+ if check_only :
112
+ cmd .append ("--check" )
113
+ else :
114
+ cmd .append ("--quiet" )
115
+
116
+ cmd .extend (files )
117
+
118
+ return run_command (cmd , verbose )
119
+
120
+
121
+ def main ():
122
+ """Main function."""
123
+ parser = argparse .ArgumentParser (
124
+ description = "Format fix script for tritonparse project" ,
125
+ epilog = """
126
+ Examples:
127
+ # Fix all formatting issues
128
+ python -m tritonparse.tools.format_fix
129
+
130
+ # Check for issues without fixing
131
+ python -m tritonparse.tools.format_fix --check-only
132
+
133
+ # Verbose output
134
+ python -m tritonparse.tools.format_fix --verbose
135
+ """
136
+ )
137
+
138
+ parser .add_argument (
139
+ "--check-only" ,
140
+ action = "store_true" ,
141
+ help = "Only check for issues, don't fix them"
142
+ )
143
+ parser .add_argument (
144
+ "--verbose" ,
145
+ action = "store_true" ,
146
+ help = "Verbose output"
147
+ )
148
+
149
+ args = parser .parse_args ()
150
+
151
+ # Get Python files to format
152
+ files = get_python_files ()
153
+
154
+ if args .verbose :
155
+ print (f"Found { len (files )} Python files to process:" )
156
+ for f in files :
157
+ print (f" { f } " )
158
+ print ()
159
+
160
+ # Run formatters
161
+ success = True
162
+
163
+ # 1. Run usort for import sorting
164
+ print ("Running usort for import sorting..." )
165
+ if not run_usort (files , args .check_only , args .verbose ):
166
+ print ("❌ usort failed" )
167
+ success = False
168
+ else :
169
+ print ("✅ usort completed" )
170
+
171
+ # 2. Run ruff for code formatting and linting
172
+ print ("Running ruff for code formatting and linting..." )
173
+ if not run_ruff (files , args .check_only , args .verbose ):
174
+ print ("❌ ruff failed" )
175
+ success = False
176
+ else :
177
+ print ("✅ ruff completed" )
178
+
179
+ # 3. Run black as fallback
180
+ print ("Running black for additional formatting..." )
181
+ if not run_black (files , args .check_only , args .verbose ):
182
+ print ("❌ black failed" )
183
+ success = False
184
+ else :
185
+ print ("✅ black completed" )
186
+
187
+ if success :
188
+ print ("\n 🎉 All formatting tools completed successfully!" )
189
+ return 0
190
+ else :
191
+ print ("\n ❌ Some formatting tools failed. Please check the output above." )
192
+ return 1
193
+
194
+
195
+ if __name__ == "__main__" :
196
+ sys .exit (main ())
0 commit comments