From 1f86e0514e478257abf25597b1534ab2be919202 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Fri, 4 Aug 2017 13:11:51 -0400 Subject: [PATCH] Use a regex pattern instead for license searching This allows us to search for files for example in shell/ where there are no file extensions. Change-Id: I5634b233d9feb0d2dc3421b0c1a9c5280faf82f1 Signed-off-by: Thanh Ha --- lftools/cli/license.py | 10 ++++++---- lftools/license.py | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lftools/cli/license.py b/lftools/cli/license.py index 4c4d7608..c00db665 100644 --- a/lftools/cli/license.py +++ b/lftools/cli/license.py @@ -46,20 +46,22 @@ def check(ctx, license, source): @click.command(name='check-dir') @click.argument('directory') -@click.option('-e', '--extension', default='py', - help='File extension to search for.') @click.option('-l', '--license', default='license-header.txt', help='License header file to compare against.') +@click.option('-r', '--regex', default='.+\.py$', + help='File regex pattern to match on when searching.') @click.pass_context -def check_directory(ctx, license, directory, extension): +def check_directory(ctx, license, directory, regex): """Check directory for files missing license headers. + Uses a regex pattern to find files to check for approved license headers. + Does not care if about line formatting of the license as long as all of the text is there and in the correct order. Note: This code only supports '#' comments for license headers. """ - check_license_directory(license, directory, extension) + check_license_directory(license, directory, regex) license.add_command(check) diff --git a/lftools/license.py b/lftools/license.py index 93cfdaa4..a4ec135b 100644 --- a/lftools/license.py +++ b/lftools/license.py @@ -58,14 +58,14 @@ def check_license(license_file, code_file): return 0 -def check_license_directory(license_file, directory, extension="py"): +def check_license_directory(license_file, directory, regex=".+\.py$"): """Search a directory for files and calls check_license().""" missing_license = False for root, dirs, files in os.walk(directory): - for file in files: - if file.endswith(".{}".format(extension)): - if check_license(license_file, os.path.join(root, file)): + for f in files: + if re.search(regex, f): + if check_license(license_file, os.path.join(root, f)): missing_license = True if missing_license: -- 2.16.6