@@ -137,29 +137,25 @@ def _CopyStringsFile(self, source, dest):
137
137
# semicolon in dictionary.
138
138
# on invalid files. Do the same kind of validation.
139
139
import CoreFoundation
140
- s = open (source , 'rb' ).read ()
140
+ with open (source , 'rb' ) as in_file :
141
+ s = in_file .read ()
141
142
d = CoreFoundation .CFDataCreate (None , s , len (s ))
142
143
_ , error = CoreFoundation .CFPropertyListCreateFromXMLData (None , d , 0 , None )
143
144
if error :
144
145
return
145
146
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' ))
149
149
150
150
def _DetectInputEncoding (self , file_name ):
151
151
"""Reads the first few bytes from file_name and tries to guess the text
152
152
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 " )):
163
159
return "UTF-16"
164
160
elif header .startswith ("\xEF \xBB \xBF " ):
165
161
return "UTF-8"
@@ -169,9 +165,8 @@ def _DetectInputEncoding(self, file_name):
169
165
def ExecCopyInfoPlist (self , source , dest , convert_to_binary , * keys ):
170
166
"""Copies the |source| Info.plist to the destination directory |dest|."""
171
167
# 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 ()
175
170
176
171
# Insert synthesized key/value pairs (e.g. BuildMachineOSBuild).
177
172
plist = plistlib .readPlistFromString (lines )
@@ -204,17 +199,16 @@ def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys):
204
199
lines = string .replace (lines , evar , evalue )
205
200
206
201
# Remove any keys with values that haven't been replaced.
207
- lines = lines .split ( ' \n ' )
202
+ lines = lines .splitlines ( )
208
203
for i in range (len (lines )):
209
204
if lines [i ].strip ().startswith ("<string>${" ):
210
205
lines [i ] = None
211
206
lines [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 )
213
208
214
209
# 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 )
218
212
219
213
# Now write out PkgInfo file now that the Info.plist file has been
220
214
# "compiled".
@@ -242,9 +236,8 @@ def _WritePkgInfo(self, info_plist):
242
236
signature_code = '?' * 4
243
237
244
238
dest = 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 ))
248
241
249
242
def ExecFlock (self , lockfile , * cmd_list ):
250
243
"""Emulates the most basic behavior of Linux's flock(1)."""
@@ -295,9 +288,8 @@ def ExecPackageIosFramework(self, framework):
295
288
' module * { export * }\n ' \
296
289
'}\n ' % (binary , binary )
297
290
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 )
301
293
302
294
def ExecPackageFramework (self , framework , version ):
303
295
"""Takes a path to Something.framework and the Current version of that and
@@ -337,7 +329,7 @@ def _Relink(self, dest, link):
337
329
338
330
def ExecCompileIosFrameworkHeaderMap (self , out , framework , * all_headers ):
339
331
framework_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 ]
341
333
filelist = {}
342
334
for header in all_headers :
343
335
filename = os .path .basename (header )
@@ -669,7 +661,7 @@ def WriteHmap(output_name, filelist):
669
661
count = len (filelist )
670
662
capacity = NextGreaterPowerOf2 (count )
671
663
strings_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 () )
673
665
674
666
out = open (output_name , "wb" )
675
667
out .write (struct .pack ('<LHHLLLL' , magic , version , _reserved , strings_offset ,
0 commit comments