Skip to content

Commit d1e725d

Browse files
authored
Merge pull request #2592 from akugarg/improve_license_detection
Detect unknown licenses #1675 Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
2 parents 46a3594 + 7a88832 commit d1e725d

289 files changed

Lines changed: 2716 additions & 461 deletions

File tree

Some content is hidden

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

etc/scripts/licenses/buildrules.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,22 @@ def rule_exists(text):
143143
return match.rule.identifier
144144

145145

146-
def all_rule_tokens():
146+
def all_rule_by_tokens():
147147
"""
148-
Return a set of tuples of tokens, one corresponding to every existing and
149-
added rules. Used to avoid duplicates.
148+
Return a mapping of {tuples of tokens: rule id}, with one item for each
149+
existing and added rules. Used to avoid duplicates.
150150
"""
151-
rule_tokens = set()
151+
rule_tokens = {}
152152
for rule in models.get_rules():
153-
rule_tokens.add(tuple(rule.tokens()))
153+
try:
154+
rule_tokens[tuple(rule.tokens())] = rule.identifier
155+
except Exception as e:
156+
df=(' file://' + rule.data_file)
157+
tf=(' file://' + rule.text_file)
158+
raise Exception(
159+
f'Failed to to get tokens from rule:: {rule.identifier}\n'
160+
f'{df}\n{tf}'
161+
) from e
154162
return rule_tokens
155163

156164

@@ -185,7 +193,7 @@ def cli(licenses_file):
185193
"""
186194

187195
rules_data = load_data(licenses_file)
188-
rules_tokens = all_rule_tokens()
196+
rule_by_tokens = all_rule_by_tokens()
189197

190198
licenses_by_key = cache.get_licenses_db()
191199
skinny_rules = []
@@ -205,10 +213,6 @@ def cli(licenses_file):
205213

206214
print()
207215
for rule in skinny_rules:
208-
existing = rule_exists(rule.text())
209-
if existing:
210-
print('Skipping existing rule:', existing, 'with text:\n', rule.text()[:50].strip(), '...')
211-
continue
212216

213217
if rule.is_false_positive:
214218
base_name = 'false-positive'
@@ -217,6 +221,21 @@ def cli(licenses_file):
217221
else:
218222
base_name = rule.license_expression
219223

224+
text = rule.text()
225+
226+
existing_rule = rule_exists(text)
227+
skinny_text = ' '.join(text[:80].split())
228+
229+
existing_msg = (
230+
f'Skipping rule for: {base_name!r}, '
231+
'dupe of: {existing_rule} '
232+
f'with text: {skinny_text!r}...'
233+
)
234+
235+
if existing_rule:
236+
print(existing_msg.format(**locals()))
237+
continue
238+
220239
base_loc = find_rule_base_loc(base_name)
221240

222241
rd = rule.to_dict()
@@ -234,17 +253,20 @@ def cli(licenses_file):
234253

235254
rule_tokens = tuple(rulerec.tokens())
236255

237-
if rule_tokens in rules_tokens:
238-
print('Skipping already added rule with text for:', base_name)
256+
existing_rule = rule_by_tokens.get(rule_tokens)
257+
if existing_rule:
258+
print(existing_msg.format(**locals()))
259+
continue
239260
else:
240-
print('Adding new rule:')
261+
print(f'Adding new rule: {base_name}')
241262
print(' file://' + rulerec.data_file)
242263
print(' file://' + rulerec.text_file,)
243-
rules_tokens.add(rule_tokens)
244264
rulerec.dump()
245265
models.update_ignorables(rulerec, verbose=False)
246266
rulerec.dump()
247267

268+
rule_by_tokens[rule_tokens] = base_name
269+
248270

249271
if __name__ == '__main__':
250272
cli()

src/licensedcode/data/licenses/agpl-3.0-plus.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ notes: |
1010
spdx_license_key: AGPL-3.0-or-later
1111
other_spdx_license_keys:
1212
- AGPL-3.0+
13+
- LicenseRef-AGPL
1314
text_urls:
1415
- http://www.gnu.org/licenses/agpl.txt
1516
osi_url: http://www.opensource.org/licenses/agpl-v3.html

src/licensedcode/data/licenses/agpl-3.0.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ notes: |
1010
spdx_license_key: AGPL-3.0-only
1111
other_spdx_license_keys:
1212
- AGPL-3.0
13+
- LicenseRef-AGPL-3.0
1314
text_urls:
1415
- http://www.fsf.org/licensing/licenses/agpl-3.0.html
1516
osi_url: http://www.opensource.org/licenses/agpl-v3.html

src/licensedcode/data/licenses/agpl-generic-additional-terms.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ owner: Unspecified
66
notes: this is a generic entry for rare one-off AGPL extra license terms. These are typically
77
additional terms under section 7 of the AGPL-3.0
88
is_exception: yes
9+
is_generic: yes
910
spdx_license_key: LicenseRef-scancode-agpl-generic-additional-terms

src/licensedcode/data/licenses/apache-2.0.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ notes: |
88
Per SPDX.org, this version was released January 2004 This license is OSI
99
certified
1010
spdx_license_key: Apache-2.0
11+
other_spdx_license_keys:
12+
- LicenseRef-Apache
13+
- LicenseRef-Apache-2.0
1114
osi_license_key: Apache-2.0
1215
text_urls:
1316
- http://www.apache.org/licenses/LICENSE-2.0

src/licensedcode/data/licenses/commercial-license.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ short_name: Commercial License
33
name: Commercial License
44
category: Commercial
55
owner: Unspecified
6+
is_generic: yes
7+
notes: this is a generic commercial license
68
spdx_license_key: LicenseRef-scancode-commercial-license

src/licensedcode/data/licenses/commercial-option.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ short_name: Commercial Option
44
name: Commercial Option
55
category: Commercial
66
owner: Unspecified
7+
is_generic: yes
78
notes: replaced by commercial-license

src/licensedcode/data/licenses/generic-cla.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
key: generic-cla
2+
is_generic: yes
23
short_name: Generic CLA
34
name: Prior Generic Contributor License Agreement
45
category: Unstated License

src/licensedcode/data/licenses/generic-exception.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ owner: Unspecified
66
notes: this is a generic license exception notice. Actual terms are most commonly related to
77
rare, one-off extra permission to the A/L/GPL licenses
88
is_exception: yes
9+
is_generic: yes
910
spdx_license_key: LicenseRef-scancode-generic-exception

src/licensedcode/data/licenses/generic-export-compliance.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ category: Unstated License
55
owner: Unspecified
66
notes: this is a generic export compliance notice. Actual terms are most commonly related to
77
cryptography
8+
is_generic: yes
89
spdx_license_key: LicenseRef-scancode-generic-export-compliance

0 commit comments

Comments
 (0)