Files
ardupilot/Tools/scripts/run_astyle.py
T

88 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Runs astyle over directory sub-trees known to be "astyle-clean"
AP_FLAKE8_CLEAN
"""
import argparse
import contextlib
import os
import pathlib
import subprocess
import sys
os.environ['PYTHONUNBUFFERED'] = '1'
DRY_RUN_DEFAULT = False
class AStyleChecker(object):
def __init__(self, *, dry_run=DRY_RUN_DEFAULT):
self.retcode = 0
self.directories_to_check = [
'libraries/AP_DDS',
'libraries/AP_ExternalControl',
'libraries/AP_GSOF',
]
self.files_to_check = [
pathlib.Path(s) for s in [
'ArduCopter/AP_ExternalControl_Copter.cpp',
'ArduCopter/AP_ExternalControl_Copter.h',
'libraries/AP_ExternalAHRS/AP_ExternalAHRS_MicroStrain7.cpp',
'libraries/AP_ExternalAHRS/AP_ExternalAHRS_MicroStrain7.h',
'Rover/AP_ExternalControl_Rover.cpp',
'Rover/AP_ExternalControl_Rover.h',
]
]
self.dry_run = dry_run
def progress(self, string):
print(f"****** {string}")
def check(self):
'''run astyle on all files in self.files_to_check'''
# for path in self.files_to_check:
# self.progress("Checking (%s)" % path)
astyle_command = ["astyle"]
# Show the astyle version suppressing all exceptions
with contextlib.suppress():
_ = subprocess.run(astyle_command + ["--version"])
if self.dry_run:
astyle_command.append("--dry-run")
astyle_command.append("--options=Tools/CodeStyle/astylerc")
astyle_command.extend(self.files_to_check)
ret = subprocess.run(
astyle_command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True
)
if ret.returncode != 0:
self.progress(f"astyle check failed: ({ret.stdout})")
self.retcode = 1
if "Formatted" in ret.stdout:
self.progress("Files needing formatting found.")
print(ret.stdout)
self.retcode = 1
def run(self):
for d in self.directories_to_check:
self.files_to_check.extend(list(pathlib.Path(d).glob("*")))
self.files_to_check = list(filter(lambda x : x.suffix in [".c", ".h", ".cpp"], self.files_to_check))
self.check()
return self.retcode
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Check C and C++ files for astyle cleanliness')
parser.add_argument('--dry-run',
action='store_true',
default=DRY_RUN_DEFAULT,
help='Perform a trial run with no changes made to check for formatting')
args = parser.parse_args()
checker = AStyleChecker(dry_run=args.dry_run)
sys.exit(checker.run())