collect_rawproto.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import argparse
  2. import os
  3. import tarfile
  4. import stat
  5. import sys
  6. def parse_args():
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument('--output', required=True)
  9. parser.add_argument('args', nargs='*')
  10. return parser.parse_args()
  11. def main(args):
  12. rawprotos = args.args
  13. with tarfile.open(args.output, 'w') as fout:
  14. for rawproto in sorted(os.path.normpath(r).replace('\\', '/') for r in rawprotos):
  15. assert rawproto.endswith('.rawproto')
  16. arcname = os.path.splitext(rawproto[:-len('.rawproto')])[0]
  17. with open(rawproto, 'rb') as fin:
  18. tarinfo = fout.gettarinfo(rawproto, arcname)
  19. tarinfo.mode = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH if tarinfo.mode | stat.S_IXUSR else 0
  20. tarinfo.mode = (
  21. tarinfo.mode | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH
  22. )
  23. tarinfo.mtime = 0
  24. tarinfo.uid = 0
  25. tarinfo.gid = 0
  26. tarinfo.uname = 'dummy'
  27. tarinfo.gname = 'dummy'
  28. fout.addfile(tarinfo, fin)
  29. return 0
  30. if __name__ == '__main__':
  31. sys.exit(main(parse_args()))