Commit f95fe0b1 authored by Nico Weber's avatar Nico Weber Committed by Commit Bot

mac: Don't shell out to plutil for format conversions with Python 3.

plistlib can do everything we need in Python 3.
No behavior change.

Bug: 1112471,1147069
Change-Id: Ife5f754133a3694004008909b3e819afca03e278
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2561276
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Auto-Submit: Nico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#831182}
parent 9391ece9
...@@ -35,6 +35,7 @@ TOP = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) ...@@ -35,6 +35,7 @@ TOP = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
def _ConvertPlist(source_plist, output_plist, fmt): def _ConvertPlist(source_plist, output_plist, fmt):
"""Convert |source_plist| to |fmt| and save as |output_plist|.""" """Convert |source_plist| to |fmt| and save as |output_plist|."""
assert sys.version_info.major == 2, "Use plistlib directly in Python 3"
return subprocess.call( return subprocess.call(
['plutil', '-convert', fmt, '-o', output_plist, source_plist]) ['plutil', '-convert', fmt, '-o', output_plist, source_plist])
...@@ -401,16 +402,15 @@ def Main(argv): ...@@ -401,16 +402,15 @@ def Main(argv):
output_path = options.plist_output output_path = options.plist_output
# Now that all keys have been mutated, rewrite the file. # Now that all keys have been mutated, rewrite the file.
with tempfile.NamedTemporaryFile() as temp_info_plist: # Convert Info.plist to the format requested by the --format flag. Any
if sys.version_info.major == 2: # format would work on Mac but iOS requires specific format.
if sys.version_info.major == 2:
with tempfile.NamedTemporaryFile() as temp_info_plist:
plistlib.writePlist(plist, temp_info_plist.name) plistlib.writePlist(plist, temp_info_plist.name)
else: return _ConvertPlist(temp_info_plist.name, output_path, options.format)
with open(temp_info_plist.name, 'wb') as f: with open(output_path, 'wb') as f:
plist = plistlib.dump(plist, f) plist_format = {'binary1': plistlib.FMT_BINARY, 'xml1': plistlib.FMT_XML}
plistlib.dump(plist, f, fmt=plist_format[options.format])
# Convert Info.plist to the format requested by the --format flag. Any
# format would work on Mac but iOS requires specific format.
return _ConvertPlist(temp_info_plist.name, output_path, options.format)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -109,22 +109,24 @@ def LoadPList(path): ...@@ -109,22 +109,24 @@ def LoadPList(path):
def SavePList(path, format, data): def SavePList(path, format, data):
"""Saves |data| as a Plist to |path| in the specified |format|.""" """Saves |data| as a Plist to |path| in the specified |format|."""
fd, name = tempfile.mkstemp() if sys.version_info.major == 2:
try: fd, name = tempfile.mkstemp()
# "plutil" does not replace the destination file but update it in place, try:
# so if more than one hardlink points to destination all of them will be # "plutil" does not replace the destination file but update it in place,
# modified. This is not what is expected, so delete destination file if # so if more than one hardlink points to destination all of them will be
# it does exist. # modified. This is not what is expected, so delete destination file if
if os.path.exists(path): # it does exist.
os.unlink(path) if os.path.exists(path):
with os.fdopen(fd, 'wb') as f: os.unlink(path)
if sys.version_info.major == 2: with os.fdopen(fd, 'wb') as f:
plistlib.writePlist(data, f) plistlib.writePlist(data, f)
else: subprocess.check_call(['plutil', '-convert', format, '-o', path, name])
plistlib.dump(data, f) finally:
subprocess.check_call(['plutil', '-convert', format, '-o', path, name]) os.unlink(name)
finally: else:
os.unlink(name) with open(path, 'wb') as f:
plist_format = {'binary1': plistlib.FMT_BINARY, 'xml1': plistlib.FMT_XML}
plistlib.dump(data, f, fmt=plist_format[options.format])
def MergePList(plist1, plist2): def MergePList(plist1, plist2):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment