Debian Codegraph @

data/generators/generate-spdx-syntax.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # Generate SPDX-Comments syntax file
5 #
6 # SPDX-FileCopyrightText: 2020 Alex Turbov <[email protected]>
7 # SPDX-License-Identifier: MIT
8 #
9 # To install prerequisites:
10 #
11 # $ pip install --user click jinja2
12 #
13 # To use:
14 #
15 # $ ./generate-spdx-syntax.py > ../syntax/spdx-comments.xml
16 #
17
18 import json
19 import pathlib
20 import urllib.request
21
22 import click
23 import jinja2
24
25
26 def get_json(url):
27 with urllib.request.urlopen(url=url) as body:
28 return json.load(body)
29
30
31 @click.command()
32 @click.argument('template', type=click.File('r'), default='./spdx-comments.xml.tpl')
33 def cli(template):
34
35 data = {
36 'licenses': [
37 *filter(
38 lambda l: not l['licenseId'].endswith('+')
39 , get_json(url='https://spdx.org/licenses/licenses.json')['licenses']
40 )
41 ]
42 , 'exceptions': [
43 *filter(
44 lambda l: not l['licenseExceptionId'].endswith('+')
45 , get_json(url='https://spdx.org/licenses/exceptions.json')['exceptions']
46 )
47 ]
48 }
49
50 env = jinja2.Environment(
51 keep_trailing_newline=True
52 )
53 env.block_start_string = '<!--['
54 env.block_end_string = ']-->'
55 env.variable_start_string = '<!--{'
56 env.variable_end_string = '}-->'
57 env.comment_start_string = '<!--#'
58 env.comment_end_string = '#-->'
59
60 tpl = env.from_string(template.read())
61 result = tpl.render(data)
62 print(result.strip(), end=None)
63
64
65 if __name__ == '__main__':
66 cli()
67 # TODO Handle execptions and show errors