1- # Copyright (c) 2019-2021 , NVIDIA CORPORATION.
1+ # Copyright (c) 2020-2022 , NVIDIA CORPORATION.
22#
33# Licensed under the Apache License, Version 2.0 (the "License");
44# you may not use this file except in compliance with the License.
1818import argparse
1919import io
2020import os
21- import git_helpers
21+ import sys
22+
23+ SCRIPT_DIR = os .path .dirname (os .path .realpath (os .path .expanduser (__file__ )))
24+
25+ # Add the scripts dir for gitutils
26+ sys .path .append (os .path .normpath (os .path .join (SCRIPT_DIR ,
27+ "../../cpp/scripts" )))
28+
29+ # Now import gitutils. Ignore flake8 error here since there is no other way to
30+ # set up imports
31+ import gitutils # noqa: E402
2232
2333FilesToCheck = [
2434 re .compile (r"[.](cmake|cpp|cu|cuh|h|hpp|sh|pxd|py|pyx)$" ),
2838 re .compile (r"[.]flake8[.]cython$" ),
2939 re .compile (r"meta[.]yaml$" )
3040]
41+ ExemptFiles = []
3142
3243# this will break starting at year 10000, which is probably OK :)
33- CheckSimple = re .compile (r"Copyright \(c\) (\d{4}), NVIDIA CORPORATION" )
44+ CheckSimple = re .compile (
45+ r"Copyright *(?:\(c\))? *(\d{4}),? *NVIDIA C(?:ORPORATION|orporation)" )
3446CheckDouble = re .compile (
35- r"Copyright \(c\) (\d{4})-(\d{4}), NVIDIA CORPORATION" )
47+ r"Copyright *(?:\(c\))? *(\d{4})-(\d{4}),? *NVIDIA C(?:ORPORATION|orporation)" # noqa: E501
48+ )
3649
3750
3851def checkThisFile (f ):
3952 # This check covers things like symlinks which point to files that DNE
40- if not (os .path .exists (f )):
53+ if not (os .path .exists (f )):
4154 return False
42- if git_helpers and git_helpers .isFileEmpty (f ):
43- return False
44- # Special case for versioneer.py - it uses a separate copyright.
45- if os .path .basename (f ) == "versioneer.py" :
55+ if gitutils and gitutils .isFileEmpty (f ):
4656 return False
57+ for exempt in ExemptFiles :
58+ if exempt .search (f ):
59+ return False
4760 for checker in FilesToCheck :
4861 if checker .search (f ):
4962 return True
@@ -87,12 +100,22 @@ def checkCopyright(f, update_current_year):
87100 continue
88101 crFound = True
89102 if start > end :
90- e = [f , lineNum , "First year after second year in the copyright "
91- "header (manual fix required)" , None ]
103+ e = [
104+ f ,
105+ lineNum ,
106+ "First year after second year in the copyright "
107+ "header (manual fix required)" ,
108+ None
109+ ]
92110 errs .append (e )
93111 if thisYear < start or thisYear > end :
94- e = [f , lineNum , "Current year not included in the "
95- "copyright header" , None ]
112+ e = [
113+ f ,
114+ lineNum ,
115+ "Current year not included in the "
116+ "copyright header" ,
117+ None
118+ ]
96119 if thisYear < start :
97120 e [- 1 ] = replaceCurrentYear (line , thisYear , end )
98121 if thisYear > end :
@@ -103,8 +126,13 @@ def checkCopyright(f, update_current_year):
103126 fp .close ()
104127 # copyright header itself not found
105128 if not crFound :
106- e = [f , 0 , "Copyright header missing or formatted incorrectly "
107- "(manual fix required)" , None ]
129+ e = [
130+ f ,
131+ 0 ,
132+ "Copyright header missing or formatted incorrectly "
133+ "(manual fix required)" ,
134+ None
135+ ]
108136 errs .append (e )
109137 # even if the year matches a copyright header, make the check pass
110138 if yearMatched :
@@ -125,7 +153,6 @@ def checkCopyright(f, update_current_year):
125153 return errs
126154
127155
128-
129156def getAllFilesUnderDir (root , pathFilter = None ):
130157 retList = []
131158 for (dirpath , dirnames , filenames ) in os .walk (root ):
@@ -143,25 +170,47 @@ def checkCopyright_main():
143170 it compares between branches "$PR_TARGET_BRANCH" and "current-pr-branch"
144171 """
145172 retVal = 0
173+ global ExemptFiles
146174
147175 argparser = argparse .ArgumentParser (
148- description = "Checks for a consistent copyright header" )
149- argparser .add_argument ("--update-current-year" , dest = 'update_current_year' ,
150- action = "store_true" , required = False , help = "If set, "
151- "update the current year if a header is already "
152- "present and well formatted." )
153- argparser .add_argument ("--git-modified-only" , dest = 'git_modified_only' ,
154- action = "store_true" , required = False , help = "If set, "
155- "only files seen as modified by git will be "
156- "processed." )
176+ "Checks for a consistent copyright header in git's modified files" )
177+ argparser .add_argument ("--update-current-year" ,
178+ dest = 'update_current_year' ,
179+ action = "store_true" ,
180+ required = False ,
181+ help = "If set, "
182+ "update the current year if a header "
183+ "is already present and well formatted." )
184+ argparser .add_argument ("--git-modified-only" ,
185+ dest = 'git_modified_only' ,
186+ action = "store_true" ,
187+ required = False ,
188+ help = "If set, "
189+ "only files seen as modified by git will be "
190+ "processed." )
191+ argparser .add_argument ("--exclude" ,
192+ dest = 'exclude' ,
193+ action = "append" ,
194+ required = False ,
195+ default = ["python/cuml/_thirdparty/" ],
196+ help = ("Exclude the paths specified (regexp). "
197+ "Can be specified multiple times." ))
157198
158199 (args , dirs ) = argparser .parse_known_args ()
200+ try :
201+ ExemptFiles = ExemptFiles + [pathName for pathName in args .exclude ]
202+ ExemptFiles = [re .compile (file ) for file in ExemptFiles ]
203+ except re .error as reException :
204+ print ("Regular expression error:" )
205+ print (reException )
206+ return 1
207+
159208 if args .git_modified_only :
160- files = git_helpers .modifiedFiles (pathFilter = checkThisFile )
209+ files = gitutils .modifiedFiles (pathFilter = checkThisFile )
161210 else :
162211 files = []
163212 for d in [os .path .abspath (d ) for d in dirs ]:
164- if not (os .path .isdir (d )):
213+ if not (os .path .isdir (d )):
165214 raise ValueError (f"{ d } is not a directory." )
166215 files += getAllFilesUnderDir (d , pathFilter = checkThisFile )
167216
@@ -178,8 +227,9 @@ def checkCopyright_main():
178227 path_parts = os .path .abspath (__file__ ).split (os .sep )
179228 file_from_repo = os .sep .join (path_parts [path_parts .index ("ci" ):])
180229 if n_fixable > 0 :
181- print ("You can run {} --update-current-year to fix {} of these "
182- "errors.\n " .format (file_from_repo , n_fixable ))
230+ print (("You can run `python {} --git-modified-only "
231+ "--update-current-year` to fix {} of these "
232+ "errors.\n " ).format (file_from_repo , n_fixable ))
183233 retVal = 1
184234 else :
185235 print ("Copyright check passed" )
0 commit comments