xref: /aosp_15_r20/external/angle/build/apple/write_pkg_info.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import argparse
6import os
7import plist_util
8import sys
9
10# This script creates a PkgInfo file for an OS X .app bundle's plist.
11# Usage: python write_pkg_info.py --plist Foo.app/Contents/Info.plist \
12#           --output Foo.app/Contents/PkgInfo
13
14
15def Main():
16  parser = argparse.ArgumentParser(
17      description='A script to write PkgInfo files for .app bundles.')
18  parser.add_argument('--plist',
19                      required=True,
20                      help='Path to the Info.plist for the .app.')
21  parser.add_argument('--output',
22                      required=True,
23                      help='Path to the desired output file.')
24  args = parser.parse_args()
25
26  # Remove the output if it exists already.
27  try:
28    os.unlink(args.output)
29  except FileNotFoundError:
30    pass
31
32  plist = plist_util.LoadPList(args.plist)
33  package_type = plist['CFBundlePackageType']
34  if package_type != 'APPL':
35    raise ValueError('Expected CFBundlePackageType to be %s, got %s' % \
36        ('APPL', package_type))
37
38  # The format of PkgInfo is eight characters, representing the bundle type
39  # and bundle signature, each four characters. If that is missing, four
40  # '?' characters are used instead.
41  signature_code = plist.get('CFBundleSignature', '????')
42  if len(signature_code) != 4:
43    raise ValueError('CFBundleSignature should be exactly four characters, ' +
44                     'got %s' % signature_code)
45
46  with open(args.output, 'w') as fp:
47    fp.write('%s%s' % (package_type, signature_code))
48  return 0
49
50
51if __name__ == '__main__':
52  sys.exit(Main())
53