Skip to content

Commit 8567120

Browse files
Merge pull request #3545 from nexB/license-rules-update-fall-2023-1
License rules update
2 parents 5412724 + 8504274 commit 8567120

File tree

342 files changed

+7929
-159
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

342 files changed

+7929
-159
lines changed

etc/scripts/licenses/buildrules.py

+117-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from licensedcode import cache
1616
from licensedcode import models
1717
from licensedcode import match_hash
18+
from licensedcode import frontmatter
19+
from license_expression import Licensing
1820

1921
"""
2022
A script to generate license detection rules from a simple text data file.
@@ -172,10 +174,112 @@ def find_rule_base_loc(license_expression):
172174
)
173175

174176

177+
def validate_and_dump_rules(rules, licenses_by_key, licenses_file_path):
178+
valid_rules_text, invalid_rules_text = validate_rules_with_details(rules, licenses_by_key)
179+
if invalid_rules_text:
180+
valid_rules_file = licenses_file_path + ".valid"
181+
with open(valid_rules_file, "w") as o:
182+
o.write(valid_rules_text)
183+
184+
invalid_rules_file = licenses_file_path + ".invalid"
185+
with open(invalid_rules_file, "w") as o:
186+
o.write(invalid_rules_text)
187+
188+
message = [
189+
'Errors while validating rules:',
190+
f'See valid rules file: {valid_rules_file}',
191+
f'See invalid rules file: {invalid_rules_file}',
192+
]
193+
raise models.InvalidRule('\n'.join(message))
194+
195+
196+
def validate_rules_with_details(rules, licenses_by_key):
197+
"""
198+
Return a tuple of (text of valid rules, text of invalid rules) in the format
199+
expected by this tool. Invalid rules have a YAML comment text added to their
200+
metadata section that describes the issue.
201+
"""
202+
203+
licensing = Licensing(symbols=licenses_by_key.values())
204+
205+
valid_rules_texts = []
206+
invalid_rules_texts = []
207+
208+
for rule in rules:
209+
error_messages = list(rule.validate(licensing, thorough=True))
210+
rule_as_text = dump_skinny_rule(rule, error_messages=error_messages)
211+
212+
if error_messages:
213+
invalid_rules_texts.append(rule_as_text)
214+
else:
215+
valid_rules_texts.append(rule_as_text)
216+
217+
valid_rules_text = "\n".join(valid_rules_texts) + start_delimiter
218+
219+
if invalid_rules_texts:
220+
invalid_rules_text = "\n".join(invalid_rules_texts) + start_delimiter
221+
else:
222+
invalid_rules_text = ""
223+
224+
return valid_rules_text, invalid_rules_text
225+
226+
227+
SKINNY_RULE_TEMPLATE = """\
228+
{start_delimiter}
229+
{metadata}
230+
{end_delimiter}
231+
{content}
232+
"""
233+
234+
start_delimiter = "----------------------------------------"
235+
236+
237+
def dump_skinny_rule(rule, error_messages=()):
238+
"""
239+
Return a string that dumps the ``rule`` Rule in the input format used by
240+
this tool. Add a comment with the ``error_message`` to the metadata if any.
241+
"""
242+
metadata = rule.to_dict()
243+
if error_messages:
244+
# add missing metadata for sanity
245+
if "license_expression" not in metadata:
246+
m = {"license_expression": None}
247+
m.update(metadata)
248+
metadata = m
249+
250+
if "notes" not in metadata:
251+
metadata["notes"] = None
252+
253+
if "referenced_filenames" not in metadata:
254+
metadata["referenced_filenames"] = None
255+
256+
msgs = "\n".join(f"# {m}" for m in error_messages)
257+
end_delimiter = f"{msgs}\n---"
258+
else:
259+
end_delimiter = "---"
260+
261+
return frontmatter.dumps_frontmatter(
262+
content=rule.text,
263+
metadata=metadata,
264+
template=SKINNY_RULE_TEMPLATE,
265+
start_delimiter=start_delimiter,
266+
end_delimiter=end_delimiter)
267+
268+
175269
@click.command()
176-
@click.argument("licenses_file", type=click.Path(), metavar="FILE")
270+
@click.argument(
271+
"licenses_file", type=click.Path(), metavar="FILE",
272+
)
273+
@click.option(
274+
"-d", "--dump-to-file-on-errors",
275+
is_flag=True,
276+
default=False,
277+
help="On errors, dump the valid rules and the invalid rules in text files "
278+
"named after the input FILE with a .valid and a .invalid extension.",
279+
)
280+
177281
@click.help_option("-h", "--help")
178-
def cli(licenses_file):
282+
def cli(licenses_file, dump_to_file_on_errors=False):
179283
"""
180284
Create rules from a text file with delimited blocks of metadata and texts.
181285
@@ -191,6 +295,12 @@ def cli(licenses_file):
191295
it under the terms of the GNU Lesser General Public License
192296
version 2.1 as published by the Free Software Foundation;
193297
----------------------------------------
298+
license_expression: lgpl-2.1
299+
relevance: 100
300+
is_license_reference: yes
301+
---
302+
LGPL-2.1
303+
----------------------------------------
194304
"""
195305

196306
rules_data = load_data(licenses_file)
@@ -213,7 +323,11 @@ def cli(licenses_file):
213323
rl = models.BasicRule(text=rdata.text, **rdata.data)
214324
skinny_rules.append(rl)
215325

216-
models.validate_rules(skinny_rules, licenses_by_key, with_text=True, thorough=True)
326+
# these will raise an exception and exit on errors
327+
if not dump_to_file_on_errors:
328+
models.validate_rules(rules=skinny_rules, licenses_by_key=licenses_by_key, with_text=True, thorough=True)
329+
else:
330+
validate_and_dump_rules(rules=skinny_rules, licenses_by_key=licenses_by_key, licenses_file_path=licenses_file)
217331

218332
print()
219333
for rule in skinny_rules:

src/licensedcode/data/licenses/mit.LICENSE

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ owner: MIT
77
homepage_url: http://opensource.org/licenses/mit-license.php
88
notes: Per SPDX.org, this license is OSI certified.
99
spdx_license_key: MIT
10+
other_spdx_license_keys:
11+
- LicenseRef-MIT-Bootstrap
12+
- LicenseRef-MIT-Discord
13+
- LicenseRef-MIT-TC
14+
- LicenseRef-MIT-Diehl
1015
text_urls:
1116
- http://opensource.org/licenses/mit-license.php
1217
osi_url: http://www.opensource.org/licenses/MIT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
license_expression: agpl-3.0-plus
3+
is_license_notice: yes
4+
---
5+
6+
The JavaScript code in this page is free software: you can
7+
redistribute it and/or modify it under the terms of the GNU Affero
8+
General Public License as published by the Free Software
9+
Foundation, either version 3 of the License, or (at your option)
10+
any later version. The code is distributed WITHOUT ANY WARRANTY;
11+
without even the implied warranty of MERCHANTABILITY or FITNESS
12+
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
13+
14+
As additional permission under GNU AGPL version 3 section 7, you
15+
may distribute non-source (e.g., minimized or compacted) forms of
16+
that code without the copy of the GNU AGPL normally required by
17+
section 4, provided you include this license notice and a URL
18+
through which recipients can access the Corresponding Source.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
relevance: 100
5+
referenced_filenames:
6+
- LICENSE
7+
---
8+
9+
The default license for this project is {{AGPL-3.0-only}}(LICENSE).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
relevance: 100
5+
---
6+
7+
license for this project is {{AGPL-3.0-only}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
relevance: 100
5+
---
6+
7+
license for this project is {{AGPL-3.0}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
---
5+
6+
/// License: AGPLv3
7+
///
8+
/// This program is free software: you can redistribute it and/or modify
9+
/// it under the terms of the GNU Affero General Public License as
10+
/// published by the Free Software Foundation, either version 3 of the
11+
/// License.
12+
///
13+
/// This program is distributed in the hope that it will be useful,
14+
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
/// GNU Affero General Public License for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
---
5+
6+
License: AGPLv3
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU Affero General Public License as
10+
published by the Free Software Foundation, either version 3.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Affero General Public License for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
---
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as
8+
published by the Free Software Foundation, either version 3.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Affero General Public License for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
---
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as
8+
published by the Free Software Foundation, either version 3 of the
9+
License.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Affero General Public License for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
---
5+
6+
is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as
8+
published by the Free Software Foundation, either version 3 of the
9+
License.
10+
is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Affero General Public License for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
---
5+
6+
is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as
8+
published by the Free Software Foundation, either version 3.
9+
is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Affero General Public License for more details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
ignorable_urls:
5+
- http://www.gnu.org/licenses/
6+
---
7+
8+
is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU Affero General Public License as
10+
published by the Free Software Foundation, either version 3 of the
11+
License.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU Affero General Public License for more details.
17+
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
ignorable_urls:
5+
- http://www.gnu.org/licenses/
6+
---
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU Affero General Public License as
10+
published by the Free Software Foundation, either version 3 of the
11+
License.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU Affero General Public License for more details.
17+
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
ignorable_urls:
5+
- http://www.gnu.org/licenses/
6+
---
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU Affero General Public License as
10+
published by the Free Software Foundation, either version 3.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Affero General Public License for more details.
16+
17+
You should have received a copy of the GNU Affero General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
license_expression: agpl-3.0
3+
is_license_notice: yes
4+
ignorable_urls:
5+
- http://www.gnu.org/licenses/
6+
---
7+
8+
-
9+
- is free software: you can redistribute it and/or modify
10+
- it under the terms of the GNU Affero General Public License as published by
11+
- the Free Software Foundation, either version 3 of the License.
12+
-
13+
- is distributed in the hope that it will be useful,
14+
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
- GNU General Public License for more details.
17+
-
18+
- You should have received a copy of the GNU Affero General Public License
19+
- along with . If not, see <http://www.gnu.org/licenses/>.

0 commit comments

Comments
 (0)