style: Add options to select checkers and apply fixes

Add an option, --checker/-c, to style.py that selects individual style
checkers to apply. When this option isn't specified, the script
defaults to all available style checkers. The option may be specified
multiple times to run multiple style checkers.

The option, --fix/-f, can be specified to automatically fix style
violations.

Change-Id: Id7597fba6b65cecfa17a88b1c87c8a4c8315af59
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-by: Andreas Hansson <andreas.hansson@arm.com>
This commit is contained in:
Andreas Sandberg 2016-11-25 10:33:15 +00:00
parent ac29b6c6fc
commit faaf2d396f

View file

@ -41,21 +41,28 @@ import os
import sys import sys
from style.file_types import lang_type from style.file_types import lang_type
from style.verifiers import all_verifiers import style.verifiers
from style.region import all_regions from style.region import all_regions
from style.style import StdioUI from style.style import StdioUI
from style import repo from style import repo
def verify(filename, regions=all_regions, verbose=False): verifier_names = dict([
(c.__name__, c) for c in style.verifiers.all_verifiers ])
def verify(filename, regions=all_regions, verbose=False, verifiers=None,
auto_fix=False):
ui = StdioUI() ui = StdioUI()
opts = {} opts = {
"fix_all" : auto_fix,
}
base = os.path.join(os.path.dirname(__file__), "..") base = os.path.join(os.path.dirname(__file__), "..")
verifiers = [ v(ui, opts, base=base) for v in all_verifiers ] if verifiers is None:
verifiers = style.verifiers.all_verifiers
if verbose: if verbose:
print "Verifying %s[%s]..." % (filename, regions) print "Verifying %s[%s]..." % (filename, regions)
for verifier in verifiers: for verifier in [ v(ui, opts, base=base) for v in verifiers ]:
if verbose: if verbose:
print "Applying %s (%s)" % ( print "Applying %s (%s)" % (
verifier.test_name, verifier.__class__.__name__) verifier.test_name, verifier.__class__.__name__)
@ -95,6 +102,9 @@ if __name__ == '__main__':
parser.add_argument("--verbose", "-v", action="count", parser.add_argument("--verbose", "-v", action="count",
help="Produce verbose output") help="Produce verbose output")
parser.add_argument("--fix", "-f", action="store_true",
help="Automatically fix style violations.")
parser.add_argument("--modifications", "-m", action="store_true", parser.add_argument("--modifications", "-m", action="store_true",
help="""Apply the style checker to modified regions help="""Apply the style checker to modified regions
instead of whole files""") instead of whole files""")
@ -102,6 +112,11 @@ if __name__ == '__main__':
parser.add_argument("--repo-type", choices=repo_types, default="auto", parser.add_argument("--repo-type", choices=repo_types, default="auto",
help="Repository type to use to detect changes") help="Repository type to use to detect changes")
parser.add_argument("--checker", "-c", choices=verifier_names, default=[],
action="append",
help="""Style checkers to run. Can be specified
multiple times.""")
parser.add_argument("files", metavar="FILE", nargs="*", parser.add_argument("files", metavar="FILE", nargs="*",
type=str, type=str,
help="Source file(s) to inspect") help="Source file(s) to inspect")
@ -110,6 +125,9 @@ if __name__ == '__main__':
repo = repo_types[args.repo_type]() repo = repo_types[args.repo_type]()
verifiers = [ verifier_names[name] for name in args.checker ] \
if args.checker else None
files = args.files files = args.files
if not files and repo: if not files and repo:
added, modified = repo.staged_files() added, modified = repo.staged_files()
@ -122,5 +140,7 @@ if __name__ == '__main__':
regions = all_regions regions = all_regions
if not verify(filename, regions=regions, if not verify(filename, regions=regions,
verbose=args.verbose): verbose=args.verbose,
verifiers=verifiers,
auto_fix=args.fix):
sys.exit(1) sys.exit(1)