Use a regex pattern instead for license searching 41/5741/1
authorThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 4 Aug 2017 17:11:51 +0000 (13:11 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 4 Aug 2017 17:11:53 +0000 (13:11 -0400)
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 <thanh.ha@linuxfoundation.org>
lftools/cli/license.py
lftools/license.py

index 4c4d760..c00db66 100644 (file)
@@ -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)
index 93cfdaa..a4ec135 100644 (file)
@@ -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: