2023-04-04 23:59:12 -03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
Runs astyle over directory sub-trees known to be "astyle-clean"
|
|
|
|
|
|
|
|
AP_FLAKE8_CLEAN
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
os.environ['PYTHONUNBUFFERED'] = '1'
|
2023-08-17 19:17:54 -03:00
|
|
|
DRY_RUN_DEFAULT = False
|
2023-04-04 23:59:12 -03:00
|
|
|
|
|
|
|
|
|
|
|
class AStyleChecker(object):
|
2023-08-17 19:17:54 -03:00
|
|
|
def __init__(self, *, dry_run=DRY_RUN_DEFAULT):
|
2023-04-04 23:59:12 -03:00
|
|
|
self.retcode = 0
|
|
|
|
self.directories_to_check = [
|
|
|
|
'libraries/AP_DDS',
|
2023-09-23 16:43:11 -03:00
|
|
|
'libraries/AP_ExternalControl',
|
2024-05-12 00:45:11 -03:00
|
|
|
'libraries/AP_GSOF',
|
2023-09-23 16:43:11 -03:00
|
|
|
]
|
|
|
|
self.files_to_check = [
|
|
|
|
pathlib.Path(s) for s in [
|
2024-05-01 16:43:49 -03:00
|
|
|
'ArduCopter/AP_ExternalControl_Copter.cpp',
|
|
|
|
'ArduCopter/AP_ExternalControl_Copter.h',
|
2023-09-23 16:43:11 -03:00
|
|
|
'libraries/AP_ExternalAHRS/AP_ExternalAHRS_MicroStrain7.cpp',
|
|
|
|
'libraries/AP_ExternalAHRS/AP_ExternalAHRS_MicroStrain7.h',
|
2024-05-01 16:43:49 -03:00
|
|
|
'Rover/AP_ExternalControl_Rover.cpp',
|
|
|
|
'Rover/AP_ExternalControl_Rover.h',
|
2023-09-23 16:43:11 -03:00
|
|
|
]
|
2023-04-04 23:59:12 -03:00
|
|
|
]
|
2023-08-17 19:17:54 -03:00
|
|
|
self.dry_run = dry_run
|
2023-04-04 23:59:12 -03:00
|
|
|
|
|
|
|
def progress(self, string):
|
|
|
|
print("****** %s" % (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)
|
2023-08-17 19:17:54 -03:00
|
|
|
astyle_command = ["astyle"]
|
|
|
|
if self.dry_run:
|
|
|
|
astyle_command.append("--dry-run")
|
|
|
|
|
2023-05-02 20:56:50 -03:00
|
|
|
astyle_command.append("--options=Tools/CodeStyle/astylerc")
|
2023-04-04 23:59:12 -03:00
|
|
|
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("astyle check failed: (%s)" % (ret.stdout))
|
|
|
|
self.retcode = 1
|
|
|
|
if "Formatted" in ret.stdout:
|
2024-11-08 16:02:11 -04:00
|
|
|
self.progress("Files needing formatting found.")
|
2023-04-04 23:59:12 -03:00
|
|
|
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 all Python files for astyle cleanliness')
|
2023-08-17 19:17:54 -03:00
|
|
|
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')
|
2023-04-04 23:59:12 -03:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-08-17 19:17:54 -03:00
|
|
|
checker = AStyleChecker(dry_run=args.dry_run)
|
2023-04-04 23:59:12 -03:00
|
|
|
sys.exit(checker.run())
|