tared_protoc.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. import optparse
  3. import tarfile
  4. import stat
  5. import subprocess as sp
  6. def parse_args():
  7. parser = optparse.OptionParser()
  8. parser.disable_interspersed_args()
  9. parser.add_option('--tar-output')
  10. parser.add_option('--protoc-out-dir')
  11. return parser.parse_args()
  12. def main():
  13. opts, args = parse_args()
  14. assert opts.tar_output
  15. assert opts.protoc_out_dir
  16. if not os.path.exists(opts.protoc_out_dir):
  17. os.makedirs(opts.protoc_out_dir)
  18. sp.check_call(args)
  19. with tarfile.open(opts.tar_output, 'w', format=tarfile.USTAR_FORMAT) as tf:
  20. for root, dirs, files in os.walk(opts.protoc_out_dir, topdown=True):
  21. dirs.sort()
  22. for name in sorted(files):
  23. fname = os.path.join(root, name)
  24. with open(fname, 'rb') as fin:
  25. tarinfo = tf.gettarinfo(fname, os.path.relpath(fname, opts.protoc_out_dir))
  26. tarinfo.mode = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH if tarinfo.mode | stat.S_IXUSR else 0
  27. tarinfo.mode = (
  28. tarinfo.mode | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH
  29. )
  30. tarinfo.mtime = 0
  31. tarinfo.uid = 0
  32. tarinfo.gid = 0
  33. tarinfo.uname = 'dummy'
  34. tarinfo.gname = 'dummy'
  35. tf.addfile(tarinfo, fin)
  36. if __name__ == '__main__':
  37. main()