Skip to content

Commit 6053141

Browse files
committed
Create option to set threshold for origin summary
Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 9d02f36 commit 6053141

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/scancode/plugin_summary.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,21 @@ class OriginSummary(PostScanPlugin):
8484
is_flag=True, default=False,
8585
help='Summarize copyright holders and license expressions to the directory level '
8686
'if a copyright holder or license expression is detected in 75% or more of '
87-
'total files in a directory',
87+
'total files in a directory.',
88+
help_group=POST_SCAN_GROUP
89+
),
90+
CommandLineOption(('--origin-summary-threshold',),
91+
is_flag=False, default=False,
92+
help='Set a custom threshold for origin summarization.'
93+
required_options=['origin_summary'],
8894
help_group=POST_SCAN_GROUP
8995
)
9096
]
9197

9298
def is_enabled(self, origin_summary, **kwargs):
9399
return origin_summary
94100

95-
def process_codebase(self, codebase, **kwargs):
101+
def process_codebase(self, codebase, origin_summary_threshold=None, **kwargs):
96102
root = codebase.get_resource(0)
97103
if not hasattr(root, 'copyrights') or not hasattr(root, 'licenses'):
98104
# TODO: Raise warning(?) if these fields are not there
@@ -135,7 +141,7 @@ def process_codebase(self, codebase, **kwargs):
135141
origin, top_count = origin_count.most_common(1)[0]
136142
holders, license_expression = origin
137143
# TODO: Check for contradictions when performing summarizations
138-
if is_majority(top_count, resource.files_count):
144+
if is_majority(top_count, resource.files_count, origin_summary_threshold):
139145
resource.origin_summary['license_expression'] = license_expression
140146
resource.origin_summary['holders'] = holders
141147
resource.origin_summary['count'] = top_count
@@ -172,9 +178,10 @@ def process_codebase(self, codebase, **kwargs):
172178
codebase_summarized_dirs[license_expression] = OrderedDict({holder: sorted_summarized_dirs})
173179

174180

175-
def is_majority(count, files_count):
181+
def is_majority(count, files_count, threshold):
176182
"""
177-
Return True if `count` is 75% or more of `files_count`
183+
Return True if `count` divided by `files_count` is greater than or equal to `threshold`
178184
"""
179185
# TODO: Increase this and test with real codebases
180-
return count / files_count >= 0.75
186+
threshold = threshold or 0.75
187+
return count / files_count >= threshold

0 commit comments

Comments
 (0)