aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/merge_protosrc.py
blob: 55e44c9063a4382a1ad1a4b1a6e1b4b76e0c982c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import argparse
import os
import tarfile
import sys


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--output', required=True)
    parser.add_argument('args', nargs='+')
    return parser.parse_args()


def main(args):
    peers = args.args

    compression_mode = ''
    if args.output.endswith(('.tar.gz', '.tgz')):
        compression_mode = 'gz'
    elif args.output.endswith('.bzip2'):
        compression_mode = 'bz2'

    files = set()
    with tarfile.open(args.output, f'w:{compression_mode}') as dest:
        for psrc in [p[:-len('.self.protodesc')]+'.protosrc' for p in peers if p.endswith('.self.protodesc')]:
            with tarfile.open(psrc, 'r') as src:
                for tarinfo in [m for m in src.getmembers() if m.name != '']:
                    if tarinfo.name in files:
                        continue
                    files.add(tarinfo.name)
                    if tarinfo.isdir():
                        dest.addfile(tarinfo)
                    else:
                        dest.addfile(tarinfo, src.extractfile(tarinfo))
    return 0


if __name__ == '__main__':
    sys.exit(main(parse_args()))