pack_ios.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import print_function
  2. import argparse
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. import tarfile
  8. def just_do_it():
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument("--binary", required=True, help="executable file")
  11. parser.add_argument("--target", required=True, help="target archive path")
  12. parser.add_argument("--temp-dir", required=True, help="temp dir")
  13. parser.add_argument("peers", nargs='*')
  14. args = parser.parse_args()
  15. app_tar = [p for p in args.peers if p.endswith('.ios.interface')]
  16. if not app_tar:
  17. print('No one IOS_INTERFACE module found', file=sys.stderr)
  18. shutil.copyfile(args.binary, os.path.join(args.temp_dir, 'bin'))
  19. if os.path.exists(args.target):
  20. os.remove(args.target)
  21. with tarfile.open(args.target, 'w') as tf:
  22. tf.add(
  23. os.path.join(args.temp_dir, 'bin'), arcname=os.path.join(os.path.basename(args.binary) + '.app', 'bin')
  24. )
  25. return
  26. if len(app_tar) > 1:
  27. app_tar = [p for p in args.peers if not p.endswith('.default.ios.interface')]
  28. if len(app_tar) > 1:
  29. print('Many IOS_INTERFACE modules found, {} will be used'.format(app_tar[-1]), file=sys.stderr)
  30. app_tar = app_tar[-1]
  31. with tarfile.open(app_tar) as tf:
  32. tf.extractall(args.temp_dir)
  33. tar_suffix = '.default.ios.interface' if app_tar.endswith('.default.ios.interface') else '.ios.interface'
  34. app_unpacked_path = os.path.join(args.temp_dir, os.path.basename(app_tar)[: -len(tar_suffix)] + '.app')
  35. if not os.path.exists(app_unpacked_path):
  36. raise Exception('Bad IOS_INTERFACE resource: {}'.format(app_tar))
  37. shutil.copyfile(args.binary, os.path.join(app_unpacked_path, 'bin'))
  38. subprocess.check_call(['/usr/bin/codesign', '--force', '--sign', '-', app_unpacked_path])
  39. if os.path.exists(args.target):
  40. os.remove(args.target)
  41. binary_origin_name = os.path.basename(args.binary)
  42. while os.path.splitext(binary_origin_name)[1]:
  43. binary_origin_name = os.path.splitext(binary_origin_name)[0]
  44. with tarfile.open(args.target, 'w') as tf:
  45. tf.add(app_unpacked_path, arcname=binary_origin_name + '.app', recursive=True)
  46. if __name__ == '__main__':
  47. just_do_it()