pack_ios.py 2.2 KB

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