@@ -40,38 +40,59 @@ def has_existing_header(file_content, header):
4040
4141def has_copyright (file_content ):
4242 lines = file_content .splitlines ()
43- header = "\n " .join (lines [:20 ].lower () )
43+ header = "\n " .join (lines [:20 ]) .lower ()
4444 return "copyright" in header or "spdx-license" in header
4545
4646
4747def has_hillbot_line (file_content ):
4848 lines = file_content .splitlines ()
49- header = "\n " .join (lines [:5 ].lower () )
49+ header = "\n " .join (lines [:5 ]) .lower ()
5050 return "hillbot inc" in header
5151
5252
53+ def split_skip (content : str , skip ):
54+ skip = [re .compile (s ) for s in skip ]
55+ lines = content .splitlines ()
56+ i = 0
57+ for i , l in enumerate (lines ):
58+ if any (s .match (l ) for s in skip ):
59+ continue
60+ break
61+ head = lines [:i ]
62+ tail = lines [i :]
63+ head = "\n " .join (head )
64+ tail = "\n " .join (tail )
65+ if head != "" :
66+ head += "\n "
67+ return head , tail
68+
69+
5370# Update or add the header to the file
5471def update_file_header (file_path , header , comment_style ):
5572 with open (file_path , "r" ) as file :
5673 content = file .read ()
74+ content_header , content = split_skip (content , comment_style ["skip" ])
5775
5876 # If the file already has a header, check if it needs to be replaced
5977 if has_existing_header (content , header ):
6078 # If the header is already up-to-date, return
6179 if content .startswith (header ):
80+ print ("Skip:" , file_path )
6281 return
6382 # Otherwise, remove the old header
6483 content = re .sub (
6584 r"^{}.*?\n" .format (re .escape (comment_style .get ("start" , "" ))), "" , content , flags = re .DOTALL
6685 )
6786
6887 if has_copyright (content ):
88+ print ("Skip 3rd party: " , file_path )
6989 # If there is copyright added by someone else, return
7090 return
7191
7292 # Add the new header
7393 with open (file_path , "w" ) as file :
74- file .write (header + content )
94+ print ("Apply license: " , file_path )
95+ file .write (content_header + header + content )
7596
7697
7798# Remove the header from the file
@@ -110,12 +131,18 @@ def process_directory(directory, header_content, comment_styles, delete_mode=Fal
110131 else :
111132 header = generate_header (comment_style , header_content )
112133 update_file_header (file_path , header , comment_style )
134+ else :
135+ print ("Skip unknown extension: " , file_path )
113136
114137
115138def main (config_file , delete_mode = False ):
116139 config = load_config (config_file )
117140 header_content = config .get ("header_content" , [])
118- comment_styles = config .get ("comment_styles" , {})
141+ comment_styles = config .get ("comment_styles" , {}) # lang -> style
142+ comment_styles = dict (
143+ (e , comment_styles [lang ]) for lang , exts in config .get ("language" , {}).items () for e in exts
144+ )
145+
119146 target_files = config .get ("target_files" , [])
120147 target_directories = config .get ("target_directories" , [])
121148
0 commit comments