Skip to content

Commit ce00772

Browse files
committed
Update spdx function
Signed-off-by: jiyeong.seok <[email protected]>
1 parent bc1d960 commit ce00772

File tree

5 files changed

+237
-123
lines changed

5 files changed

+237
-123
lines changed

requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pytest-cov
44
pytest-flake8
55
flake8==3.9.2
66
tox-wheel
7-
fosslight-source
7+
fosslight-source
8+
spdx-tools==0.8.2

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ coloredlogs
88
python3-wget
99
beautifulsoup4
1010
jsonmerge
11-
spdx-tools==0.7.0rc0
11+
spdx-tools
1212
setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability
1313
numpy; python_version < '3.8'
1414
numpy>=1.22.2; python_version >= '3.8'

src/fosslight_util/oss_item.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from typing import List, Dict
1111

1212
_logger = logging.getLogger(LOGGER_NAME)
13+
CHECKSUM_NULL = "0"
1314

1415

1516
class OssItem:
@@ -98,6 +99,7 @@ def __init__(self, value):
9899
self._comment = ""
99100
self.is_binary = False
100101
self.oss_items: List[OssItem] = []
102+
self.checksum = CHECKSUM_NULL
101103

102104
def __del__(self):
103105
pass

src/fosslight_util/output_format.py

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
# Copyright (c) 2021 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
55
import os
6+
import platform
67
from fosslight_util.write_excel import write_result_to_excel, write_result_to_csv
78
from fosslight_util.write_opossum import write_opossum
89
from fosslight_util.write_yaml import write_yaml
10+
from fosslight_util.write_spdx import write_spdx
911
from typing import Tuple
1012

11-
SUPPORT_FORMAT = {'excel': '.xlsx', 'csv': '.csv', 'opossum': '.json', 'yaml': '.yaml'}
13+
SUPPORT_FORMAT = {'excel': '.xlsx', 'csv': '.csv', 'opossum': '.json', 'yaml': '.yaml',
14+
'spdx-yaml': '.yaml', 'spdx-json': '.json', 'spdx-xml': '.xml',
15+
'spdx-tag': '.tag'}
1216

1317

1418
def check_output_format(output='', format='', customized_format={}):
@@ -106,25 +110,95 @@ def check_output_formats(output='', formats=[], customized_format={}):
106110
return success, msg, output_path, output_files, output_extensions
107111

108112

113+
def check_output_formats_v2(output='', formats=[], customized_format={}):
114+
success = True
115+
msg = ''
116+
output_path = ''
117+
output_files = []
118+
output_extensions = []
119+
120+
if customized_format:
121+
support_format = customized_format
122+
else:
123+
support_format = SUPPORT_FORMAT
124+
125+
if formats:
126+
# If -f option exist
127+
formats = [format.lower() for format in formats]
128+
for format in formats:
129+
if format not in list(support_format.keys()):
130+
success = False
131+
msg = 'Enter the supported format with -f option: ' + ', '.join(list(support_format.keys()))
132+
else:
133+
output_extensions.append(support_format[format])
134+
135+
if success:
136+
if output != '':
137+
basename_extension = ''
138+
if not os.path.isdir(output):
139+
output_path = os.path.dirname(output)
140+
141+
basename = os.path.basename(output)
142+
basename_file, basename_extension = os.path.splitext(basename)
143+
if basename_extension:
144+
if formats:
145+
if basename_extension not in output_extensions:
146+
success = False
147+
msg = f"The format of output file(-o:'{output}') should be in the format list(-f:'{formats}')."
148+
else:
149+
if basename_extension not in support_format.values():
150+
success = False
151+
msg = 'Enter the supported file extension: ' + ', '.join(list(support_format.values()))
152+
output_extensions.append(basename_extension)
153+
output_files = [basename_file for _ in range(len(output_extensions))]
154+
else:
155+
output_path = output
156+
if not output_extensions:
157+
output_extensions = ['.xlsx']
158+
if not formats:
159+
for ext in output_extensions:
160+
for key, value in support_format.items():
161+
if value == ext:
162+
formats.append(key)
163+
break
164+
return success, msg, output_path, output_files, output_extensions, formats
165+
166+
109167
def write_output_file(output_file_without_ext: str, file_extension: str, scan_item, extended_header: dict = {},
110-
hide_header: dict = {}) -> Tuple[bool, str, str]:
168+
hide_header: dict = {}, format: str = '', spdx_version: str = '2.3') -> Tuple[bool, str, str]:
111169
success = True
112170
msg = ''
113171

114172
if file_extension == '':
115173
file_extension = '.xlsx'
116174
result_file = output_file_without_ext + file_extension
117175

118-
if file_extension == '.xlsx':
119-
success, msg = write_result_to_excel(result_file, scan_item, extended_header, hide_header)
120-
elif file_extension == '.csv':
121-
success, msg, result_file = write_result_to_csv(result_file, scan_item, False, extended_header)
122-
elif file_extension == '.json':
123-
success, msg = write_opossum(result_file, scan_item)
124-
elif file_extension == '.yaml':
125-
success, msg, result_file = write_yaml(result_file, scan_item, False)
176+
if format:
177+
if format == 'excel':
178+
success, msg = write_result_to_excel(result_file, scan_item, extended_header, hide_header)
179+
elif format == 'csv':
180+
success, msg, _ = write_result_to_csv(result_file, scan_item, False, extended_header)
181+
elif format == 'opossum':
182+
success, msg = write_opossum(result_file, scan_item)
183+
elif format == 'yaml':
184+
success, msg, _ = write_yaml(result_file, scan_item, False)
185+
elif format.startswith('spdx'):
186+
if platform.system() != 'Windows':
187+
success, msg, _ = write_spdx(output_file_without_ext, file_extension, scan_item, spdx_version)
188+
else:
189+
success = False
190+
msg = 'Windows not support spdx format.'
126191
else:
127-
success = False
128-
msg = f'Not supported file extension({file_extension})'
192+
if file_extension == '.xlsx':
193+
success, msg = write_result_to_excel(result_file, scan_item, extended_header, hide_header)
194+
elif file_extension == '.csv':
195+
success, msg, result_file = write_result_to_csv(result_file, scan_item, False, extended_header)
196+
elif file_extension == '.json':
197+
success, msg = write_opossum(result_file, scan_item)
198+
elif file_extension == '.yaml':
199+
success, msg, result_file = write_yaml(result_file, scan_item, False)
200+
else:
201+
success = False
202+
msg = f'Not supported file extension({file_extension})'
129203

130204
return success, msg, result_file

0 commit comments

Comments
 (0)