Skip to content

Commit

Permalink
Refactor task base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Jan 13, 2024
1 parent 6318dcb commit 128a12e
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 82 deletions.
76 changes: 42 additions & 34 deletions wpiformat/wpiformat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from wpiformat.lint import Lint
from wpiformat.pyformat import PyFormat
from wpiformat.stdlib import Stdlib
from wpiformat.task import Task
from wpiformat.task import BatchTask, PipelineTask, StandaloneTask, Task
from wpiformat.usingdeclaration import UsingDeclaration
from wpiformat.usingnamespacestd import UsingNamespaceStd
from wpiformat.whitespace import Whitespace
Expand Down Expand Up @@ -212,6 +212,16 @@ def run_pipeline(task_pipeline, args, files):
"""
init_args = (task_pipeline, args.verbose1, args.verbose2)

# Check tasks are all pipeline tasks
invalid_tasks = [
type(task).__name__
for task in task_pipeline
if not issubclass(type(task), PipelineTask)
]
if invalid_tasks:
print(f"error: the following pipeline tasks are invalid: {invalid_tasks}")
sys.exit(1)

with mp.Pool(args.jobs, proc_init, init_args) as pool:
# Start worker processes for task pipeline
results = pool.map(proc_pipeline, files)
Expand All @@ -220,40 +230,61 @@ def run_pipeline(task_pipeline, args, files):
sys.exit(1)


def run_standalone(task_pipeline, args, files):
"""Spawns process pool for proc_standalone().
def run_batch(task_pipeline, args, file_batches):
"""Spawns process pool for proc_batch().
Keyword arguments:
task_pipeline -- task pipeline
args -- command line arguments from argparse
files -- list of file names to process
file_batches -- list of file names to process
Calls sys.exit(1) if any task fails.
"""
init_args = (task_pipeline, args.verbose1, args.verbose2)

# Check tasks are all batch tasks
invalid_tasks = [
type(task).__name__
for task in task_pipeline
if not issubclass(type(task), BatchTask)
]
if invalid_tasks:
print(f"error: the following batch tasks are invalid: {invalid_tasks}")
sys.exit(1)

with mp.Pool(args.jobs, proc_init, init_args) as pool:
# Start worker processes for task pipeline
results = pool.map(proc_standalone, files)
# Start worker processes for batch tasks
results = pool.map(proc_batch, file_batches)

if not all(results):
sys.exit(1)


def run_batch(task_pipeline, args, file_batches):
"""Spawns process pool for proc_batch().
def run_standalone(task_pipeline, args, files):
"""Spawns process pool for proc_standalone().
Keyword arguments:
task_pipeline -- task pipeline
args -- command line arguments from argparse
file_batches -- list of file names to process
files -- list of file names to process
Calls sys.exit(1) if any task fails.
"""
init_args = (task_pipeline, args.verbose1, args.verbose2)

# Check tasks are all standalone tasks
invalid_tasks = [
type(task).__name__
for task in task_pipeline
if not issubclass(type(task), StandaloneTask)
]
if invalid_tasks:
print(f"error: the following standalone tasks are invalid: {invalid_tasks}")
sys.exit(1)

with mp.Pool(args.jobs, proc_init, init_args) as pool:
# Start worker processes for batch tasks
results = pool.map(proc_batch, file_batches)
# Start worker processes for task pipeline
results = pool.map(proc_standalone, files)

if not all(results):
sys.exit(1)
Expand Down Expand Up @@ -497,18 +528,6 @@ def main():
ClangFormat(args.clang_version),
Jni(), # Fixes clang-format formatting
]

# Check tasks are all pipeline tasks
invalid_tasks = [
type(task).__name__
for task in task_pipeline
# Pipeline tasks must override run_pipeline
if Task.run_pipeline == type(task).run_pipeline
]
if invalid_tasks:
print(f"error: the following pipeline tasks are invalid: {invalid_tasks}")
return False

run_pipeline(task_pipeline, args, files)

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

# Check tasks are all batch tasks
invalid_tasks = [
type(task).__name__
for task in task_pipeline
# Batch tasks must override run_batch
if Task.run_batch == type(task).run_batch
]
if invalid_tasks:
print(f"error: the following batch tasks are invalid: {invalid_tasks}")
return False

run_batch(task_pipeline, args, file_batches)

# ClangTidy is run last of all; it needs the actual files
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/bracecomment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import regex

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class BraceComment(Task):
class BraceComment(PipelineTask):
@staticmethod
def should_process_file(config_file, name):
return config_file.is_c_file(name) or config_file.is_cpp_file(name)
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/cidentlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import regex

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class CIdentList(Task):
class CIdentList(PipelineTask):
@staticmethod
def __print_failure(name):
print(
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/clangformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import clang_format

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class ClangFormat(Task):
class ClangFormat(PipelineTask):
def __init__(self, clang_version):
"""Constructor for ClangFormat task.
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/clangtidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import clang_tidy

