Add option for regex pattern exclusions for file paths

This commit is contained in:
Matt Martz 2015-10-05 11:47:20 -05:00 committed by John Barker
parent cf9b22103d
commit 3760ae3bfe

View file

@ -373,12 +373,28 @@ class PythonPackageValidator(Validator):
self.errors.append('Ansible module subdirectories must contain an ' self.errors.append('Ansible module subdirectories must contain an '
'__init__.py') '__init__.py')
def re_compile(value):
"""
Argparse expects things to raise TypeError, re.compile raises an re.error
exception
This function is a shorthand to convert the re.error exception to a
TypeError
"""
try:
return re.compile(value)
except re.error as e:
raise TypeError(e)
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('modules', help='Path to module or module directory') parser.add_argument('modules', help='Path to module or module directory')
parser.add_argument('-w', '--warnings', help='Show warnings', parser.add_argument('-w', '--warnings', help='Show warnings',
action='store_true') action='store_true')
parser.add_argument('--exclude', help='Regex exclusion pattern',
type=re_compile)
args = parser.parse_args() args = parser.parse_args()
args.modules = os.path.abspath(args.modules.rstrip('/')) args.modules = os.path.abspath(args.modules.rstrip('/'))
@ -387,7 +403,10 @@ def main():
# Allow testing against a single file # Allow testing against a single file
if os.path.isfile(args.modules): if os.path.isfile(args.modules):
mv = ModuleValidator(os.path.abspath(args.modules), root=args.modules) path = os.path.abspath(args.modules)
if args.exclude and args.exclude.search(path):
sys.exit(0)
mv = ModuleValidator(path, root=args.modules)
mv.validate() mv.validate()
exit.append(mv.report(args.warnings)) exit.append(mv.report(args.warnings))
sys.exit(sum(exit)) sys.exit(sum(exit))
@ -400,6 +419,8 @@ def main():
if root == args.modules and dirname in BLACKLIST_DIRS: if root == args.modules and dirname in BLACKLIST_DIRS:
continue continue
path = os.path.join(root, dirname) path = os.path.join(root, dirname)
if args.exclude and args.exclude.search(path):
continue
pv = PythonPackageValidator(os.path.abspath(path), pv = PythonPackageValidator(os.path.abspath(path),
root=args.modules) root=args.modules)
pv.validate() pv.validate()
@ -407,6 +428,8 @@ def main():
for filename in files: for filename in files:
path = os.path.join(root, filename) path = os.path.join(root, filename)
if args.exclude and args.exclude.search(path):
continue
mv = ModuleValidator(os.path.abspath(path), root=args.modules) mv = ModuleValidator(os.path.abspath(path), root=args.modules)
mv.validate() mv.validate()
exit.append(mv.report(args.warnings)) exit.append(mv.report(args.warnings))