@@ -137,29 +137,25 @@ def _CopyStringsFile(self, source, dest):
137137# semicolon in dictionary.
138138# on invalid files. Do the same kind of validation.
139139import CoreFoundation
140- s = open (source , 'rb' ).read ()
140+ with open (source , 'rb' ) as in_file :
141+ s = in_file .read ()
141142d = CoreFoundation .CFDataCreate (None , s , len (s ))
142143_ , error = CoreFoundation .CFPropertyListCreateFromXMLData (None , d , 0 , None )
143144if error :
144145return
145146
146- fp = open (dest , 'wb' )
147- fp .write (s .decode (input_code ).encode ('UTF-16' ))
148- fp .close ()
147+ with open (dest , 'wb' ) as fp :
148+ fp .write (s .decode (input_code ).encode ('UTF-16' ))
149149
150150def _DetectInputEncoding (self , file_name ):
151151"""Reads the first few bytes from file_name and tries to guess the text
152152 encoding. Returns None as a guess if it can't detect it."""
153- fp = open (file_name , 'rb' )
154- try :
155- header = fp .read (3 )
156- except Exception :
157- fp .close ()
158- return None
159- fp .close ()
160- if header .startswith ("\xFE \xFF " ):
161- return "UTF-16"
162- elif header .startswith ("\xFF \xFE " ):
153+ with open (file_name , 'rb' ) as fp :
154+ try :
155+ header = fp .read (3 )
156+ except Exception :
157+ return None
158+ if header .startswith (("\xFE \xFF " , "\xFF \xFE " )):
163159return "UTF-16"
164160elif header .startswith ("\xEF \xBB \xBF " ):
165161return "UTF-8"
@@ -169,9 +165,8 @@ def _DetectInputEncoding(self, file_name):
169165def ExecCopyInfoPlist (self , source , dest , convert_to_binary , * keys ):
170166"""Copies the |source| Info.plist to the destination directory |dest|."""
171167# Read the source Info.plist into memory.
172- fd = open (source , 'r' )
173- lines = fd .read ()
174- fd .close ()
168+ with open (source , 'r' ) as fd :
169+ lines = fd .read ()
175170
176171# Insert synthesized key/value pairs (e.g. BuildMachineOSBuild).
177172plist = plistlib .readPlistFromString (lines )
@@ -204,17 +199,16 @@ def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys):
204199lines = string .replace (lines , evar , evalue )
205200
206201# Remove any keys with values that haven't been replaced.
207- lines = lines .split ( ' \n ' )
202+ lines = lines .splitlines ( )
208203for i in range (len (lines )):
209204if lines [i ].strip ().startswith ("<string>${" ):
210205lines [i ] = None
211206lines [i - 1 ] = None
212- lines = '\n ' .join (filter ( lambda x : x is not None , lines ) )
207+ lines = '\n ' .join (line for line in lines if line is not None )
213208
214209# Write out the file with variables replaced.
215- fd = open (dest , 'w' )
216- fd .write (lines )
217- fd .close ()
210+ with open (dest , 'w' ) as fd :
211+ fd .write (lines )
218212
219213# Now write out PkgInfo file now that the Info.plist file has been
220214# "compiled".
@@ -242,9 +236,8 @@ def _WritePkgInfo(self, info_plist):
242236signature_code = '?' * 4
243237
244238dest = os .path .join (os .path .dirname (info_plist ), 'PkgInfo' )
245- fp = open (dest , 'w' )
246- fp .write ('%s%s' % (package_type , signature_code ))
247- fp .close ()
239+ with open (dest , 'w' ) as fp :
240+ fp .write ('%s%s' % (package_type , signature_code ))
248241
249242def ExecFlock (self , lockfile , * cmd_list ):
250243"""Emulates the most basic behavior of Linux's flock(1)."""
@@ -295,9 +288,8 @@ def ExecPackageIosFramework(self, framework):
295288' module * { export * }\n ' \
296289'}\n ' % (binary , binary )
297290
298- module_file = open (os .path .join (module_path , 'module.modulemap' ), "w" )
299- module_file .write (module_template )
300- module_file .close ()
291+ with open (os .path .join (module_path , 'module.modulemap' ), "w" ) as module_file :
292+ module_file .write (module_template )
301293
302294def ExecPackageFramework (self , framework , version ):
303295"""Takes a path to Something.framework and the Current version of that and
@@ -337,7 +329,7 @@ def _Relink(self, dest, link):
337329
338330def ExecCompileIosFrameworkHeaderMap (self , out , framework , * all_headers ):
339331framework_name = os .path .basename (framework ).split ('.' )[0 ]
340- all_headers = map ( os .path .abspath , all_headers )
332+ all_headers = [ os .path .abspath ( header ) for header in all_headers ]
341333filelist = {}
342334for header in all_headers :
343335filename = os .path .basename (header )
@@ -669,7 +661,7 @@ def WriteHmap(output_name, filelist):
669661count = len (filelist )
670662capacity = NextGreaterPowerOf2 (count )
671663strings_offset = 24 + (12 * capacity )
672- max_value_length = len ( max (filelist . items (), key = lambda ( k , v ): len ( v ))[ 1 ] )
664+ max_value_length = max (len ( value ) for value in filelist . values () )
673665
674666out = open (output_name , "wb" )
675667out .write (struct .pack ('<LHHLLLL' , magic , version , _reserved , strings_offset ,
0 commit comments