Skip to content

Commit 40921ed

Browse files
authored
Merge pull request #1285 from nexB/211-scan-headers
Add new "headers" top level attribute
2 parents 466ed6e + 4083e72 commit 40921ed

112 files changed

Lines changed: 2004 additions & 1970 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/add_spdx_keys_as_rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def add_spdx_key_rules():
221221
222222
"""
223223
by_key = load_licenses(with_deprecated=True)
224-
by_spdx_key = synclic.get_by_spdx(by_key.values(), include_other=True)
224+
by_spdx_key = synclic.get_licenses_by_spdx_key(by_key.values(), include_other=True)
225225

226226
click.echo('Checking all SPDX ids.')
227227
# first accumulate non-matches

etc/scripts/synclic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ class ScanCodeLicenses(object):
7676

7777
def __init__(self):
7878
self.by_key = load_licenses(with_deprecated=True)
79-
self.by_spdx_key = get_by_spdx(self.by_key.values())
79+
self.by_spdx_key = get_licenses_by_spdx_key(self.by_key.values())
8080

8181
# TODO: not yet used
8282
foreign_dir = join(licensedcode.models.data_dir, 'non-english', 'licenses')
8383
self.non_english_by_key = load_licenses(foreign_dir, with_deprecated=True)
84-
self.non_english_by_spdx_key = get_by_spdx(self.non_english_by_key.values())
84+
self.non_english_by_spdx_key = get_licenses_by_spdx_key(self.non_english_by_key.values())
8585

8686
def clean(self):
8787
"""
@@ -105,7 +105,7 @@ def _clean(licenses):
105105
_clean(lics)
106106

107107

