aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/collect_rawproto.py
blob: 92086b4f773402adf200aba7e9d542596a5efe15 (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
import argparse
import os
import tarfile
import stat
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):
    rawprotos = args.args
    with tarfile.open(args.output, 'w') as fout:
        for rawproto in sorted(os.path.normpath(r).replace('\\', '/') for r in rawprotos):
            assert rawproto.endswith('.rawproto')
            arcname = os.path.splitext(rawproto[:-len('.rawproto')])[0]
            with open(rawproto, 'rb') as fin:
                tarinfo = fout.gettarinfo(rawproto, arcname)
                tarinfo.mode = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH if tarinfo.mode | stat.S_IXUSR else 0
                tarinfo.mode = (
                    tarinfo.mode | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH
                )
                tarinfo.mtime = 0
                tarinfo.uid = 0
                tarinfo.gid = 0
                tarinfo.uname = 'dummy'
                tarinfo.gname = 'dummy'
                fout.addfile(tarinfo, fin)
    return 0


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