summaryrefslogtreecommitdiffstats
path: root/bin/vendor-licenses
blob: 9bbce91a00d4b15cc7c777b7d161f38bf049c53c (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
"""Usage:

    ./bin/vendor-licenses > identify/vendor/licenses.py
"""
import argparse
import os.path
import subprocess
import tempfile


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument('--revision', default='HEAD')
    args = parser.parse_args()

    licenses = []

    with tempfile.TemporaryDirectory() as tmpdir:
        subprocess.check_call((
            'git', 'clone', '--no-checkout', '--quiet',
            'https://github.com/github/choosealicense.com', tmpdir,
        ))
        subprocess.check_call((
            'git', '-C', tmpdir, 'checkout', args.revision, '--', '_licenses',
        ))

        for filename in os.listdir(os.path.join(tmpdir, '_licenses')):
            filename = os.path.join(tmpdir, '_licenses', filename)

            with open(filename) as f:
                contents = f.read()

            _, data, license_text = contents.split('---\n', 2)

            spdx, = (
                line[len('spdx-id:'):].strip()
                for line in data.splitlines()
                if line.startswith('spdx-id:')
            )

            licenses.append((spdx, license_text))

        print('LICENSES = (')
        for spdx, text in sorted(licenses):
            print('    (')
            print(f'        {spdx!r},')
            print("        '''\\")
            print(text.replace('\t', '    ').replace(' \n', '').strip())
            print("''',")
            print('    ),')
        print(')')
    return 0


if __name__ == '__main__':
    raise SystemExit(main())