|
3 | 3 | # Copyright (c) 2021 LG Electronics Inc. |
4 | 4 | # SPDX-License-Identifier: Apache-2.0 |
5 | 5 | import os |
| 6 | +import platform |
6 | 7 | from fosslight_util.write_excel import write_result_to_excel, write_result_to_csv |
7 | 8 | from fosslight_util.write_opossum import write_opossum |
8 | 9 | from fosslight_util.write_yaml import write_yaml |
| 10 | +from fosslight_util.write_spdx import write_spdx |
9 | 11 | from typing import Tuple |
10 | 12 |
|
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'} |
12 | 16 |
|
13 | 17 |
|
14 | 18 | def check_output_format(output='', format='', customized_format={}): |
@@ -106,25 +110,95 @@ def check_output_formats(output='', formats=[], customized_format={}): |
106 | 110 | return success, msg, output_path, output_files, output_extensions |
107 | 111 |
|
108 | 112 |
|
| 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 | + |
109 | 167 | 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]: |
111 | 169 | success = True |
112 | 170 | msg = '' |
113 | 171 |
|
114 | 172 | if file_extension == '': |
115 | 173 | file_extension = '.xlsx' |
116 | 174 | result_file = output_file_without_ext + file_extension |
117 | 175 |
|
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.' |
126 | 191 | 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})' |
129 | 203 |
|
130 | 204 | return success, msg, result_file |
0 commit comments