@@ -89,16 +89,13 @@ def is_application(f):
89
89
pattern_exemptions = dict (
90
90
(
91
91
re .compile (file_pattern ),
92
- dict (
93
- (code , [re .compile (p ) for p in patterns ])
94
- for code , patterns in error_dict .items ()
95
- ),
92
+ dict ((code , [re .compile (p ) for p in patterns ]) for code , patterns in error_dict .items ()),
96
93
)
97
94
for file_pattern , error_dict in pattern_exemptions .items ()
98
95
)
99
96
100
-
101
- tool_names = ["flake8" ]
97
+ # Tools run in the given order, with flake8 as the last check.
98
+ tool_names = ["black" , " flake8" ]
102
99
103
100
tools = {}
104
101
@@ -221,9 +218,17 @@ def setup_parser(subparser):
221
218
action = "append" ,
222
219
help = "specify tools to skip (choose from %s)" % "," .join (tool_names ),
223
220
)
224
- subparser .add_argument (
225
- "files" , nargs = argparse .REMAINDER , help = "specific files to check"
226
- )
221
+ subparser .add_argument ("files" , nargs = argparse .REMAINDER , help = "specific files to check" )
222
+
223
+
224
+ def print_tool_header (tool , file_list ):
225
+ print ("=======================================================" )
226
+ print (f"{ tool } : running { tool } checks on ramble." )
227
+ print ()
228
+ print ("Modified files:" )
229
+ for filename in file_list :
230
+ print (f" { filename .strip ()} " )
231
+ print ("=======================================================" )
227
232
228
233
229
234
def print_tool_result (tool , returncode ):
@@ -241,9 +246,7 @@ def print_output(output, args):
241
246
# print results relative to current working directory
242
247
def cwd_relative (path ):
243
248
return "{0}: [" .format (
244
- os .path .relpath (
245
- os .path .join (ramble .paths .prefix , path .group (1 )), os .getcwd ()
246
- )
249
+ os .path .relpath (os .path .join (ramble .paths .prefix , path .group (1 )), os .getcwd ())
247
250
)
248
251
249
252
for line in output .split ("\n " ):
@@ -335,13 +338,7 @@ def run_flake8(flake8_cmd, file_list, args):
335
338
temp = tempfile .mkdtemp ()
336
339
returncode = 1
337
340
try :
338
- print ("=======================================================" )
339
- print ("flake8: running flake8 code checks on ramble." )
340
- print ()
341
- print ("Modified files:" )
342
- for filename in file_list :
343
- print (f" { filename .strip ()} " )
344
- print ("=======================================================" )
341
+ print_tool_header ("flake8" , file_list )
345
342
346
343
# run flake8 on the temporary tree, once for core, once for apps
347
344
application_file_list = [f for f in file_list if is_application (f )]
@@ -413,12 +410,27 @@ def run_flake8(flake8_cmd, file_list, args):
413
410
return returncode
414
411
415
412
413
+ @tool ("black" )
414
+ def run_black (black_cmd , file_list , args ):
415
+ print_tool_header ("black" , file_list )
416
+ black_args = ("--config" , os .path .join (ramble .paths .prefix , "pyproject.toml" ))
417
+ if not args .fix :
418
+ black_args += ("--check" , "--diff" )
419
+ black_args += tuple (file_list )
420
+
421
+ output = black_cmd (* black_args , fail_on_error = False , output = str , error = str )
422
+ returncode = black_cmd .returncode
423
+ print_output (output , args )
424
+ print_tool_result ("black" , returncode )
425
+ return returncode
426
+
427
+
416
428
def validate_toolset (arg_value ):
417
429
"""Validate --tool and --skip arguments (sets of optionally comma-separated tools)."""
418
430
tools = set ("," .join (arg_value ).split ("," )) # allow args like 'black,flake8'
419
431
for tool in tools :
420
432
if tool not in tool_names :
421
- print (f"Invaild tool: { tool } , choose from: { ', ' .join (tool_names )} " )
433
+ print (f"Invalid tool: { tool } , choose from: { ', ' .join (tool_names )} " )
422
434
return tools
423
435
424
436
@@ -427,9 +439,7 @@ def style(parser, args):
427
439
if file_list :
428
440
429
441
def prefix_relative (path ):
430
- return os .path .relpath (
431
- os .path .abspath (os .path .realpath (path )), ramble .paths .prefix
432
- )
442
+ return os .path .relpath (os .path .abspath (os .path .realpath (path )), ramble .paths .prefix )
433
443
434
444
file_list = [prefix_relative (p ) for p in file_list ]
435
445
@@ -469,9 +479,7 @@ def prefix_relative(path):
469
479
470
480
for tool_name in tools_to_run :
471
481
print (f"Running { tool_name } check" )
472
- returncode |= tools [tool_name ](
473
- which (tool_name , required = True ), file_list , args
474
- )
482
+ returncode |= tools [tool_name ](which (tool_name , required = True ), file_list , args )
475
483
476
484
if returncode != 0 :
477
485
print ("style checks found errors." )
0 commit comments