aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/generate_mf.py
blob: 447215c723525ae0d0b13307569ba2e21f2774c8 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
import optparse
import os
import sys
import io

import six

import process_command_files as pcf


class BadMfError(Exception):
    pass


class GplNotAllowed(Exception):
    pass


def process_quotes(s):
    for quote_char in '\'"':
        if s.startswith(quote_char) and s.endswith(quote_char):
            return s[1:-1]
    return s


def parse_args():
    args = pcf.get_args(sys.argv[1:])
    lics, peers, free_args, credits = [], [], [], []
    current_list = free_args
    for a in args:
        if a == '-Ya,lics':
            current_list = lics
        elif a == '-Ya,peers':
            current_list = peers
        elif a == '-Ya,credits':
            current_list = credits
        elif a and a.startswith('-'):
            current_list = free_args
            current_list.append(a)
        else:
            current_list.append(a)

    parser = optparse.OptionParser()
    parser.add_option('--build-root')
    parser.add_option('--module-name')
    parser.add_option('-o', '--output')
    parser.add_option('-c', '--credits-output')
    parser.add_option('-t', '--type')
    opts, _ = parser.parse_args(free_args)
    return (
        lics,
        peers,
        credits,
        opts,
    )


def generate_header(meta):
    return '-' * 20 + meta.get('path', 'Unknown module') + '-' * 20


def generate_mf():
    lics, peers, credits, options = parse_args()

    meta = {
        'module_name': options.module_name,
        'path': os.path.dirname(options.output),
        'licenses': lics,
        'dependencies': [],
        'license_texts': '',
    }

    build_root = options.build_root
    file_name = os.path.join(build_root, options.output)

    if options.type != 'LIBRARY':
        for rel_filename in peers:
            with open(os.path.join(build_root, rel_filename + '.mf')) as peer_file:
                peer_meta = json.load(peer_file)
                meta['dependencies'].append(peer_meta)

    if credits:
        union_texts = []
        for texts_file in credits:
            with open(process_quotes(texts_file)) as f:
                union_texts.append(f.read())
        meta['license_texts'] = '\n\n'.join(union_texts)

    if options.credits_output:
        final_credits = []
        if meta['license_texts']:
            final_credits.append(generate_header(meta) + '\n' + meta['license_texts'])
        for peer in peers:
            candidate = os.path.join(build_root, peer + '.mf')
            with open(candidate) as src:
                data = json.loads(src.read())
                texts = data.get('license_texts')
                if texts:
                    candidate_text = generate_header(data) + '\n' + texts
                    if isinstance(candidate_text, six.text_type):
                        candidate_text = candidate_text.encode('utf-8')
                    final_credits.append(candidate_text)

        with io.open(options.credits_output, 'w', encoding='utf-8') as f:
            data = '\n\n'.join(final_credits)
            if isinstance(data, str):
                data = data.decode('utf-8')
            f.write(data)

    with open(file_name, 'w') as mf_file:
        json.dump(meta, mf_file, indent=4)


if __name__ == '__main__':
    try:
        generate_mf()
    except Exception as e:
        sys.stderr.write(str(e) + '\n')
        sys.exit(1)