write_pkg_info.py 1.7 KB

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