Skip to content

Commit 312404b

Browse files
committed
Add basic SPDX tag/value output
Closes #338.
1 parent 62ca88f commit 312404b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def read(*names, **kwargs):
100100
'MarkupSafe >= 0.23',
101101
'colorama',
102102
'simplejson',
103+
'spdx-tools',
103104

104105
# packagedcode
105106
'requests >= 2.7.0, < 3.0.0',

src/scancode/cli.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class ScanCommand(utils.BaseCommand):
245245
Try 'scancode --help' for help on options and arguments.'''
246246

247247

248-
formats = ('json', 'html', 'html-app',)
248+
formats = ('json', 'html', 'html-app', 'spdx-tv')
249249

250250
def validate_formats(ctx, param, value):
251251
value_lower = value.lower()
@@ -647,5 +647,49 @@ def save_results(files_count, scanned_files, format, input, output_file):
647647
meta['files'] = scanned_files
648648
output_file.write(unicode(json.dumps(meta, indent=2 * ' ', iterable_as_array=True, encoding='utf-8')))
649649
output_file.write('\n')
650+
651+
elif format == 'spdx-tv':
652+
from spdx.checksum import Algorithm
653+
from spdx.creationinfo import Tool
654+
from spdx.document import Document, License
655+
from spdx.file import File
656+
from spdx.package import Package
657+
from spdx.utils import NoAssert
658+
from spdx.version import Version
659+
from spdx.writers.tagvalue import write_document
660+
661+
doc = Document(Version(2, 1), License.from_identifier('CC0-1.0'))
662+
663+
doc.creation_info.add_creator(Tool('ScanCode ' + version))
664+
doc.creation_info.set_created_now()
665+
666+
doc.package = Package(input, NoAssert())
667+
668+
for file_data in scanned_files:
669+
file_entry = File(file_data['path'])
670+
# FIXME: should we really compue the checcksum here rather than get it from the scan?
671+
file_entry.chk_sum = Algorithm('SHA1', file_entry.calc_chksum())
672+
for file_license in file_data['licenses']:
673+
spdx_id = file_license.get('spdx_license_key')
674+
# TODO: we should create a "LicenseRef:xxx" identifier
675+
# if the license is not known to SPDX
676+
if spdx_id:
677+
spdx_license = License.from_identifier(spdx_id)
678+
file_entry.add_lics(spdx_license)
679+
doc.package.add_lics_from_file(spdx_license)
680+
681+
file_entry.conc_lics = NoAssert()
682+
file_entry.copyright = NoAssert()
683+
doc.package.add_file(file_entry)
684+
685+
# Remove duplicate licenses from the list.
686+
doc.package.licenses_from_files = list(set(doc.package.licenses_from_files))
687+
688+
doc.package.verif_code = doc.package.calc_verif_code()
689+
doc.package.cr_text = NoAssert()
690+
doc.package.license_declared = NoAssert()
691+
doc.package.conc_lics = NoAssert()
692+
693+
write_document(doc, output_file)
650694
else:
651695
raise Exception('Unknown format')

0 commit comments

Comments
 (0)