from wpiformat.task import Task
from wpiformat.task import StandaloneTask


class ClangTidy(Task):
class ClangTidy(StandaloneTask):
def __init__(self, clang_version, compile_commands, extra_args):
"""Constructor for ClangTidy task.
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/cmakeformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import subprocess
import sys

from wpiformat.task import Task
from wpiformat.task import BatchTask


class CMakeFormat(Task):
class CMakeFormat(BatchTask):
@staticmethod
def should_process_file(config_file, name):
return name.endswith("CMakeLists.txt") or name.endswith(".cmake")
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/eofnewline.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""This task ensures that the file has exactly one EOF newline."""

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class EofNewline(Task):
class EofNewline(PipelineTask):
def run_pipeline(self, config_file, name, lines):
return lines.rstrip() + super().get_linesep(lines), True
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/gtestname.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import regex

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class GTestName(Task):
class GTestName(PipelineTask):
@staticmethod
def should_process_file(config_file, name):
return config_file.is_cpp_file(name)
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/includeguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import regex

from enum import Enum
from wpiformat.task import Task
from wpiformat.task import PipelineTask


class State(Enum):
Expand All @@ -15,7 +15,7 @@ class State(Enum):
DONE = 3


class IncludeGuard(Task):
class IncludeGuard(PipelineTask):
@staticmethod
def should_process_file(config_file, name):
return config_file.is_header_file(name)
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/includeorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import os
import regex

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class IncludeOrder(Task):
class IncludeOrder(PipelineTask):
def __init__(self):
super().__init__()

Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/javaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import regex

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class JavaClass(Task):
class JavaClass(PipelineTask):
@staticmethod
def should_process_file(config_file, name):
return name.endswith(".java")
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/jni.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import regex

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class Jni(Task):
class Jni(PipelineTask):
@staticmethod
def should_process_file(config_file, name):
return config_file.is_cpp_src_file(name)
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/licenseupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import sys

from wpiformat.config import Config
from wpiformat.task import Task
from wpiformat.task import PipelineTask


class LicenseUpdate(Task):
class LicenseUpdate(PipelineTask):
@staticmethod
def should_process_file(config_file, name):
license_regex = config_file.regex("licenseUpdateExclude")
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import sys

from wpiformat import cpplint
from wpiformat.task import Task
from wpiformat.task import BatchTask


class Lint(Task):
class Lint(BatchTask):
@staticmethod
def should_process_file(config_file, name):
return config_file.is_cpp_file(name)
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/pyformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import subprocess
import sys

from wpiformat.task import Task
from wpiformat.task import BatchTask


class PyFormat(Task):
class PyFormat(BatchTask):
@staticmethod
def should_process_file(config_file, name):
return name.endswith(".py")
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/wpiformat/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import regex

from wpiformat.task import Task
from wpiformat.task import PipelineTask


class Header:
Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(self, name, func_names=None, type_regexes=None, add_prefix=True):
self.type_regex = None


class Stdlib(Task):
class Stdlib(PipelineTask):
def __init__(self):
super().__init__()

Expand Down
Loading

0 comments on commit 128a12e

Please sign in to comment.