Skip to content

Commit b3a1192

Browse files
Add --license-diagnostics CLI option
Adding a --license-diagnostics options for showing detection logs, which are now disabled by default. This functionality was added to --license-text-diagnostics previously, but with this commit it is a separate CLI option. Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent a7ee549 commit b3a1192

File tree

10 files changed

+58
-11
lines changed

10 files changed

+58
-11
lines changed

src/licensedcode/detection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ def to_dict(
415415
self,
416416
include_text=False,
417417
license_text_diagnostics=False,
418+
license_diagnostics=False,
418419
whole_lines=True,
419420
):
420421
"""
@@ -425,7 +426,7 @@ def dict_fields(attr, value):
425426
if attr.name == 'file_region':
426427
return False
427428

428-
if attr.name == 'detection_log' and not license_text_diagnostics:
429+
if attr.name == 'detection_log' and not license_diagnostics:
429430
return False
430431

431432
return True
@@ -592,7 +593,7 @@ class UniqueDetection:
592593
files = attr.ib(factory=list)
593594

594595
@classmethod
595-
def get_unique_detections(cls, license_detections, license_text_diagnostics):
596+
def get_unique_detections(cls, license_detections):
596597
"""
597598
Return all unique UniqueDetection from a ``license_detections`` list of
598599
LicenseDetection.
@@ -621,14 +622,14 @@ def get_unique_detections(cls, license_detections, license_text_diagnostics):
621622

622623
return unique_license_detections
623624

624-
def to_dict(self, license_text_diagnostics):
625+
def to_dict(self, license_diagnostics):
625626

626627
def dict_fields(attr, value):
627628

628629
if attr.name == 'files':
629630
return False
630631

631-
if attr.name == 'detection_log' and not license_text_diagnostics:
632+
if attr.name == 'detection_log' and not license_diagnostics:
632633
return False
633634

634635
return True

src/licensedcode/plugin_license.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ class LicenseScanner(ScanPlugin):
104104
help_group=SCAN_OPTIONS_GROUP,
105105
),
106106

