Skip to content

Commit b654673

Browse files
cclausstargos
authored andcommitted
tools: fix Python 3 syntax error in mac_tool.py
PR-URL: #30146 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Shelley Vohr <codebytere@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent 87cb6b2 commit b654673

1 file changed

Lines changed: 22 additions & 30 deletions

File tree

‎tools/gyp/pylib/gyp/mac_tool.py‎

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,29 +137,25 @@ def _CopyStringsFile(self, source, dest):
137137
# semicolon in dictionary.
138138
# on invalid files. Do the same kind of validation.
139139
importCoreFoundation
140-
s=open(source, 'rb').read()
140+
withopen(source, 'rb') asin_file:
141+
s=in_file.read()
141142
d=CoreFoundation.CFDataCreate(None, s, len(s))
142143
_, error=CoreFoundation.CFPropertyListCreateFromXMLData(None, d, 0, None)
143144
iferror:
144145
return
145146

146-
fp=open(dest, 'wb')
147-
fp.write(s.decode(input_code).encode('UTF-16'))
148-
fp.close()
147+
withopen(dest, 'wb') asfp:
148+
fp.write(s.decode(input_code).encode('UTF-16'))
149149

150150
def_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-
exceptException:
157-
fp.close()
158-
returnNone
159-
fp.close()
160-
ifheader.startswith("\xFE\xFF"):
161-
return"UTF-16"
162-
elifheader.startswith("\xFF\xFE"):
153+
withopen(file_name, 'rb') asfp:
154+
try:
155+
header=fp.read(3)
156+
exceptException:
157+
returnNone
158+
ifheader.startswith(("\xFE\xFF", "\xFF\xFE")):
163159
return"UTF-16"
164160
elifheader.startswith("\xEF\xBB\xBF"):
165161
return"UTF-8"
@@ -169,9 +165,8 @@ def _DetectInputEncoding(self, file_name):
169165
defExecCopyInfoPlist(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+
withopen(source, 'r') asfd:
169+
lines=fd.read()
175170

176171
# Insert synthesized key/value pairs (e.g. BuildMachineOSBuild).
177172
plist=plistlib.readPlistFromString(lines)
@@ -204,17 +199,16 @@ def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys):
204199
lines=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()
208203
foriinrange(len(lines)):
209204
iflines[i].strip().startswith("<string>${"):
210205
lines[i] =None
211206
lines[i-1] =None
212-
lines='\n'.join(filter(lambdax: xisnotNone, lines))
207+
lines='\n'.join(lineforlineinlinesiflineisnotNone)
213208

214209
# Write out the file with variables replaced.
215-
fd=open(dest, 'w')
216-
fd.write(lines)
217-
fd.close()
210+
withopen(dest, 'w') asfd:
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):
242236
signature_code='?'*4
243237

244238
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+
withopen(dest, 'w') asfp:
240+
fp.write('%s%s'% (package_type, signature_code))
248241

249242
defExecFlock(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+
withopen(os.path.join(module_path, 'module.modulemap'), "w") asmodule_file:
292+
module_file.write(module_template)
301293

302294
defExecPackageFramework(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

338330
defExecCompileIosFrameworkHeaderMap(self, out, framework, *all_headers):
339331
framework_name=os.path.basename(framework).split('.')[0]
340-
all_headers=map(os.path.abspath, all_headers)
332+
all_headers=[os.path.abspath(header) forheaderinall_headers]
341333
filelist= {}
342334
forheaderinall_headers:
343335
filename=os.path.basename(header)
@@ -669,7 +661,7 @@ def WriteHmap(output_name, filelist):
669661
count=len(filelist)
670662
capacity=NextGreaterPowerOf2(count)
671663
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) forvalueinfilelist.values())
673665

674666
out=open(output_name, "wb")
675667
out.write(struct.pack('<LHHLLLL', magic, version, _reserved, strings_offset,

0 commit comments

Comments
 (0)