3939 '"' : """ ,
4040 "'" : "'" ,
4141 ">" : ">" ,
42- "<" : "<" ,
43- }
42+ "<" : "<" }
43+
4444
4545def hexify (s ):
46- return ("%02x" * len (s )) % tuple (map (ord , s ))
46+ return ("%02x" * len (s )) % tuple (map (ord , s ))
47+
4748
4849def md5sum (filename ):
4950 """Calculate an MD5 of the file given by FILENAME,
@@ -63,9 +64,10 @@ def md5sum(filename):
6364
6465def html_escape (text ):
6566 """Produce entities within text."""
66- return "" .join (HTML_ESCAPE_TABLE .get (c ,c ) for c in text )
67+ return "" .join (HTML_ESCAPE_TABLE .get (c , c ) for c in text )
68+
6769
68- HTML_OUTPUT_CSS = """
70+ HTML_OUTPUT_CSS = """
6971<style type="text/css">
7072body { padding: 0; font-family: sans-serif; }
7173.same-license { background-color: #eeeeee; border-top: 20px solid white; padding: 10px; }
@@ -74,6 +76,7 @@ def html_escape(text):
7476</style>
7577"""
7678
79+
7780def combine_notice_files_html (file_hash , input_dirs , output_filename ):
7881 """Combine notice files in FILE_HASH and output a HTML version to OUTPUT_FILENAME."""
7982
@@ -85,68 +88,70 @@ def combine_notice_files_html(file_hash, input_dirs, output_filename):
8588 id_count = 0
8689 for value in file_hash :
8790 for filename in value :
88- id_table [filename ] = id_count
91+ id_table [filename ] = id_count
8992 id_count += 1
9093
9194 # Open the output file, and output the header pieces
9295 output_file = open (output_filename , "wb" )
9396
94- print >> output_file , "<html><head>"
95- print >> output_file , HTML_OUTPUT_CSS
96- print >> output_file , '</head><body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">'
97+ print ( "<html><head>" , file = output_file )
98+ print ( HTML_OUTPUT_CSS , file = output_file )
99+ print ( '</head><body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">' , file = output_file )
97100
98101 # Output our table of contents
99- print >> output_file , '<div class="toc">'
100- print >> output_file , "<ul>"
102+ print ( '<div class="toc">' , file = output_file )
103+ print ( "<ul>" , file = output_file )
101104
102105 # Flatten the list of lists into a single list of filenames
103106 sorted_filenames = sorted (itertools .chain .from_iterable (file_hash ))
104107
105108 # Print out a nice table of contents
106109 for filename in sorted_filenames :
107110 stripped_filename = SRC_DIR_STRIP_RE .sub (r"\1" , filename )
108- print >> output_file , '<li><a href="#id%d">%s</a></li>' % (id_table .get (filename ), stripped_filename )
111+ print ( '<li><a href="#id%d">%s</a></li>' % (id_table .get (filename ), stripped_filename ), file = output_file )
109112
110- print >> output_file , "</ ul>"
111- print >> output_file , "</div><!-- table of contents -->"
113+ print ( "< ul>", file = output_file )
114+ print ( "</div><!-- table of contents -->" , file = output_file )
112115 # Output the individual notice file lists
113- print >> output_file , '<table cellpadding="0" cellspacing="0" border="0">'
116+ print ( '<table cellpadding="0" cellspacing="0" border="0">' , file = output_file )
114117 for value in file_hash :
115- print >> output_file , '<tr id="id%d"><td class="same-license">' % id_table .get (value [0 ])
116- print >> output_file , '<div class="label">Notices for file(s):</div>'
117- print >> output_file , '<div class="file-list">'
118+ print ('<tr id="id%d"><td class="same-license">' % id_table .get (value [0 ]), file = output_file )
119+ print ('<div class="label">Notices for file(s):</div>' , file = output_file )
118120 for filename in value :
119- print >> output_file , "%s <br/>" % (SRC_DIR_STRIP_RE .sub (r"\1" , filename ))
120- print >> output_file , "</div><!-- file-list -->"
121- print >> output_file
122- print >> output_file , '<pre class="license-text">'
123- print >> output_file , html_escape (open (value [0 ]).read ())
124- print >> output_file , "</pre><!-- license-text -->"
125- print >> output_file , "</td></tr><!-- same-license -->"
126- print >> output_file
127- print >> output_file
128- print >> output_file
121+ print ( "%s <br/>" % (SRC_DIR_STRIP_RE .sub (r"\1" , filename )), file = output_file )
122+ print ( "</div><!-- file-list -->" , file = output_file )
123+ print ( file = output_file )
124+ print ( '<pre class="license-text">' , file = output_file )
125+ print ( html_escape (open (value [0 ]).read ()), file = output_file )
126+ print ( "</pre><!-- license-text -->" , file = output_file )
127+ print ( "</td></tr><!-- same-license -->" , file = output_file )
128+ print ( file = output_file )
129+ print ( file = output_file )
130+ print ( file = output_file )
129131
130132 # Finish off the file output
131- print >> output_file , "</table>"
132- print >> output_file , "</body></html>"
133+ print ( "</table>" , file = output_file )
134+ print ( "</body></html>" , file = output_file )
133135 output_file .close ()
134136
137+
135138def combine_notice_files_text (file_hash , input_dirs , output_filename , file_title ):
136139 """Combine notice files in FILE_HASH and output a text version to OUTPUT_FILENAME."""
137140
138141 SRC_DIR_STRIP_RE = re .compile ("(?:" + "|" .join (re .escape (input_dirs )) + ")(/.*).txt" )
139142 output_file = open (output_filename , "wb" )
140- print >> output_file , file_title
143+ print ( file_title , file = output_file )
141144 for value in file_hash :
142- print >> output_file , "============================================================"
143- print >> output_file , "Notices for file(s):"
144- for filename in value :
145- print >> output_file , SRC_DIR_STRIP_RE .sub (r"\1" , filename )
146- print >> output_file , "------------------------------------------------------------"
147- print >> output_file , open (value [0 ]).read ()
145+ print ("============================================================" , file = output_file )
146+ print ("Notices for file(s):" , file = output_file )
147+
148+ for filename in value :
149+ print (SRC_DIR_STRIP_RE .sub (r"\1" , filename ), file = output_file )
150+ print ("------------------------------------------------------------" , file = output_file )
151+ print (open (value [0 ]).read (), file = output_file )
148152 output_file .close ()
149153
154+
150155def combine_notice_files_xml (files_with_same_hash , input_dirs , output_filename ):
151156 """Combine notice files in FILE_HASH and output a XML version to OUTPUT_FILENAME."""
152157
@@ -157,24 +162,24 @@ def combine_notice_files_xml(files_with_same_hash, input_dirs, output_filename):
157162 id_table = {}
158163 for file_key in files_with_same_hash .keys ():
159164 for filename in files_with_same_hash [file_key ]:
160- id_table [filename ] = file_key
165+ id_table [filename ] = file_key
161166
162167 # Open the output file, and output the header pieces
163168 output_file = open (output_filename , "wb" )
164169
165- print >> output_file , '<?xml version="1.0" encoding="utf-8"?>'
166- print >> output_file , "<licenses>"
170+ print ( '<?xml version="1.0" encoding="utf-8"?>' , file = output_file )
171+ print ( "<licenses>" , file = output_file )
167172
168173 # Flatten the list of lists into a single list of filenames
169174 sorted_filenames = sorted (id_table .keys ())
170175
171176 # Print out a nice table of contents
172177 for filename in sorted_filenames :
173178 stripped_filename = SRC_DIR_STRIP_RE .sub (r"\1" , filename )
174- print >> output_file , '<file-name contentId="%s">%s</file-name>' % (id_table .get (filename ), stripped_filename )
179+ print ( '<file-name contentId="%s">%s</file-name>' % (id_table .get (filename ), stripped_filename ), file = output_file )
175180
176- print >> output_file
177- print >> output_file
181+ print ( file = output_file )
182+ print ( file = output_file )
178183
179184 processed_file_keys = []
180185 # Output the individual notice file lists
@@ -184,13 +189,14 @@ def combine_notice_files_xml(files_with_same_hash, input_dirs, output_filename):
184189 continue
185190 processed_file_keys .append (file_key )
186191
187- print >> output_file , '<file-content contentId="%s"><![CDATA[%s]]></file-content>' % (file_key , html_escape (open (filename ).read ()))
188- print >> output_file
192+ print ( '<file-content contentId="%s"><![CDATA[%s]]></file-content>' % (file_key , html_escape (open (filename ).read ())), file = output_file )
193+ print ( file = output_file )
189194
190195 # Finish off the file output
191- print >> output_file , "</licenses>"
196+ print ( "</licenses>" , file = output_file )
192197 output_file .close ()
193198
199+
194200def get_args ():
195201 parser = argparse .ArgumentParser ()
196202 parser .add_argument (
@@ -216,6 +222,7 @@ def get_args():
216222 help = 'The sub directories which should be excluded.' )
217223 return parser .parse_args ()
218224
225+
219226def main (argv ):
220227 args = get_args ()
221228
@@ -229,7 +236,7 @@ def main(argv):
229236 included_subdirs = args .included_subdirs
230237 if args .excluded_subdirs is not None :
231238 excluded_subdirs = args .excluded_subdirs
232-
239+
233240 notice_files_to_append = os .path .join (args .source_dir [0 ], "notice" )
234241 copy_notice_files .copy_and_create_dir (notice_files_to_append )
235242 input_dirs = [os .path .normpath (source_dir ) for source_dir in args .source_dir ]
@@ -242,14 +249,16 @@ def main(argv):
242249 if len (included_subdirs ) > 0 :
243250 matched = False
244251 for subdir in included_subdirs :
245- if (root == (input_dir + '/' + subdir ) or
246- root .startswith (input_dir + '/' + subdir + '/' )):
252+ matches_subdir = root == (input_dir + '/' + subdir )
253+ starts_with_subdir = root .startswith (input_dir + '/' + subdir + '/' )
254+ if matches_subdir or starts_with_subdir :
247255 matched = True
248256 break
249257 elif len (excluded_subdirs ) > 0 :
250258 for subdir in excluded_subdirs :
251- if (root == (input_dir + '/' + subdir ) or
252- root .startswith (input_dir + '/' + subdir + '/' )):
259+ matches_subdir = root == (input_dir + '/' + subdir )
260+ starts_with_subdir = root .startswith (input_dir + '/' + subdir + '/' )
261+ if matches_subdir or starts_with_subdir :
253262 matched = False
254263 break
255264 if root .startswith (notice_files_to_append ):
@@ -268,5 +277,6 @@ def main(argv):
268277 if xml_output_file is not None :
269278 combine_notice_files_xml (files_with_same_hash , input_dirs , xml_output_file )
270279
280+
271281if __name__ == "__main__" :
272282 main (sys .argv )
0 commit comments