forked from aboutcode-org/scancode-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlicensing.py
More file actions
1000 lines (816 loc) · 34.7 KB
/
licensing.py
File metadata and controls
1000 lines (816 loc) · 34.7 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import logging
import os
from license_expression import Licensing
from licensedcode.cache import build_spdx_license_expression
from licensedcode.cache import get_cache
from licensedcode.detection import LicenseDetection
from licensedcode.detection import DetectionCategory
from licensedcode.detection import group_matches
from licensedcode.detection import get_detected_license_expression
from licensedcode.detection import get_matches_from_detection_mappings
from licensedcode.detection import get_new_identifier_from_detections
from licensedcode.detection import get_unknown_license_detection
from licensedcode.detection import get_referenced_filenames
from licensedcode.detection import find_referenced_resource
from licensedcode.detection import detect_licenses
from licensedcode.detection import LicenseDetectionFromResult
from licensedcode.detection import populate_matches_with_path
from licensedcode.detection import use_referenced_license_expression
from licensedcode.spans import Span
from licensedcode import query
from packagedcode.utils import combine_expressions
from summarycode.classify import check_resource_name_start_and_end
from summarycode.classify import LEGAL_STARTS_ENDS
from summarycode.classify import README_STARTS_ENDS
import saneyaml
"""
Detect and normalize licenses as found in package manifests data.
"""
TRACE = os.environ.get('SCANCODE_DEBUG_PACKAGE_LICENSE', False)
def logger_debug(*args):
pass
logger = logging.getLogger(__name__)
if TRACE:
import sys
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args))
RESOURCE_TO_PACKAGE_LICENSE_FIELDS = {
'detected_license_expression': 'declared_license_expression',
'detected_license_expression_spdx': 'declared_license_expression_spdx',
'license_detections': 'license_detections',
}
def add_referenced_license_matches_for_package(resource, codebase):
"""
Return an updated ``resource`` saving it in place, after adding new license
detections to the package manifests detected in this resource, following their
Rule ``referenced_filenames`` if any. Return None if ``resource`` is not a
file Resource or was not updated.
"""
if TRACE:
logger_debug(
f'packagedcode.licensing: add_referenced_license_matches_for_package: '
'resource: {resource.path}'
)
if not resource.is_file:
return
package_data = resource.package_data
if not package_data:
return
for pkg in package_data:
license_detection_mappings = pkg["license_detections"]
if not license_detection_mappings:
continue
modified = False
for license_detection_mapping in license_detection_mappings:
license_detection_object = LicenseDetectionFromResult.from_license_detection_mapping(
license_detection_mapping=license_detection_mapping,
file_path=resource.path,
)
detections_added = []
license_match_mappings = license_detection_mapping["matches"]
referenced_filenames = get_referenced_filenames(license_detection_object.matches)
if not referenced_filenames:
continue
referenced_detections = []
for referenced_filename in referenced_filenames:
referenced_resource = find_referenced_resource(
referenced_filename=referenced_filename,
resource=resource,
codebase=codebase,
)
if referenced_resource and referenced_resource.license_detections:
referenced_detections.extend(
referenced_resource.license_detections
)
# For LicenseMatches with different resources as origin, add the
# resource path to these matches as origin info
for detection in referenced_resource.license_detections:
populate_matches_with_path(
matches=detection["matches"],
path=referenced_resource.path
)
referenced_license_expression = combine_expressions(
expressions=[
detection["license_expression"]
for detection in referenced_detections
],
)
if not use_referenced_license_expression(
referenced_license_expression=referenced_license_expression,
license_detection=license_detection_object,
):
continue
modified = True
detections_added.extend(referenced_resource.license_detections)
matches_to_extend = get_matches_from_detection_mappings(
license_detections=referenced_resource.license_detections,
)
license_match_mappings.extend(matches_to_extend)
detection_log, license_expression = get_detected_license_expression(
license_match_mappings=license_match_mappings,
analysis=DetectionCategory.PACKAGE_UNKNOWN_FILE_REFERENCE_LOCAL.value,
post_scan=True,
)
license_expression_spdx = build_spdx_license_expression(
license_expression=str(license_expression),
licensing=get_cache().licensing,
)
license_detection_mapping["license_expression"] = str(license_expression)
license_detection_mapping["license_expression_spdx"] = str(license_expression_spdx)
license_detection_mapping["detection_log"] = detection_log
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
initial_detection=license_detection_mapping,
detections_added=detections_added,
license_expression=license_expression,
)
if modified:
license_expressions = [
detection["license_expression"]
for detection in license_detection_mappings
]
pkg["declared_license_expression"] = combine_expressions(
expressions=license_expressions,
relation='AND',
unique=True,
)
pkg["declared_license_expression_spdx"] = str(build_spdx_license_expression(
license_expression=pkg["declared_license_expression"],
licensing=get_cache().licensing,
))
codebase.save_resource(resource)
yield resource
def add_referenced_license_detection_from_package(resource, codebase):
"""
Return an updated ``resource`` saving it in place, after adding new license
matches (licenses and license_expressions) following their Rule
``referenced_filenames`` if it is pointing to a package. If there is no
top level packages, check for License/Readme files at codebase root and
add licenses from there.
"""
if TRACE:
logger_debug(f'packagedcode.licensing: add_referenced_license_matches_from_package: resource: {resource.path}')
if not resource.is_file:
return
license_detection_mappings = resource.license_detections
if not license_detection_mappings:
return
codebase_packages = codebase.attributes.packages
modified = False
for license_detection_mapping in license_detection_mappings:
license_detection_object = LicenseDetectionFromResult.from_license_detection_mapping(
license_detection_mapping=license_detection_mapping,
file_path=resource.path,
)
detection_modified = False
license_match_mappings = license_detection_mapping["matches"]
detections_added = []
referenced_filenames = get_referenced_filenames(license_matches=license_detection_object.matches)
if not referenced_filenames:
continue
has_reference_to_package = any([
'INHERIT_LICENSE_FROM_PACKAGE' in referenced_filename
for referenced_filename in referenced_filenames
])
if not has_reference_to_package:
continue
if not codebase_packages:
root_path = codebase.root.path
root_resource = codebase.get_resource(path=root_path)
sibling_license_detections, _le = get_license_detections_from_sibling_file(
resource=root_resource,
codebase=codebase,
)
if TRACE:
logger_debug(
f'packagedcode.licensing: add_referenced_license_matches_from_package: root_path: {root_path}'
f'sibling_license_detections: {sibling_license_detections}'
)
referenced_license_expression = combine_expressions(
expressions=[
detection["license_expression"]
for detection in sibling_license_detections
],
)
if not use_referenced_license_expression(
referenced_license_expression=referenced_license_expression,
license_detection=license_detection_object,
):
continue
for sibling_detection in sibling_license_detections:
modified = True
detection_modified = True
license_match_mappings.extend(sibling_detection["matches"])
detections_added.append(sibling_detection)
analysis = DetectionCategory.UNKNOWN_REFERENCE_IN_FILE_TO_NONEXISTENT_PACKAGE.value
else:
for_packages = resource.for_packages
for package_uid in for_packages:
for codebase_package in codebase_packages:
if codebase_package["package_uid"] == package_uid:
break
pkg_detections = codebase_package["license_detections"]
if not pkg_detections:
continue
referenced_license_expression = combine_expressions(
expressions=[
detection["license_expression"]
for detection in pkg_detections
],
)
if not use_referenced_license_expression(
referenced_license_expression=referenced_license_expression,
license_detection=license_detection_object,
):
continue
for pkg_detection in pkg_detections:
modified = True
detection_modified = True
populate_matches_with_path(
matches=pkg_detection["matches"],
path=resource.path
)
license_match_mappings.extend(pkg_detection["matches"])
detections_added.append(pkg_detection)
analysis = DetectionCategory.UNKNOWN_REFERENCE_IN_FILE_TO_PACKAGE.value
if not detection_modified:
continue
detection_log, license_expression = get_detected_license_expression(
license_match_mappings=license_match_mappings,
analysis=analysis,
post_scan=True,
)
license_expression_spdx = build_spdx_license_expression(
license_expression=str(license_expression),
licensing=get_cache().licensing,
)
license_detection_mapping["license_expression"] = str(license_expression)
license_detection_mapping["license_expression_spdx"] = str(license_expression_spdx)
license_detection_mapping["detection_log"] = detection_log
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
initial_detection=license_detection_mapping,
detections_added=detections_added,
license_expression=license_expression,
)
if modified:
license_expressions = [
detection["license_expression"]
for detection in license_detection_mappings
]
resource.detected_license_expression = combine_expressions(
expressions=license_expressions,
relation='AND',
unique=True,
)
resource.detected_license_expression_spdx = str(build_spdx_license_expression(
license_expression=resource.detected_license_expression,
licensing=get_cache().licensing,
))
codebase.save_resource(resource)
yield resource
def add_license_from_sibling_file(resource, codebase):
"""
Given a resource and it's codebase object, assign licenses to the package
detections in that resource, from the sibling files of it.
"""
if TRACE:
logger_debug(f'packagedcode.licensing: add_license_from_sibling_file: resource: {resource.path}')
if not resource.is_file:
return
package_data = resource.package_data
if not package_data:
return
for pkg in package_data:
pkg_license_detections = pkg["license_detections"]
if pkg_license_detections:
return
license_detections, license_expression = get_license_detections_from_sibling_file(
resource=resource,
codebase=codebase,
)
if not license_detections:
return
package = resource.package_data[0]
package["license_detections"] = license_detections
package["declared_license_expression"] = license_expression
package["declared_license_expression_spdx"] = str(build_spdx_license_expression(
license_expression=package["declared_license_expression"],
licensing=get_cache().licensing,
))
codebase.save_resource(resource)
return package
def is_legal_or_readme(resource):
"""
Return True if `resource` is a legal (like LICENSE/COPYING) or README file, otherwise
return False.
"""
is_legal = check_resource_name_start_and_end(resource=resource, STARTS_ENDS=LEGAL_STARTS_ENDS)
is_readme = check_resource_name_start_and_end(resource=resource, STARTS_ENDS=README_STARTS_ENDS)
if is_legal or is_readme:
return True
return False
def get_license_detections_from_sibling_file(resource, codebase):
"""
Return `license_detections`, a list of LicenseDetection objects and a
`license_expression`, given a resource and it's codebase object, from
the sibling files of the resource.
"""
siblings = []
if resource.has_parent():
for sibling in resource.siblings(codebase):
if is_legal_or_readme(resource=sibling):
siblings.append(sibling)
elif resource.has_children:
for child in resource.children(codebase):
if is_legal_or_readme(resource=child):
siblings.append(child)
if not siblings:
return [], None
license_detections = []
for sibling in siblings:
for detection in sibling.license_detections:
populate_matches_with_path(
matches=detection["matches"],
path=sibling.path
)
license_detections.extend(sibling.license_detections)
if not license_detections:
return [], None
license_expression = get_license_expression_from_detection_mappings(
detections=license_detections,
valid_expression=False,
)
return license_detections, license_expression
def get_license_detection_mappings(
location,
index=None,
analysis=None,
post_scan=False,
package_license=True,
):
"""
Return a list of LicenseDetection mappings by running license detection on
the file at ``location``. This performs license detection the same way as
with `scancode.api.get_licenses`.
"""
license_detections = []
detections = detect_licenses(
index=index,
location=location,
analysis=analysis,
post_scan=post_scan,
package_license=package_license,
)
for detection in detections:
# TODO: also return these?
# FIXME: How can we ever get a license detection WITHOUT an expression??
if detection.license_expression is None:
continue
# FIXME: why not honoring the arguments for include_text and license_text_diagnostics?
license_detections.append(
detection.to_dict(
include_text=True,
license_text_diagnostics=False,
)
)
return license_detections
def get_declared_license_expression_spdx(declared_license_expression):
"""
Return a string SPDX license expression corresponding to the scancode
`declared_license_expression` string.
"""
if not declared_license_expression:
return
detected_license_expression_spdx = build_spdx_license_expression(
declared_license_expression,
licensing=get_cache().licensing,
)
return str(detected_license_expression_spdx)
def get_license_matches(location=None, query_string=None):
"""
Returns a sequence of LicenseMatch objects from license detection of the
`query_string` or the file at `location`.
"""
if TRACE:
logger_debug('get_license_matches: location:', location)
if not query_string and not location:
return []
from licensedcode import cache
idx = cache.get_index()
matches = idx.match(location=location, query_string=query_string)
if TRACE:
logger_debug('get_license_matches: matches:', matches)
return matches
def get_license_matches_from_query_string(query_string, start_line=1):
"""
Returns a sequence of LicenseMatch objects from license detection of the
`query_string` starting at ``start_line`` number. This is useful when
matching a text fragment alone when it is part of a larger text.
"""
if not query_string:
return []
from licensedcode import cache
idx = cache.get_index()
qry = query.build_query(
query_string=query_string,
idx=idx,
start_line=start_line,
)
return idx.match_query(qry=qry)
def get_license_expression_from_matches(license_matches, relation='AND', unique=True):
"""
Craft a license expression from `license_matches`, a list of LicenseMatch objects.
"""
if not license_matches:
return
if type(license_matches[0]) == dict:
license_expressions = [
match['license_expression'] for match in license_matches
]
else:
license_expressions = [
match.rule.license_expression for match in license_matches
]
if len(license_expressions) == 1:
license_expression = str(license_expressions[0])
else:
license_expression = str(
combine_expressions(license_expressions, relation=relation, unique=unique)
)
return license_expression
def get_license_expression_from_detection_mappings(
detections,
relation='AND',
unique=True,
valid_expression=False,
):
"""
Return a license expression string from a list of LicenseDetection
mappings.
If `valid_expression` is True, also include the license_expression
from detected license clues.
"""
expressions = []
for detection in detections:
if valid_expression:
if not detection["license_expression"]:
expressions.append(
get_license_expression_from_matches(detection["matches"])
)
continue
expressions.append(detection["license_expression"])
return str(
combine_expressions(expressions, relation=relation, unique=unique)
)
def matches_have_unknown(matches, licensing=Licensing()):
"""
Return True if any of the LicenseMatch in `matches` has an unknown license.
"""
for match in matches:
exp = match.rule.license_expression_object
if any(
key in ('unknown', 'unknown-spdx')
for key in licensing.license_keys(exp)
):
return True
def get_license_detections_from_matches(matches):
"""
Return a list of LicenseDetection objects given a list of LicenseMatch objects.
"""
license_detections = []
if not matches:
return license_detections
for group_of_matches in group_matches(license_matches=matches):
license_detections.append(
LicenseDetection.from_matches(
matches=group_of_matches,
package_license=True,
)
)
return license_detections
def get_license_expression_from_detections(license_detections, relation='AND', unique=True):
"""
Return a license expression string built from a list of LicenseDetection objects.
"""
if not license_detections:
return
license_expressions = [
detection.license_expression for detection in license_detections
]
if len(license_expressions) == 1:
license_expression = str(license_expressions[0])
else:
license_expression = str(
combine_expressions(license_expressions, relation=relation, unique=unique)
)
return license_expression
def get_mapping_and_expression_from_detections(
license_detections,
relation='AND',
unique=True,
include_text=True,
license_text_diagnostics=False,
whole_lines=True,
):
"""
Return a list of LicenseDetection data mapping and a license_expression
from a list of LicenseDetection objects.
"""
detection_data = []
if not license_detections:
return detection_data, None
for license_detection in license_detections:
if license_detection.license_expression is None:
license_detection.license_expression = get_license_expression_from_matches(
license_matches=license_detection.matches,
relation=relation,
unique=unique,
)
detection_data.append(
license_detection.to_dict(
include_text=include_text,
license_text_diagnostics=license_text_diagnostics,
whole_lines=whole_lines,
)
)
license_expression = get_license_expression_from_detections(
license_detections=license_detections,
relation=relation,
unique=unique,
)
return detection_data, license_expression
def is_declared_license_not_fully_matched(matches):
"""
verify that we consumed 100% of the query string e.g. that we
have no unknown leftover.
# 1. have all matches 100% coverage?
# 2. is declared license fully matched?
"""
all_matches_have_full_coverage = all(m.coverage() == 100 for m in matches)
query = matches[0].query
# the query object should be the same for all matches. Is this always true??
for mt in matches:
if mt.query != query:
# FIXME: the expception may be swallowed in callers!!!
raise Exception(
'Inconsistent package.extracted_license_statement: text with multiple "queries".'
'Please report this issue to the scancode-toolkit team.\n'
f'{matches}'
)
query_len = len(query.tokens)
matched_qspans = [m.qspan for m in matches]
matched_qpositions = Span.union(*matched_qspans)
len_all_matches = len(matched_qpositions)
declared_license_is_fully_matched = query_len == len_all_matches
if not declared_license_is_fully_matched and not all_matches_have_full_coverage:
return True
return False
def get_normalized_license_detections(
extracted_license,
try_as_expression=True,
approximate=True,
expression_symbols=None,
):
"""
Return a normalized license expression string detected from a list of
declared license items.
"""
license_detections = []
if not extracted_license:
return license_detections
if TRACE:
logger_debug(f'get_normalized_license_detections: extracted_license: {extracted_license}')
logger_debug(f'get_normalized_license_detections: type(extracted_license): {type(extracted_license)}')
if not isinstance(extracted_license, list):
if isinstance(extracted_license, str):
license_detections = get_license_detections_for_extracted_license_statement(
extracted_license_statement=extracted_license,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if TRACE:
logger_debug(f'get_normalized_license_detections: str:')
elif isinstance(extracted_license, dict):
for extracted_license_statement in extracted_license.values():
detections = get_license_detections_for_extracted_license_statement(
extracted_license_statement=extracted_license_statement,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if TRACE:
logger_debug(f'get_normalized_license_detections: dict: extracted_license_statement: {extracted_license_statement}: detections: {detections}')
if detections:
license_detections.extend(detections)
if not license_detections:
unknown_dict_object = repr(dict(extracted_license.items()))
unknown_detection = get_unknown_license_detection(query_string=unknown_dict_object)
license_detections.append(unknown_detection)
if TRACE:
logger_debug(f'get_normalized_license_detections: dict: unknown_dict_object: {unknown_dict_object}, unknown_detection: {saneyaml.dump(unknown_detection.to_dict())}')
else:
extracted_license_statement = saneyaml.dump(extracted_license)
license_detections = get_license_detections_for_extracted_license_statement(
extracted_license_statement=extracted_license_statement,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if TRACE:
logger_debug(f'get_normalized_license_detections: dict:')
elif isinstance(extracted_license, list):
for extracted_license_item in extracted_license:
if isinstance(extracted_license_item, str):
detections = get_license_detections_for_extracted_license_statement(
extracted_license_statement=extracted_license_item,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if detections:
license_detections.extend(detections)
if TRACE:
logger_debug(f'get_normalized_license_detections: list(str): extracted_license_item: {extracted_license_item}: detections: {license_detections}')
elif isinstance(extracted_license_item, dict):
for extracted_license_statement in extracted_license_item.values():
detections = get_license_detections_for_extracted_license_statement(
extracted_license_statement=extracted_license_statement,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if TRACE:
logger_debug(f'get_normalized_license_detections: list(dict): extracted_license_statement: {extracted_license_statement}: detections: {detections}')
if detections:
license_detections.extend(detections)
else:
extracted_license_statement = saneyaml.dump(extracted_license_item)
detections = get_license_detections_for_extracted_license_statement(
extracted_license_statement=extracted_license_statement,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if detections:
license_detections.extend(detections)
if TRACE:
logger_debug(f'get_normalized_license_detections: list(other): extracted_license_statement: {extracted_license_statement}: detections: {license_detections}')
return license_detections
def get_license_detections_and_expression(
extracted_license_statement,
default_relation_license=None,
try_as_expression=True,
approximate=True,
expression_symbols=None,
datasource_id = None,
):
"""
Given a text `extracted_license_statement` return a list of LicenseDetection objects.
`extracted_license_statement` is typically found in package manifests.
If `try_as_expression` is True try first to parse this as a license
expression using the ``expression_symbols`` mapping of {lowered key:
LicenseSymbol} if provided. Otherwise use the standard SPDX license symbols.
If `approximate` is True, also include approximate license detection as
part of the matching procedure.
Return None if the `query_string` is empty. Return "unknown" as a license
expression if there is a `query_string` but nothing was detected.
"""
from packagedcode import PACKAGE_DATA_CLASS_BY_DATASOURCE_ID
detection_data = []
license_expression = None
if not extracted_license_statement:
return detection_data, license_expression
package_data_class = PACKAGE_DATA_CLASS_BY_DATASOURCE_ID.get(datasource_id)
if package_data_class:
license_detections = package_data_class.get_license_detections_for_extracted_license_statement(
extracted_license=extracted_license_statement,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
else:
license_detections = get_normalized_license_detections(
extracted_license=extracted_license_statement,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if not license_detections:
if not isinstance(extracted_license_statement, str):
extracted_license_statement = saneyaml.dump(extracted_license_statement)
license_detection = get_unknown_license_detection(query_string=extracted_license_statement)
license_detections = [license_detection]
if default_relation_license:
relation = default_relation_license
else:
relation = 'AND'
return get_mapping_and_expression_from_detections(
license_detections=license_detections,
relation=relation,
)
def get_license_detections_for_extracted_license_statement(
extracted_license_statement,
try_as_expression=True,
approximate=True,
expression_symbols=None,
):
"""
Return a list of LicenseDetection objects after detecting licenses in
the given `extracted_license_statement`.
"""
if not extracted_license_statement:
return []
if not isinstance(extracted_license_statement, str):
extracted_license_statement = saneyaml.dump(extracted_license_statement)
matches, matched_as_expression = get_license_matches_for_extracted_license_statement(
query_string=extracted_license_statement,
try_as_expression=try_as_expression,
approximate=approximate,
expression_symbols=expression_symbols,
)
if not matches:
extracted_license_statement = f'license {extracted_license_statement}'
matches, matched_as_expression = get_license_matches_for_extracted_license_statement(
query_string=extracted_license_statement,
try_as_expression=False,
approximate=approximate,
expression_symbols=expression_symbols,
)
if not matches:
return []
license_detections = get_license_detections_from_matches(matches)
if matched_as_expression:
return license_detections
if is_declared_license_not_fully_matched(matches):
# FIXME: Only include the undetected part in the matched_text
license_detections.append(
get_unknown_license_detection(extracted_license_statement)
)
return license_detections
def get_license_matches_for_extracted_license_statement(
query_string,
try_as_expression=True,
approximate=True,
expression_symbols=None,
):
"""
Return `matches` list of LicenseMatch and a flag `matched_as_expression`
which is True if the `query_string` was matched as an expression.
Here the `query_string` is generally a extracted_license_statement
which can be a valid license-expression.
"""
from licensedcode.cache import get_index
idx = get_index()
# we match twice in a cascade: as an expression, then as plain text if we
# did not succeed.
matches = None
if try_as_expression:
try:
matched_as_expression = True
matches = idx.match(
query_string=query_string,
as_expression=True,
expression_symbols=expression_symbols,
)
if matches_have_unknown(matches):
# rematch also if we have unknowns
matched_as_expression = False
matches = idx.match(
query_string=query_string,
as_expression=False,
approximate=approximate,
)
except Exception:
matched_as_expression = False
matches = idx.match(
query_string=query_string,
as_expression=False,
approximate=approximate,
)
else:
matched_as_expression = False
matches = idx.match(
query_string=query_string,
as_expression=False,
approximate=approximate,
)
return matches, matched_as_expression
def get_only_expression_from_extracted_license(extracted_license_statement):
"""
Return a license_expression from license detections in a given
`extracted_license_statement` string.
"""
_detections, license_expression = get_license_detections_and_expression(
extracted_license_statement=extracted_license_statement
)
return license_expression