107+
PluggableCommandLineOption(('--license-diagnostics',),
108+
is_flag=True,
109+
required_options=['license'],
110+
help='In license detections, include diagnostic details to figure '
111+
'out the license detection post processing steps applied.',
112+
help_group=SCAN_OPTIONS_GROUP,
113+
),
114+
107115
PluggableCommandLineOption(('--license-url-template',),
108116
default=SCANCODE_LICENSEDB_URL, show_default=True,
109117
required_options=['license'],
@@ -137,6 +145,7 @@ def get_scanner(
137145
license_score=0,
138146
license_text=False,
139147
license_text_diagnostics=False,
148+
license_diagnostics=False,
140149
license_url_template=SCANCODE_LICENSEDB_URL,
141150
unknown_licenses=False,
142151
**kwargs
@@ -147,11 +156,12 @@ def get_scanner(
147156
min_score=license_score,
148157
include_text=license_text,
149158
license_text_diagnostics=license_text_diagnostics,
159+
license_diagnostics=license_diagnostics,
150160
license_url_template=license_url_template,
151161
unknown_licenses=unknown_licenses,
152162
)
153163

154-
def process_codebase(self, codebase, license_text_diagnostics, **kwargs):
164+
def process_codebase(self, codebase, license_diagnostics, **kwargs):
155165
"""
156166
Post-process ``codebase`` to follow referenced filenames to license
157167
matches in other files.
@@ -182,7 +192,6 @@ def process_codebase(self, codebase, license_text_diagnostics, **kwargs):
182192
license_detections = collect_license_detections(codebase)
183193
unique_license_detections = UniqueDetection.get_unique_detections(
184194
license_detections=license_detections,
185-
license_text_diagnostics=license_text_diagnostics,
186195
)
187196

188197
if TRACE:
@@ -214,7 +223,7 @@ def process_codebase(self, codebase, license_text_diagnostics, **kwargs):
214223
detections=unique_license_detections,
215224
)
216225
codebase.attributes.license_detections.extend([
217-
unique_detection.to_dict(license_text_diagnostics=license_text_diagnostics)
226+
unique_detection.to_dict(license_diagnostics=license_diagnostics)
218227
for unique_detection in unique_license_detections
219228
])
220229

src/scancode/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def get_licenses(
152152
min_score=0,
153153
include_text=False,
154154
license_text_diagnostics=False,
155+
license_diagnostics=False,
155156
deadline=sys.maxsize,
156157
unknown_licenses=False,
157158
**kwargs,
@@ -204,6 +205,7 @@ def get_licenses(
204205
detection_mapping = detection.to_dict(
205206
include_text=include_text,
206207
license_text_diagnostics=license_text_diagnostics,
208+
license_diagnostics=license_diagnostics,
207209
)
208210
license_clues.extend(detection_mapping["matches"])
209211
else:
@@ -212,6 +214,7 @@ def get_licenses(
212214
detection.to_dict(
213215
include_text=include_text,
214216
license_text_diagnostics=license_text_diagnostics,
217+
license_diagnostics=license_diagnostics,
215218
)
216219
)
217220

tests/licensedcode/test_licenses_reference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_licenses_reference_works_with_matched_text():
3737
test_dir = test_env.get_test_loc('licenses_reference_reporting/scan', copy=True)
3838
result_file = test_env.get_temp_file('json')
3939
args = [
40-
'--license', '--package', '--license-text', '--license-text-diagnostics',
40+
'--license', '--package', '--license-text', '--license-text-diagnostics', '--license-diagnostics',
4141
'--license-references', test_dir, '--json-pp', result_file, '--verbose'
4242
]
4343
run_scan_click(args)
@@ -50,7 +50,7 @@ def test_licenses_reference_works_with_license_clues():
5050
test_dir = test_env.get_test_loc('licenses_reference_reporting/python.LICENSE', copy=True)
5151
result_file = test_env.get_temp_file('json')
5252
args = [
53-
'--license', '--license-text', '--license-text-diagnostics',
53+
'--license', '--license-text', '--license-text-diagnostics', '--license-diagnostics',
5454
'--license-references', test_dir, '--json-pp', result_file, '--verbose'
5555
]
5656
run_scan_click(args)

tests/licensedcode/test_plugin_license.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def test_license_option_reports_license_expressions_spdx_nuget():
6868
'--license',
6969
'--license-text',
7070
'--license-text-diagnostics',
71+
'--license-diagnostics',
7172
'--strip-root',
7273
'--verbose',
7374
'--json', result_file,
@@ -86,6 +87,7 @@ def test_license_option_reports_license_texts():
8687
'--license',
8788
'--license-text',
8889
'--license-text-diagnostics',
90+
'--license-diagnostics',
8991
'--strip-root',
9092
'--verbose',
9193
'--json', result_file,
@@ -104,6 +106,7 @@ def test_license_option_reports_license_texts_diag():
104106
'--license',
105107
'--license-text',
106108
'--license-text-diagnostics',
109+
'--license-diagnostics',
107110
'--strip-root',
108111
'--verbose',
109112
'--json', result_file,
@@ -121,6 +124,7 @@ def test_license_option_reports_license_texts_long_lines():
121124
'--license',
122125
'--license-text',
123126
'--license-text-diagnostics',
127+
'--license-diagnostics',
124128
'--strip-root',
125129
'--verbose',
126130
'--json', result_file,
@@ -138,6 +142,7 @@ def test_license_option_reports_license_texts_diag_long_lines():
138142
'--license',
139143
'--license-text',
140144
'--license-text-diagnostics',
145+
'--license-diagnostics',
141146
'--strip-root',
142147
'--verbose',
143148
'--json', result_file,

tests/licensedcode/test_plugin_license_detection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def test_complicated_license_text_from_ffmpeg():
2727
'--license',
2828
'--license-text',
2929
'--license-text-diagnostics',
30+
'--license-diagnostics',
3031
'--strip-root',
3132
'--verbose',
3233
'--json', result_file,
@@ -44,6 +45,7 @@ def test_license_match_unknown_license_intro_with_imperfect_matches():
4445
'--license',
4546
'--license-text',
4647
'--license-text-diagnostics',
48+
'--license-diagnostics',
4749
'--strip-root',
4850
'--verbose',
4951
'--json', result_file,
@@ -61,6 +63,7 @@ def test_license_match_unknown_license_intro_with_dual_license():
6163
'--license',
6264
'--license-text',
6365
'--license-text-diagnostics',
66+
'--license-diagnostics',
6467
'--strip-root',
6568
'--verbose',
6669
'--json', result_file,
@@ -78,6 +81,7 @@ def test_license_match_unknown_license_intro_eclipse_foundation():
7881
'--license',
7982
'--license-text',
8083
'--license-text-diagnostics',
84+
'--license-diagnostics',
8185
'--strip-root',
8286
'--verbose',
8387
'--json', result_file,
@@ -95,6 +99,7 @@ def test_license_match_unknown_license_intro_eclipse_foundation_tycho():
9599
'--license',
96100
'--license-text',
97101
'--license-text-diagnostics',
102+
'--license-diagnostics',
98103
'--strip-root',
99104
'--verbose',
100105
'--json', result_file,
@@ -112,6 +117,7 @@ def test_license_match_unknown_license_intro_with_long_gaps_between():
112117
'--license',
113118
'--license-text',
114119
'--license-text-diagnostics',
120+
'--license-diagnostics',
115121
'--strip-root',
116122
'--verbose',
117123
'--json', result_file,
@@ -129,6 +135,7 @@ def test_license_match_unknown_license_with_license_ref_to_key_file_at_root():
129135
'--license',
130136
'--license-text',
131137
'--license-text-diagnostics',
138+
'--license-diagnostics',
132139
'--strip-root',
133140
'--verbose',
134141
'--json', result_file,
@@ -146,6 +153,7 @@ def test_license_match_unknown_license_with_license_reference():
146153
'--license',
147154
'--license-text',
148155
'--license-text-diagnostics',
156+
'--license-diagnostics',
149157
'--strip-root',
150158
'--verbose',
151159
'--json', result_file,
@@ -163,6 +171,7 @@ def test_license_match_unknown_license_without_license_reference():
163171
'--license',
164172
'--license-text',
165173
'--license-text-diagnostics',
174+
'--license-diagnostics',
166175
'--strip-root',
167176
'--verbose',
168177
'--json', result_file,
@@ -180,6 +189,7 @@ def test_license_match_referenced_filename():
180189
'--license',
181190
'--license-text',
182191
'--license-text-diagnostics',
192+
'--license-diagnostics',
183193
'--strip-root',
184194
'--verbose',
185195
'--json', result_file,
@@ -197,6 +207,7 @@ def test_license_match_referenced_filename_unknown_ref():
197207
'--license',
198208
'--license-text',
199209
'--license-text-diagnostics',
210+
'--license-diagnostics',
200211
'--strip-root',
201212
'--verbose',
202213
'--json', result_file,
@@ -215,6 +226,7 @@ def test_find_referenced_resource():
215226
'--license',
216227
'--license-text',
217228
'--license-text-diagnostics',
229+
'--license-diagnostics',
218230
'--json', scan_loc,
219231
test_dir,
220232
]
@@ -253,6 +265,7 @@ def test_match_reference_license():
253265
'--license',
254266
'--license-text',
255267
'--license-text-diagnostics',
268+
'--license-diagnostics',
256269
'--json', scan_loc,
257270
test_dir,
258271
]

tests/licensedcode/test_plugin_license_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_is_licensing_works():
2828
test_dir = test_env.get_test_loc('plugin_license_text/scan', copy=True)
2929
result_file = test_env.get_temp_file('json')
3030
args = [
31-
'--license', '--license-text', '--license-text-diagnostics',
31+
'--license', '--license-text', '--license-text-diagnostics', '--license-diagnostics',
3232
'--info',
3333
test_dir, '--json-pp', result_file, '--verbose']
3434
run_scan_click(args)

tests/packagedcode/test_license_detection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_license_reference_detection_in_manifest_unknown():
2626
'--license',
2727
'--license-text',
2828
'--license-text-diagnostics',
29+
'--license-diagnostics',
2930
'--package',
3031
'--strip-root',
3132
'--verbose',
@@ -44,6 +45,7 @@ def test_license_reference_detection_in_manifest_known():
4445
'--license',
4546
'--license-text',
4647
'--license-text-diagnostics',
48+
'--license-diagnostics',
4749
'--package',
4850
'--strip-root',
4951
'--verbose',
@@ -62,6 +64,7 @@ def test_license_reference_detection_in_manifest_licence_comment():
6264
'--license',
6365
'--license-text',
6466
'--license-text-diagnostics',
67+
'--license-diagnostics',
6568
'--package',
6669
'--strip-root',
6770
'--verbose',
@@ -80,6 +83,7 @@ def test_license_reference_detection_in_manifest_siblings():
8083
'--license',
8184
'--license-text',
8285
'--license-text-diagnostics',
86+
'--license-diagnostics',
8387
'--package',
8488
'--strip-root',
8589
'--verbose',
@@ -157,6 +161,7 @@ def test_license_reference_to_unknown_package_complex_package():
157161
'--license',
158162
'--license-text',
159163
'--license-text-diagnostics',
164+
'--license-diagnostics',
160165
'--package',
161166
'--strip-root',
162167
'--verbose',
@@ -174,6 +179,7 @@ def test_license_reference_to_unknown_package_simple():
174179
'--license',
175180
'--license-text',
176181
'--license-text-diagnostics',
182+
'--license-diagnostics',
177183
'--package',
178184
'--strip-root',
179185
'--verbose',
@@ -191,6 +197,7 @@ def test_license_reference_to_unknown_package_with_detected_package():
191197
'--license',
192198
'--license-text',
193199
'--license-text-diagnostics',
200+
'--license-diagnostics',
194201
'--package',
195202
'--strip-root',
196203
'--verbose',
@@ -209,6 +216,7 @@ def test_license_reference_to_unknown_package_paddlenlp():
209216
'--license',
210217
'--license-text',
211218
'--license-text-diagnostics',
219+
'--license-diagnostics',
212220
'--package',
213221
'--strip-root',
214222
'--verbose',
@@ -227,6 +235,7 @@ def test_license_reference_to_unknown_package_without_detected_package():
227235
'--license',
228236
'--license-text',
229237
'--license-text-diagnostics',
238+
'--license-diagnostics',
230239
'--package',
231240
'--strip-root',
232241
'--verbose',
@@ -245,6 +254,7 @@ def test_license_reference_to_unknown_package_special_case_debian():
245254
'--license',
246255
'--license-text',
247256
'--license-text-diagnostics',
257+
'--license-diagnostics',
248258
'--package',
249259
'--strip-root',
250260
'--verbose',

tests/scancode/data/help/help.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Options:
2121
-u, --url Scan <input> for urls.
2222

2323
scan options:
24+
--license-diagnostics In license detections, include diagnostic details
25+
to figure out the license detection post
26+
processing steps applied.
2427
--license-score INTEGER Do not return license matches with a score lower
2528
than this score. A number between 0 and 100.
2629
[default: 0]

tests/scancode/test_cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,10 @@ def test_scan_can_return_matched_license_text():
492492
test_file = test_env.get_test_loc('license_text/test.txt')
493493
expected_file = test_env.get_test_loc('license_text/test.expected')
494494
result_file = test_env.get_temp_file('json')
495-
args = ['--license', '--license-text', '--license-text-diagnostics', '--strip-root', test_file, '--json', result_file]
495+
args = [
496+
'--license', '--license-text', '--license-text-diagnostics', '--license-diagnostics',
497+
'--strip-root', test_file, '--json', result_file
498+
]
496499
run_scan_click(args)
497500
check_json_scan(test_env.get_test_loc(expected_file), result_file, regen=REGEN_TEST_FIXTURES)
498501

0 commit comments

Comments
 (0)