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 <[email protected]> Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent 87cb6b2 commit b654673

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

tools/gyp/pylib/gyp/mac_tool.py

+22-30
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
import CoreFoundation
140-
s = open(source, 'rb').read()
140+
with open(source, 'rb') as in_file:
141+
s = in_file.read()
141142
d = CoreFoundation.CFDataCreate(None, s, len(s))
142143
_, error = CoreFoundation.CFPropertyListCreateFromXMLData(None, d, 0, None)
143144
if error:
144145
return
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

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-
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")):
163159
return "UTF-16"
164160
elif header.startswith("\xEF\xBB\xBF"):
165161
return "UTF-8"
@@ -169,9 +165,8 @@ def _DetectInputEncoding(self, file_name):
169165
def 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).
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
for i in range(len(lines)):
209204
if lines[i].strip().startswith("<string>${"):
210205
lines[i] = None
211206
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)
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):
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+
with open(dest, 'w') as fp:
240+
fp.write('%s%s' % (package_type, signature_code))
248241

249242
def 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

302294
def 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

338330
def ExecCompileIosFrameworkHeaderMap(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) for header in all_headers]
341333
filelist = {}
342334
for header in all_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) for value in filelist.values())
673665

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

0 commit comments

Comments
 (0)