merge_protosrc.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import argparse
  2. import os
  3. import tarfile
  4. import sys
  5. def parse_args():
  6. parser = argparse.ArgumentParser()
  7. parser.add_argument('--output', required=True)
  8. parser.add_argument('args', nargs='+')
  9. return parser.parse_args()
  10. def main(args):
  11. peers = args.args
  12. compression_mode = ''
  13. if args.output.endswith(('.tar.gz', '.tgz')):
  14. compression_mode = 'gz'
  15. elif args.output.endswith('.bzip2'):
  16. compression_mode = 'bz2'
  17. files = set()
  18. with tarfile.open(args.output, f'w:{compression_mode}') as dest:
  19. for psrc in [p[:-len('.self.protodesc')]+'.protosrc' for p in peers if p.endswith('.self.protodesc')]:
  20. with tarfile.open(psrc, 'r') as src:
  21. for tarinfo in [m for m in src.getmembers() if m.name != '']:
  22. if tarinfo.name in files:
  23. continue
  24. files.add(tarinfo.name)
  25. if tarinfo.isdir():
  26. dest.addfile(tarinfo)
  27. else:
  28. dest.addfile(tarinfo, src.extractfile(tarinfo))
  29. return 0
  30. if __name__ == '__main__':
  31. sys.exit(main(parse_args()))