add_tar_exec.py 813 B

12345678910111213141516171819202122232425
  1. import argparse
  2. from io import BytesIO
  3. import tarfile
  4. parser = argparse.ArgumentParser(
  5. description="Add the main binary to a tar and force it to be executable"
  6. )
  7. parser.add_argument("input_tar_file", help="input tar file")
  8. parser.add_argument("main_binary_path", help="Main executable path")
  9. parser.add_argument("main_binary_tar_path", help="Main executable tar path")
  10. args = parser.parse_args()
  11. input_tar_file = args.input_tar_file
  12. main_binary_path = args.main_binary_path
  13. main_binary_tar_path = args.main_binary_tar_path
  14. with open(main_binary_path, "rb") as f:
  15. with tarfile.open(input_tar_file, "a") as tar:
  16. data = f.read()
  17. tar_info = tarfile.TarInfo(main_binary_tar_path)
  18. tar_info.mode = 0o755
  19. tar_info.size = len(data)
  20. tar.addfile(tar_info, BytesIO(data))