108-
def get_by_spdx(licenses, include_other=False):
108+
def get_licenses_by_spdx_key(licenses, include_other=False):
109109
"""
110110
Return a mapping of {spdx_key: license object} given a sequence of License objects.
111111
"""
@@ -966,9 +966,9 @@ def synchronize_licenses(scancode_licenses, external_source, use_spdx_key=False,
966966

967967
if use_spdx_key:
968968
scancodes_by_key = scancode_licenses.by_spdx_key
969-
externals_by_key = get_by_spdx(externals_by_key.values())
969+
externals_by_key = get_licenses_by_spdx_key(externals_by_key.values())
970970

971-
externals_by_spdx_key = get_by_spdx(externals_by_key.values())
971+
externals_by_spdx_key = get_licenses_by_spdx_key(externals_by_key.values())
972972

973973
# track changes with sets of license keys
974974
same = set()

src/formattedcode/output_csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def is_enabled(self, csv, **kwargs):
5454
return csv
5555

5656
def process_codebase(self, codebase, csv, **kwargs):
57-
results = self.get_results(codebase, **kwargs)
57+
results = self.get_files(codebase, **kwargs)
5858
write_csv(results, csv)
5959

6060

src/formattedcode/output_html.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
from commoncode.fileutils import fsencode
5151
from commoncode.fileutils import parent_directory
5252
from commoncode.system import on_linux
53-
from formattedcode.utils import get_headings
5453
from plugincode.output import output_impl
5554
from plugincode.output import OutputPlugin
5655
from scancode import CommandLineOption
@@ -81,8 +80,8 @@ def is_enabled(self, html, **kwargs):
8180
return html
8281

8382
def process_codebase(self, codebase, html, **kwargs):
84-
results = self.get_results(codebase, **kwargs)
85-
_files_count, version, _notice, _scan_start, _options = get_headings(codebase)
83+
results = self.get_files(codebase, **kwargs)
84+
version = codebase.get_or_create_current_header().tool_version
8685
write_templated(html, results, version, template_or_format='html')
8786

8887

@@ -114,8 +113,8 @@ def is_enabled(self, custom_output, custom_template, **kwargs):
114113
return custom_output and custom_template
115114

116115
def process_codebase(self, codebase, custom_output, custom_template, **kwargs):
117-
results = self.get_results(codebase, **kwargs)
118-
_files_count, version, _notice, _start, _options = get_headings(codebase)
116+
results = self.get_files(codebase, **kwargs)
117+
version = codebase.get_or_create_current_header().tool_version
119118

120119
if on_linux:
121120
custom_template = fsencode(custom_template)
@@ -143,8 +142,8 @@ def is_enabled(self, html_app, **kwargs):
143142
return html_app
144143

145144
def process_codebase(self, codebase, input, html_app, **kwargs): # NOQA
146-
results = self.get_results(codebase, **kwargs)
147-
_files_count, version, _notice, _start, _options = get_headings(codebase)
145+
results = self.get_files(codebase, **kwargs)
146+
version = codebase.get_or_create_current_header().tool_version
148147
html_app.write(as_html_app(html_app, input, version))
149148
create_html_app_assets(results, html_app)
150149

src/formattedcode/output_json.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import simplejson
3131

32-
from formattedcode.utils import get_headings
3332
from plugincode.output import output_impl
3433
from plugincode.output import OutputPlugin
3534
from scancode import CommandLineOption
@@ -77,8 +76,8 @@ def is_enabled(self, output_json, **kwargs):
7776
return output_json
7877

7978
def process_codebase(self, codebase, output_json, **kwargs):
80-
results = self.get_results(codebase, **kwargs)
81-
write_json(codebase, results, output_file=output_json, pretty=False)
79+
files = self.get_files(codebase, **kwargs)
80+
write_json(codebase, files, output_file=output_json, pretty=False)
8281

8382

8483
@output_impl
@@ -97,36 +96,29 @@ def is_enabled(self, output_json_pp, **kwargs):
9796
return output_json_pp
9897

9998
def process_codebase(self, codebase, output_json_pp, **kwargs):
100-
results = self.get_results(codebase, **kwargs)
101-
write_json(codebase, results, output_file=output_json_pp, pretty=True)
99+
files = self.get_files(codebase, **kwargs)
100+
write_json(codebase, files, output_file=output_json_pp, pretty=True, **kwargs)
102101

103102

104-
def write_json(codebase, results, output_file,
103+
def write_json(codebase, files, output_file,
105104
include_summary=False, include_score=False,
106-
pretty=False):
105+
pretty=False, **kwargs):
106+
# NOTE: we write as binary, not text
107107

108-
files_count, version, notice, scan_start, options = get_headings(codebase)
109-
110-
scan = OrderedDict([
111-
('scancode_notice', notice),
112-
('scancode_version', version),
113-
('scancode_options', options),
114-
('scan_start', scan_start),
115-
('files_count', files_count),
116-
# FIXME: we are missing top level codebase ERRORs!!!
117-
])
108+
codebase.add_files_count_to_current_header()
109+
scan = OrderedDict([(b'headers', codebase.get_headers()), ])
118110

119111
# add codebase toplevel attributes such as summaries
120112
if codebase.attributes:
121113
scan.update(codebase.attributes.to_dict())
122114

123115
if TRACE:
124-
logger_debug('write_json: results')
125-
results = list(results)
116+
logger_debug('write_json: files')
117+
files = list(files)
126118
from pprint import pformat
127-
logger_debug(pformat(results))
119+
logger_debug(pformat(files))
128120

129-
scan['files'] = results
121+
scan[b'files'] = files
130122

131123
kwargs = dict(iterable_as_array=True, encoding='utf-8')
132124
if pretty:

src/formattedcode/output_jsonlines.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
2525
from __future__ import absolute_import
2626
from __future__ import unicode_literals
2727

28-
from collections import OrderedDict
29-
3028
import simplejson
3129

32-
from formattedcode.utils import get_headings
3330
from plugincode.output import output_impl
3431
from plugincode.output import OutputPlugin
3532
from scancode import CommandLineOption
@@ -57,32 +54,31 @@ def is_enabled(self, output_json_lines, **kwargs):
5754
return output_json_lines
5855

5956
def process_codebase(self, codebase, output_json_lines, **kwargs):
60-
results = self.get_results(codebase, **kwargs)
61-
files_count, version, notice, scan_start, options = get_headings(codebase)
57+
#NOTE: we write as binary, not text
58+
files = self.get_files(codebase, **kwargs)
59+
60+
codebase.add_files_count_to_current_header()
6261

63-
header = dict(header=OrderedDict([
64-
('scancode_notice', notice),
65-
('scancode_version', version),
66-
('scancode_options', options),
67-
('scan_start', scan_start),
68-
('files_count', files_count)
69-
]))
62+
headers = dict(headers=codebase.get_headers())
7063

71-
kwargs = dict(
64+
simplejson_kwargs = dict(
7265
iterable_as_array=True,
7366
encoding='utf-8',
7467
separators=(b',', b':',)
7568
)
76-
output_json_lines.write(simplejson.dumps(header, **kwargs))
69+
output_json_lines.write(
70+
simplejson.dumps(headers, **simplejson_kwargs))
7771
output_json_lines.write(b'\n')
7872

7973
for name, value in codebase.attributes.to_dict().items():
8074
if value:
8175
smry = {name: value}
82-
output_json_lines.write(simplejson.dumps(smry, **kwargs))
76+
output_json_lines.write(
77+
simplejson.dumps(smry, **simplejson_kwargs))
8378
output_json_lines.write(b'\n')
8479

85-
for scanned_file in results:
86-
scanned_file_line = {'files': [scanned_file]}
87-
output_json_lines.write(simplejson.dumps(scanned_file_line, **kwargs))
80+
for scanned_file in files:
81+
scanned_file_line = {b'files': [scanned_file]}
82+
output_json_lines.write(
83+
simplejson.dumps(scanned_file_line, **simplejson_kwargs))
8884
output_json_lines.write(b'\n')

src/formattedcode/output_spdx.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from spdx.utils import SPDXNone
4747
from spdx.version import Version
4848

49-
from formattedcode.utils import get_headings
5049
from plugincode.output import output_impl
5150
from plugincode.output import OutputPlugin
5251
from scancode import CommandLineOption
@@ -101,12 +100,12 @@ def _patch_license_list():
101100
from spdx.config import LICENSE_MAP
102101
from licensedcode.models import load_licenses
103102
licenses = load_licenses(with_deprecated=True)
104-
spdx_licenses = get_by_spdx(licenses.values())
103+
spdx_licenses = get_licenses_by_spdx_key(licenses.values())
105104
LICENSE_MAP.update(spdx_licenses)
106105
_spdx_list_is_patched = True
107106

108107

109-
def get_by_spdx(licenses):
108+
def get_licenses_by_spdx_key(licenses):
110109
"""
111110
Return a mapping of {spdx_key: license object} given a sequence of License
112111
objects.
@@ -149,9 +148,13 @@ def is_enabled(self, spdx_tv, info, **kwargs):
149148

150149
def process_codebase(self, codebase, spdx_tv, **kwargs):
151150
input = kwargs.get('input', '') # NOQA
152-
results = self.get_results(codebase, **kwargs)
153-
_files_count, version, notice, _scan_start, _options = get_headings(codebase)
154-
write_spdx(spdx_tv, results, version, notice, input, as_tagvalue=True)
151+
files = self.get_files(codebase, **kwargs)
152+
header = codebase.get_or_create_current_header()
153+
tool_name = header.tool_name
154+
tool_version = header.tool_version
155+
notice = header.notice
156+
write_spdx(
157+
spdx_tv, files, tool_name, tool_version, notice, input, as_tagvalue=True)
155158

156159

157160
@output_impl
@@ -171,13 +174,16 @@ def is_enabled(self, spdx_rdf, info, **kwargs):
171174

172175
def process_codebase(self, codebase, spdx_rdf, **kwargs):
173176
input = kwargs.get('input', '') # NOQA
174-
results = self.get_results(codebase, **kwargs)
175-
_files_count, version, notice, _scan_start, _options = get_headings(codebase)
176-
write_spdx(spdx_rdf, results, version, notice, input, as_tagvalue=False)
177+
files = self.get_files(codebase, **kwargs)
178+
header = codebase.get_or_create_current_header()
179+
tool_name = header.tool_name
180+
tool_version = header.tool_version
181+
notice = header.notice
182+
write_spdx(
183+
spdx_rdf, files, tool_name, tool_version, notice, input, as_tagvalue=False)
177184

178185

179-
def write_spdx(output_file, results, scancode_version, scancode_notice,
180-
input_file, as_tagvalue=True):
186+
def write_spdx(output_file, files, tool_name, tool_version, notice, input_file, as_tagvalue=True):
181187
"""
182188
Write scan output as SPDX Tag/value or RDF.
183189
"""
@@ -191,9 +197,9 @@ def write_spdx(output_file, results, scancode_version, scancode_notice,
191197
input_path = dirname(absinput)
192198

193199
doc = Document(Version(2, 1), License.from_identifier('CC0-1.0'))
194-
doc.comment = scancode_notice
195-
196-
doc.creation_info.add_creator(Tool('ScanCode ' + scancode_version))
200+
doc.comment = notice
201+
tool_name = tool_name or 'ScanCode'
202+
doc.creation_info.add_creator(Tool(tool_name + ' ' + tool_version))
197203
doc.creation_info.set_created_now()
198204

199205
package = doc.package = Package(
@@ -208,7 +214,7 @@ def write_spdx(output_file, results, scancode_version, scancode_notice,
208214
all_files_have_no_copyright = True
209215

210216
# FIXME: this should walk the codebase instead!!!
211-
for file_data in results:
217+
for file_data in files:
212218
# Construct the absolute path in case we need to access the file
213219
# to calculate its SHA1.
214220
file_entry = File(join(input_path, file_data.get('path')))

src/formattedcode/utils.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/plugincode/output.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ def process_codebase(self, codebase, output, **kwargs):
9191
raise NotImplementedError
9292

9393
@classmethod
94-
def get_results(cls, codebase, **kwargs):
94+
def get_files(cls, codebase, **kwargs):
9595
"""
96-
Return an iterable of serialized scan results from a codebase.
96+
Return an iterable of serialized files mapping from a codebase.
97+
Include "info", "timing" and strip root as needed.
9798
"""
9899
# FIXME: serialization SHOULD NOT be needed: only some format need it
99100
# (e.g. JSON) and only these should serialize

0 commit comments

Comments
 (0)