Skip to content

Commit 6c93efd

Browse files
authored
Refactor task base classes (#274)
1 parent 6318dcb commit 6c93efd

20 files changed

+102
-82
lines changed

wpiformat/wpiformat/__init__.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from wpiformat.lint import Lint
2424
from wpiformat.pyformat import PyFormat
2525
from wpiformat.stdlib import Stdlib
26-
from wpiformat.task import Task
26+
from wpiformat.task import BatchTask, PipelineTask, StandaloneTask, Task
2727
from wpiformat.usingdeclaration import UsingDeclaration
2828
from wpiformat.usingnamespacestd import UsingNamespaceStd
2929
from wpiformat.whitespace import Whitespace
@@ -212,6 +212,16 @@ def run_pipeline(task_pipeline, args, files):
212212
"""
213213
init_args = (task_pipeline, args.verbose1, args.verbose2)
214214

215+
# Check tasks are all pipeline tasks
216+
invalid_tasks = [
217+
type(task).__name__
218+
for task in task_pipeline
219+
if not issubclass(type(task), PipelineTask)
220+
]
221+
if invalid_tasks:
222+
print(f"error: the following pipeline tasks are invalid: {invalid_tasks}")
223+
sys.exit(1)
224+
215225
with mp.Pool(args.jobs, proc_init, init_args) as pool:
216226
# Start worker processes for task pipeline
217227
results = pool.map(proc_pipeline, files)
@@ -220,40 +230,61 @@ def run_pipeline(task_pipeline, args, files):
220230
sys.exit(1)
221231

222232

223-
def run_standalone(task_pipeline, args, files):
224-
"""Spawns process pool for proc_standalone().
233+
def run_batch(task_pipeline, args, file_batches):
234+
"""Spawns process pool for proc_batch().
225235
226236
Keyword arguments:
227237
task_pipeline -- task pipeline
228238
args -- command line arguments from argparse
229-
files -- list of file names to process
239+
file_batches -- list of file names to process
230240
231241
Calls sys.exit(1) if any task fails.
232242
"""
233243
init_args = (task_pipeline, args.verbose1, args.verbose2)
234244

245+
# Check tasks are all batch tasks
246+
invalid_tasks = [
247+
type(task).__name__
248+
for task in task_pipeline
249+
if not issubclass(type(task), BatchTask)
250+
]
251+
if invalid_tasks:
252+
print(f"error: the following batch tasks are invalid: {invalid_tasks}")
253+
sys.exit(1)
254+
235255
with mp.Pool(args.jobs, proc_init, init_args) as pool:
236-
# Start worker processes for task pipeline
237-
results = pool.map(proc_standalone, files)
256+
# Start worker processes for batch tasks
257+
results = pool.map(proc_batch, file_batches)
238258

239259
if not all(results):
240260
sys.exit(1)
241261

242262

243-
def run_batch(task_pipeline, args, file_batches):
244-
"""Spawns process pool for proc_batch().
263+
def run_standalone(task_pipeline, args, files):
264+
"""Spawns process pool for proc_standalone().
245265
246266
Keyword arguments:
247267
task_pipeline -- task pipeline
248268
args -- command line arguments from argparse
249-
file_batches -- list of file names to process
269+
files -- list of file names to process
250270
271+
Calls sys.exit(1) if any task fails.
251272
"""
252273
init_args = (task_pipeline, args.verbose1, args.verbose2)
253274

275+
# Check tasks are all standalone tasks
276+
invalid_tasks = [
277+
type(task).__name__
278+
for task in task_pipeline
279+
if not issubclass(type(task), StandaloneTask)
280+
]
281+
if invalid_tasks:
282+
print(f"error: the following standalone tasks are invalid: {invalid_tasks}")
283+
sys.exit(1)
284+
254285
with mp.Pool(args.jobs, proc_init, init_args) as pool:
255-
# Start worker processes for batch tasks
256-
results = pool.map(proc_batch, file_batches)
286+
# Start worker processes for standalone tasks
287+
results = pool.map(proc_standalone, files)
257288

258289
if not all(results):
259290
sys.exit(1)
@@ -497,18 +528,6 @@ def main():
497528
ClangFormat(args.clang_version),
498529
Jni(), # Fixes clang-format formatting
499530
]
500-
501-
# Check tasks are all pipeline tasks
502-
invalid_tasks = [
503-
type(task).__name__
504-
for task in task_pipeline
505-
# Pipeline tasks must override run_pipeline
506-
if Task.run_pipeline == type(task).run_pipeline
507-
]
508-
if invalid_tasks:
509-
print(f"error: the following pipeline tasks are invalid: {invalid_tasks}")
510-
return False
511-
512531
run_pipeline(task_pipeline, args, files)
513532

514533
# Lint is run last since previous tasks can affect its output.
@@ -519,17 +538,6 @@ def main():
519538
else:
520539
task_pipeline = [CMakeFormat(), PyFormat(), Lint()]
521540

522-
# Check tasks are all batch tasks
523-
invalid_tasks = [
524-
type(task).__name__
525-
for task in task_pipeline
526-
# Batch tasks must override run_batch
527-
if Task.run_batch == type(task).run_batch
528-
]
529-
if invalid_tasks:
530-
print(f"error: the following batch tasks are invalid: {invalid_tasks}")
531-
return False
532-
533541
run_batch(task_pipeline, args, file_batches)
534542

535543
# ClangTidy is run last of all; it needs the actual files

wpiformat/wpiformat/bracecomment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import regex
66

7-
from wpiformat.task import Task
7+
from wpiformat.task import PipelineTask
88

99

10-
class BraceComment(Task):
10+
class BraceComment(PipelineTask):
1111
@staticmethod
1212
def should_process_file(config_file, name):
1313
return config_file.is_c_file(name) or config_file.is_cpp_file(name)

wpiformat/wpiformat/cidentlist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import regex
44

5-
from wpiformat.task import Task
5+
from wpiformat.task import PipelineTask
66

77

8-
class CIdentList(Task):
8+
class CIdentList(PipelineTask):
99
@staticmethod
1010
def __print_failure(name):
1111
print(

wpiformat/wpiformat/clangformat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import clang_format
77

8-
from wpiformat.task import Task
8+
from wpiformat.task import PipelineTask
99

1010

11-
class ClangFormat(Task):
11+
class ClangFormat(PipelineTask):
1212
def __init__(self, clang_version):
1313
"""Constructor for ClangFormat task.
1414

wpiformat/wpiformat/clangtidy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import clang_tidy
77

8-
from wpiformat.task import Task
8+
from wpiformat.task import StandaloneTask
99

1010

11-
class ClangTidy(Task):
11+
class ClangTidy(StandaloneTask):
1212
def __init__(self, clang_version, compile_commands, extra_args):
1313
"""Constructor for ClangTidy task.
1414

wpiformat/wpiformat/cmakeformat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import subprocess
44
import sys
55

6-
from wpiformat.task import Task
6+
from wpiformat.task import BatchTask
77

88

9-
class CMakeFormat(Task):
9+
class CMakeFormat(BatchTask):
1010
@staticmethod
1111
def should_process_file(config_file, name):
1212
return name.endswith("CMakeLists.txt") or name.endswith(".cmake")

wpiformat/wpiformat/eofnewline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""This task ensures that the file has exactly one EOF newline."""
22

3-
from wpiformat.task import Task
3+
from wpiformat.task import PipelineTask
44

55

6-
class EofNewline(Task):
6+
class EofNewline(PipelineTask):
77
def run_pipeline(self, config_file, name, lines):
88
return lines.rstrip() + super().get_linesep(lines), True

wpiformat/wpiformat/gtestname.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import regex
66

7-
from wpiformat.task import Task
7+
from wpiformat.task import PipelineTask
88

99

10-
class GTestName(Task):
10+
class GTestName(PipelineTask):
1111
@staticmethod
1212
def should_process_file(config_file, name):
1313
return config_file.is_cpp_file(name)

wpiformat/wpiformat/includeguard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import regex
77

88
from enum import Enum
9-
from wpiformat.task import Task
9+
from wpiformat.task import PipelineTask
1010

1111

1212
class State(Enum):
@@ -15,7 +15,7 @@ class State(Enum):
1515
DONE = 3
1616

1717

18-
class IncludeGuard(Task):
18+
class IncludeGuard(PipelineTask):
1919
@staticmethod
2020
def should_process_file(config_file, name):
2121
return config_file.is_header_file(name)

wpiformat/wpiformat/includeorder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import os
44
import regex
55

6-
from wpiformat.task import Task
6+
from wpiformat.task import PipelineTask
77

88

9-
class IncludeOrder(Task):
9+
class IncludeOrder(PipelineTask):
1010
def __init__(self):
1111
super().__init__()
1212

0 commit comments

Comments
 (0)