1- ################################################################################
2- ##
3- ## CAN with Flexible Data-Rate IP Core
4- ##
1+ ################################################################################
2+ ##
3+ ## CAN with Flexible Data-Rate IP Core
4+ ##
55## Copyright (C) 2017 Ondrej Ille <[email protected] > 6- ##
6+ ##
77## Script for updating license in the header source codes of CAN FD IP Core.
88## Supports following file extensions: ".vhd" , ".tcl" , ".h" , ".c", ".cpp"
9- ##
9+ ##
1010## Arguments:
1111## lic_path - File with license which should be placed to header of the
1212## all source code files.
13- ## sup_files - Supported file endings which should have the license
13+ ## sup_files - Supported file endings which should have the license
1414## updated.
1515## subfolders - Which sub-folders should be updated
1616##
1717## Example of usage from IDLE shell
18- ## python ./license_updater.py 'myPrettyLicense.txt' ['.c','.vhd']
18+ ## python ./license_updater.py 'myPrettyLicense.txt' ['.c','.vhd']
1919## ['src','test']
2020##
2121## Revision history:
@@ -71,14 +71,14 @@ def print_help():
7171
7272
7373def write_license (lic_text , comment_char , file ):
74-
74+
7575 line_length = 80
76-
76+
7777 # Write the initial line (TCL, VHDL)
7878 if (comment_char == "-" or comment_char == "#" ):
7979 for i in range (0 , line_length ):
8080 file .write (comment_char )
81-
81+
8282 # Write the initial line (C)
8383 elif (comment_char == "*" ):
8484
@@ -91,10 +91,10 @@ def write_license(lic_text, comment_char, file):
9191 file .write (comment_char )
9292 else :
9393 print ("Unsuported Comment character" )
94-
94+
9595 file .write ("\n " )
96-
97- # Write rest of the lines
96+
97+ # Write rest of the lines
9898 buf = io .StringIO (lic_text )
9999 i = 0
100100 while True :
@@ -104,18 +104,20 @@ def write_license(lic_text, comment_char, file):
104104 lic_line = buf .readline ()
105105 if (not (str .find ("<licend1234>" , lic_line ) == - 1 )):
106106 break
107-
107+
108108 # Write Begining of the line
109109 if (comment_char == "-" or comment_char == "#" ):
110- file .write (comment_char + comment_char + " " )
110+ file .write (comment_char + comment_char )
111+ if (len (lic_line .strip ()) > 0 ):
112+ file .write (" " )
111113 elif (comment_char == "*" ):
112114 file .write (" " + comment_char )
113115 if (len (lic_line .strip ()) > 0 ):
114- file .write (" " )
115-
116+ file .write (" " )
117+
116118 # Write Rest of the line
117119 file .write (lic_line )
118-
120+
119121 # Write the final line
120122 if (comment_char == "-" or comment_char == "#" ):
121123 for i in range (0 , line_length ):
@@ -128,10 +130,10 @@ def write_license(lic_text, comment_char, file):
128130 file .write (comment_char )
129131 else :
130132 for i in range (0 , line_length - 1 ):
131- file .write (comment_char )
133+ file .write (comment_char )
132134
133135 file .write ("/" )
134-
136+
135137 file .write ("\n " )
136138
137139
@@ -141,78 +143,78 @@ def write_license(lic_text, comment_char, file):
141143def write_source (source_file , dest_file , comment_sign ):
142144 ## Read the first file line because this is always commented
143145 source_file .readline ()
144-
146+
145147 ## Read the license lines
146148 ## First or second line of the source code is
147149 ## the "comment character"
148150 line = source_file .readline ()
149- while (line .startswith (comment_sign ) or line [1 ]== comment_sign ):
151+ while (line .startswith (comment_sign ) or line [1 ]== comment_sign ):
150152 line = source_file .readline ()
151153 if (len (line )== 1 ):
152154 break
153-
155+
154156 dest_file .write ("\n " )
155-
157+
156158 ## Now read rest of the source code and copy to dest
157159 while True :
158160 char = source_file .read ()
159161 if (char == "" ):
160162 break
161163 dest_file .write (char )
162-
163-
164-
165-
164+
165+
166+
167+
166168
167169################################################################################
168170## Process the file and change the license header, if the file type is in the
169171## list of supported file extensions.
170172################################################################################
171173def process_file (filename ):
172-
174+
173175 global sup_files
174176 file_ext_match = False
175177 ext_type = ""
176178 comment_sign = ""
177-
179+
178180 for ext in sup_files :
179181 if (filename .endswith (ext )):
180182 file_ext_match = True
181183 ext_type = ext
182-
184+
183185 if (file_ext_match == False ):
184186 return
185-
187+
186188 print ("Processing file: " + filename )
187-
189+
188190 ## Check the comment sign based on file type
189191 if ((ext_type == ".c" ) or (ext_type == ".cpp" )):
190192 comment_sign = "*"
191- elif (ext_type == ".h" ):
193+ elif (ext_type == ".h" ):
192194 comment_sign = "*"
193- elif (ext_type == ".tcl" ):
195+ elif (ext_type == ".tcl" ):
194196 comment_sign = "#"
195- elif (ext_type == ".vhd" ):
197+ elif (ext_type == ".vhd" ):
196198 comment_sign = "-"
197199 else :
198200 comment_sign = "-"
199201
200202 ## Read the file content
201203 file = open (filename ,"r" )
202204 temp_file = open ("temp.txt" ,"wt" )
203-
205+
204206 ## Write the new license to the temp file
205207 write_license (lic_text , comment_sign , temp_file )
206-
208+
207209 ## Write rest of the file after license update
208210 write_source (file , temp_file , comment_sign )
209211 temp_file .close ()
210212 file .close ()
211-
213+
212214 ## Replace the original file and erase the temp file
213215 os .remove (filename )
214216 os .rename ("temp.txt" , filename )
215-
217+
216218
217219################################################################################
218220## Parse the command line arguments and fill according global variables
@@ -223,9 +225,9 @@ def parse_args():
223225 global sub_folders
224226 global lic_path
225227 global src_path
226-
227- src_path = os .path .dirname (os .path .abspath (__file__ ))
228-
228+
229+ src_path = os .path .dirname (os .path .abspath (__file__ ))
230+
229231 lic_path = sys .argv [1 ]
230232 sup_files = sys .argv [2 ]
231233 sup_files = sup_files .replace ("[" ,"" )
@@ -242,9 +244,9 @@ def parse_args():
242244def load_license (lic_path ):
243245 global lic_text
244246 global src_path
245-
246- src_path = os .path .dirname (os .path .abspath (__file__ ))
247-
247+
248+ src_path = os .path .dirname (os .path .abspath (__file__ ))
249+
248250 lic_full_path = os .path .join (src_path , lic_path )
249251 print (lic_full_path )
250252 if (os .path .isfile (lic_full_path )):
@@ -267,54 +269,54 @@ def load_license(lic_path):
267269def iterate_dir (dir_path ):
268270
269271 print ("Processing directory: " + dir_path )
270-
272+
271273 ## Process files and directories in this directory
272274 sub_dirs = os .listdir (dir_path )
273275 for fold in sub_dirs :
274276 full_dir = os .path .join (dir_path , fold )
275-
277+
276278 ## Execute recursively on directories
277279 if (os .path .isdir (full_dir )):
278280 iterate_dir (full_dir )
279-
281+
280282 ## Process the files
281283 if (os .path .isfile (full_dir )):
282284 process_file (full_dir )
283-
285+
284286
285287################################################################################
286288## Main function which executes the license replacement
287289################################################################################
288290def main ():
289-
291+
290292 global sup_files
291293 global sub_folders
292294 global lic_text
293295 global lic_path
294296 global src_path
295297 print ("" )
296-
298+
297299 ## Check valid input arguments
298- arg_count = 4 ## 3 arguments + script itself
300+ arg_count = 4 ## 3 arguments + script itself
299301 if (len (sys .argv ) != arg_count ):
300302 print (" Incorrect amount of arguments!" )
301303 print_help ()
302304 print (" Exiting!" )
303305 sys .exit (1 )
304-
306+
305307 ## Parse Command line arguments
306308 parse_args ()
307-
309+
308310 ## Check license existance and load it
309311 load_license (lic_path )
310-
312+
311313 #Check file extensions
312314 for ext in sup_files :
313315 if (not ext .startswith ("." )):
314316 print ("Invalid file extension '" + ext + "' File extenstions should " \
315317 "start with '.'" )
316318 sys .exit (1 )
317-
319+
318320 #Replace the license in the subfolders
319321 for dir in sub_folders :
320322 dir_path = os .path .join (src_path ,'..' )
@@ -323,6 +325,6 @@ def main():
323325 iterate_dir (dir_path )
324326 else :
325327 print (dir_path + " directory does not exist" )
326-
328+
327329if __name__ == "__main__" :
328330 main ()
0 commit comments