From: Thanh Ha Date: Fri, 4 Aug 2017 17:11:51 +0000 (-0400) Subject: Use a regex pattern instead for license searching X-Git-Tag: v0.7.0~5 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F41%2F5741%2F1;p=releng%2Flftools.git 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 --- 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: