22
22
# either express or implied.
23
23
# No content from ScanCode LicenseDB should be considered or used as legal advice.
24
24
# Consult an attorney for any legal advice.
25
- #
26
- # Visit https://github.com/nexB/scancode-licensedb for support.
27
- #
28
- # ScanCode Toolkit is a free Software Composition Analysis tool from nexB Inc. and
29
- # others.
30
- # Visit https://github.com/nexB/scancode-toolkit for support and download.
31
25
26
+ import os
32
27
import json
33
28
import pathlib
34
29
from datetime import datetime
30
+ from os .path import dirname
31
+ from os .path import join
32
+ from distutils .dir_util import copy_tree
33
+
35
34
36
35
import saneyaml
37
- from jinja2 import Environment , PackageLoader
36
+ from jinja2 import Environment , FileSystemLoader
38
37
from licensedcode .models import load_licenses
39
38
from scancode_config import __version__ as scancode_version
40
39
41
- licenses = load_licenses (with_deprecated = True )
42
40
43
- # GitHub Pages support only /(root) or docs/ for the source
44
- BUILD_LOCATION = "docs"
45
-
46
- env = Environment (
47
- loader = PackageLoader ("app" , "templates" ),
48
- autoescape = True ,
49
- )
41
+ TEMPLATES_DIR = os .path .join (dirname (__file__ ), 'templates' )
42
+ STATIC_DIR = os .path .join (dirname (__file__ ), 'static' )
50
43
51
44
52
45
def write_file (path , filename , content ):
@@ -63,8 +56,15 @@ def now():
63
56
}
64
57
65
58
66
- def generate_indexes (output_path ):
67
- license_list_template = env .get_template ("license_list.html" )
59
+ def generate_indexes (output_path , environment , licenses ):
60
+
61
+ static_dest_dir = join (output_path , 'static' )
62
+ if not os .path .exists (static_dest_dir ):
63
+ os .makedirs (static_dest_dir )
64
+
65
+ copy_tree (STATIC_DIR , static_dest_dir )
66
+
67
+ license_list_template = environment .get_template ("license_list.html" )
68
68
index_html = license_list_template .render (
69
69
** base_context ,
70
70
licenses = licenses ,
@@ -74,50 +74,93 @@ def generate_indexes(output_path):
74
74
index = [
75
75
{
76
76
"license_key" : key ,
77
+ "category" : license .category ,
77
78
"spdx_license_key" : license .spdx_license_key ,
78
79
"other_spdx_license_keys" : license .other_spdx_license_keys ,
79
80
"is_exception" : license .is_exception ,
80
81
"is_deprecated" : license .is_deprecated ,
82
+ "text" : license .text ,
81
83
"json" : f"{ key } .json" ,
82
- "yml " : f"{ key } .yml" ,
84
+ "yaml " : f"{ key } .yml" ,
83
85
"html" : f"{ key } .html" ,
84
- "text " : f"{ key } .LICENSE" ,
86
+ "license " : f"{ key } .LICENSE" ,
85
87
}
86
88
for key , license in licenses .items ()
87
89
]
88
- write_file (output_path , "index.json" , json .dumps (index ))
89
- write_file (output_path , "index.yml" , saneyaml .dump (index ))
90
+
91
+ write_file (
92
+ output_path ,
93
+ "index.json" ,
94
+ json .dumps (index , indent = 2 , sort_keys = False )
95
+ )
96
+ write_file (
97
+ output_path ,
98
+ "index.yml" ,
99
+ saneyaml .dump (index , indent = 2 )
100
+ )
90
101
91
102
92
- def generate_details (output_path ):
93
- license_details_template = env .get_template ("license_details.html" )
103
+ def generate_details (output_path , environment , licenses ):
104
+ license_details_template = environment .get_template ("license_details.html" )
94
105
for license in licenses .values ():
95
- license_data = license .to_dict ()
106
+ license_data = license .to_dict (include_text = True )
96
107
html = license_details_template .render (
97
108
** base_context ,
98
109
license = license ,
99
110
license_data = license_data ,
100
111
)
101
112
write_file (output_path , f"{ license .key } .html" , html )
102
- write_file (output_path , f"{ license .key } .yml" , saneyaml .dump (license_data ))
103
- write_file (output_path , f"{ license .key } .json" , json .dumps (license_data ))
104
- write_file (output_path , f"{ license .key } .LICENSE" , license .text )
113
+ write_file (
114
+ output_path ,
115
+ f"{ license .key } .yml" ,
116
+ saneyaml .dump (license_data , indent = 2 )
117
+ )
118
+ write_file (
119
+ output_path ,
120
+ f"{ license .key } .json" ,
121
+ json .dumps (license_data , indent = 2 , sort_keys = False )
122
+ )
123
+ license .dump (output_path )
105
124
106
125
107
- def generate_help (output_path ):
108
- template = env .get_template ("help.html" )
126
+ def generate_help (output_path , environment ):
127
+ template = environment .get_template ("help.html" )
109
128
html = template .render (** base_context )
110
129
write_file (output_path , "help.html" , html )
111
130
112
131
113
- def generate ():
114
- root_path = pathlib .Path (BUILD_LOCATION )
132
+ def generate (build_location , template_dir = TEMPLATES_DIR ):
133
+
134
+ if not os .path .exists (build_location ):
135
+ os .makedirs (build_location )
136
+
137
+ env = Environment (
138
+ loader = FileSystemLoader (template_dir ),
139
+ autoescape = True ,
140
+ )
141
+ licenses = load_licenses (with_deprecated = True )
142
+
143
+ root_path = pathlib .Path (build_location )
115
144
root_path .mkdir (parents = False , exist_ok = True )
116
145
117
- generate_indexes (root_path )
118
- generate_details (root_path )
119
- generate_help (root_path )
146
+ generate_indexes (output_path = root_path , environment = env , licenses = licenses )
147
+ generate_details (output_path = root_path , environment = env , licenses = licenses )
148
+ generate_help (output_path = root_path , environment = env )
149
+
150
+
151
+ def dump_license_data (ctx , param , value ):
152
+ """
153
+ Dump license data from scancode licenses to the directory passed in from
154
+ command line.
120
155
156
+ Dumps data in JSON, YAML and HTML formats and also dumps the .LICENSE file with
157
+ the license text and the data as YAML frontmatter.
158
+ """
159
+ if not value or ctx .resilient_parsing :
160
+ return
121
161
122
- if __name__ == "__main__" :
123
- generate ()
162
+ import click
163
+ click .echo ('Dumping license data to the directory specified: ' )
164
+ generate (build_location = value )
165
+ click .echo ('Done.' )
166
+ ctx .exit (0 )
0 commit comments