Skip to content

Commit 570986c

Browse files
authored
Merge pull request #36 from fosslight/develop
Add function to get spdx licenses
2 parents 90b1f7e + a9b2736 commit 570986c

File tree

6 files changed

+125
-21
lines changed

6 files changed

+125
-21
lines changed

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ It is a package that supports common utils used by FOSSLight Scanner.
1010

1111
## Features
1212
1. It simplifies the logger setup.
13-
2. It easily outputs csv file and excel file in FOSSLight Report format.
14-
3. It provides a simple function to create a text file.
13+
2. It provides a simple function to create a output file.
14+
3. It provides a spdx license list with json format.
1515
4. It defines common constant variables.
1616
5. It provides a thread that prints the spinner.
1717
6. Download source code.
@@ -65,31 +65,29 @@ def test():
6565
```
6666

6767

68-
### 2. Write csv and excel files (tests/test_excel.py)
68+
### 2. Write result files (tests/test_output_format.py)
6969
```
70-
from fosslight_util._write_excel import write_excel_and_csv
71-
70+
from fosslight_util.output_format import write_output_file
7271
72+
# 2nd param : output file format
73+
# => file format(excel: .xlsx, csv: .csv, opossum: .json)
7374
def test():
7475
sheet_contents = {'SRC':[['run_scancode.py', 'fosslight_source',
7576
'3.0.6', 'Apache-2.0', 'https://github.com/LGE-OSS/fosslight_source', 'https://github.com/LGE-OSS/fosslight_source', 'Copyright (c) 2021 LG Electronics, Inc.', 'Exclude', 'Comment message'],
7677
['dependency_unified.py', 'fosslight_dependency',
7778
'3.0.6', 'Apache-2.0', 'https://github.com/LGE-OSS/fosslight_dependency', 'https://github.com/LGE-OSS/fosslight_dependency', 'Copyright (c) 2020 LG Electronics, Inc.', '', '']],
7879
'BIN':[['askalono.exe', 'askalono',
7980
'0.4.3', 'Apache-2.0', 'https://github.com/jpeddicord/askalono', '', 'Copyright (c) 2018 Amazon.com, Inc. or its affiliates.', '', '']]}
80-
81-
success, msg = write_excel_and_csv(
82-
'test_result/excel/FOSSLight-Report', sheet_contents)
81+
success, msg = write_output_file('test_result/excel/FOSSLight-Report', '.xlsx', sheet_contents)
8382
```
8483

85-
### 3. Write a text file (tests/test_text.py)
84+
### 3. Get spdx licenses (tests/test_spdx_licenses.py)
8685
```
87-
from fosslight_util.write_txt import write_txt_file
86+
from fosslight_util.spdx_licenses import get_spdx_licenses_json
8887
8988
9089
def test():
91-
success, error_msg = write_txt_file("test_result/txt/test.txt",
92-
"Testing - Writing text in a file.")
90+
success, error_msg, licenses = get_spdx_licenses_json()
9391
```
9492

9593
### 4. Load common constant (tests/_print_log_with_another_logger.py)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"Programming Language :: Python :: 3.8",
3232
"Programming Language :: Python :: 3.9", ],
3333
install_requires=required,
34-
package_data={'fosslight_util': ['resources/frequentLicenselist.json']},
34+
package_data={'fosslight_util': ['resources/frequentLicenselist.json', 'resources/licenses.json']},
3535
include_package_data=True,
3636
entry_points={
3737
"console_scripts": [

src/fosslight_util/_create_frequenctLicenses.py renamed to src/fosslight_util/spdx_licenses.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,42 @@
1414
_frequentLicenselist_file = 'frequentLicenselist.json'
1515

1616

17-
def create_frequentlicenses():
17+
def get_spdx_licenses_json():
1818
success = True
1919
error_msg = ''
2020
licenses = ''
21+
# licenses : https://github.com/spdx/license-list-data/blob/v3.14/json/licenses.json
2122
licenses_file = os.path.join(_resources_dir, _licenses_json_file)
22-
frequentLicenses = {}
23-
_frequentLicenses_key = 'frequentLicenses'
2423

25-
# spdx_txt_url = _spdx_txt_base_url + version + /text/ + licenseId + '.txt'
26-
_spdx_txt_base_url = 'https://raw.githubusercontent.com/spdx/license-list-data/'
2724
try:
2825
base_dir = sys._MEIPASS
2926
except Exception:
3027
base_dir = os.path.dirname(__file__)
31-
# https://github.com/spdx/license-list-data/blob/v3.14/json/licenses.json
32-
file_withpath = os.path.join(base_dir, licenses_file)
3328

29+
file_withpath = os.path.join(base_dir, licenses_file)
3430
try:
3531
with open(file_withpath, 'r') as f:
3632
licenses = json.load(f)
33+
except Exception as e:
34+
success = False
35+
error_msg = 'Failed to open ' + file_withpath + ': ' + str(e)
36+
37+
return success, error_msg, licenses
38+
39+
40+
def create_frequentlicenses():
41+
success = True
42+
error_msg = ''
43+
licenses = ''
44+
frequentLicenses = {}
45+
_frequentLicenses_key = 'frequentLicenses'
46+
47+
# spdx_txt_url = _spdx_txt_base_url + version + /text/ + licenseId + '.txt'
48+
_spdx_txt_base_url = 'https://raw.githubusercontent.com/spdx/license-list-data/'
49+
50+
try:
51+
success, error_msg, licenses = get_spdx_licenses_json()
52+
if success:
3753
version = licenses['licenseListVersion']
3854
frequentLicenses[_frequentLicenses_key] = []
3955
for lic in licenses['licenses']:
@@ -55,7 +71,7 @@ def create_frequentlicenses():
5571

5672
except Exception as e:
5773
success = False
58-
error_msg = 'Failed to open and parse ' + licenses_file
74+
error_msg = 'Failed to open and parse licenses.json'
5975
print(error_msg, ": ", e)
6076
print(traceback.format_exc())
6177

tests/test_output_format.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
from fosslight_util.output_format import write_output_file
6+
from fosslight_util.set_log import init_log
7+
8+
9+
def main():
10+
logger, _result_log = init_log("test_result/output_format/log_write_output.txt")
11+
12+
sheet_list = {'SRC_FL_Source': [
13+
['test/lib/babel-polyfill.js', '', '', 'bsd-3-clause,facebook-patent-rights-2', '', '',
14+
'Copyright (c) 2014, Facebook, Inc.', 'Exclude', ''],
15+
['requirements.txt', '', '', 'MIT', 'https://pypi.org/project/future/0.18.2', '', '', '', ''],
16+
['bower.json', '', '', 'mit', '', '', '', '', ''],
17+
['LICENSE', '', '', 'mit', '', '', 'Copyright (c) 2016-2021, The Cytoscape Consortium', '', ''],
18+
['license-update.js', '', '', 'mit', '', '', 'Copyright (c) 2016-$ year, The Cytoscape Consortium', '', ''],
19+
['package.json', '', '', 'mit', '', '', '', '', ''], ['README.md', '', '', 'mit', '', '', '', '', ''],
20+
['dist/cytoscape.cjs.js', '', '', 'mit', '', '', 'Copyright Gaetan Renaudeau,Copyright (c) 2016-2021,c \
21+
The Cytoscape Consortium,copyright Koen Bok,Copyright (c) 2013-2014 Ralf S. Engelschall \
22+
(http://engelschall.com)', '', ''],
23+
['dist/cytoscape.esm.js', '', '', 'mit', '', '', 'Copyright Gaetan Renaudeau,Copyright (c) 2016-2021,\
24+
The Cytoscape Consortium,copyright Koen Bok,Copyright (c) 2013-2014 Ralf S. Engelschall \
25+
(http://engelschall.com)', '', ''],
26+
['dist/cytoscape.esm.min.js', '', '', 'mit', '', '', 'Copyright Gaetan Renaudeau,copyright Koen Bok, \
27+
Copyright (c) 2013-2014 Ralf S. Engelschall (http://engelschall.com)', '', ''],
28+
['dist/cytoscape.min.js', '', '', 'mit',
29+
'', '', 'Copyright Gaetan Renaudeau,Copyright (c) 2016-2021, The Cytoscape Consortium,copyright Koen Bok,Copyright \
30+
(c) 2013-2014 Ralf S. Engelschall (http://engelschall.com)', '', ''],
31+
['dist/cytoscape.umd.js', '', '', 'mit', '', '',
32+
'Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors,Copyright jQuery Foundation \
33+
and other contributors <https://jquery.org/>,Copyright (c) 2016-2021, The Cytoscape Consortium,copyright Koen\
34+
Bok,Copyright Gaetan Renaudeau,Copyright (c) 2013-2014 Ralf S. Engelschall (http://engelschall.com)', '', ''],
35+
['documentation/css/highlight/monokai_sublime.css', '', '', 'mit', '', '', '', '', ''],
36+
['documentation/js/cytoscape.min.js', '', '', 'mit', '', '', 'Copyright Gaetan Renaudeau,\
37+
Copyright (c) 2016-2021, The Cytoscape Consortium,copyright Koen Bok, \
38+
Copyright (c) 2013-2014 Ralf S. Engelschall (http://engelschall.com)', '', ''],
39+
['documentation/md/links.md', '', '', 'mit', '', '', '', '', ''],
40+
['src/event.js', '', '', 'mit', '', '', '', '', '']],
41+
'BIN_FL_Binary': [
42+
['askalono_macos', 'askalono', '', 'Apache-2.0', '', '', '', '', ''],
43+
['test/askalono_macos', 'askalono', '', 'Apache-2.0', '', '', '', 'Exclude', '']],
44+
'SRC_FL_Dependency': [
45+
['requirements.txt', 'pypi:future', '0.18.2', 'MIT', 'https://pypi.org/project/future/0.18.2',
46+
'https://python-future.org', '', '', ''],
47+
['requirements.txt', 'pypi:numpy', '1.19.5', 'BSD-3-Clause-Open-MPI,GCC-exception-3.1,GPL-3.0',
48+
'https://pypi.org/project/numpy/1.19.5', 'https://www.numpy.org', '', '', ''],
49+
['requirements.txt', 'pypi:pandas', '1.1.5', 'BSD-3-Clause', 'https://pypi.org/project/pandas/1.1.5',
50+
'https://pandas.pydata.org', '', '', '']]}
51+
52+
logger.warning("TESTING - Writing an excel output")
53+
success, msg = write_output_file(
54+
'test_result/output_format/FL-TEST_Excel.xlsx', '.xlsx', sheet_list)
55+
logger.warning("Result:" + str(success) + ", error_msg:" + msg)
56+
57+
logger.warning("TESTING - Writing an opossum output")
58+
success, msg = write_output_file(
59+
'test_result/output_format/FL-TEST_opossum.json', '.json', sheet_list)
60+
logger.warning("Result:" + str(success) + ", error_msg:" + msg)
61+
62+
63+
if __name__ == '__main__':
64+
main()

tests/test_spdx_licenses.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
from fosslight_util.set_log import init_log
6+
from fosslight_util.spdx_licenses import get_spdx_licenses_json
7+
8+
9+
def main():
10+
logger, _result_log = init_log("test_result/spdx_licenses/log_spdx_licenses.txt")
11+
logger.warning("TESTING - Get spdx licenses")
12+
13+
success, error_msg, licenses = get_spdx_licenses_json()
14+
logger.warning("Result:" + str(success) + ", error_msg:" + error_msg)
15+
16+
17+
if __name__ == '__main__':
18+
main()

tox.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ commands =
4747
fosslight_download -s "https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.12.2" -t test_wget -d test_logs
4848
fosslight_download -s "https://pub.dev/packages/file/versions/5.2.1" -t test_wget -d test_logs
4949
ls test_wget/
50+
# Test - write output file
51+
python tests/test_output_format.py
52+
# Test - get spdx licenses
53+
python tests/test_spdx_licenses.py
5054

5155
[testenv:release]
5256
deps =
@@ -80,5 +84,9 @@ commands =
8084
# Test - downloading source
8185
python tests/test_download.py
8286
ls test_result/download
87+
# Test - write output file
88+
python tests/test_output_format.py
89+
# Test - get spdx licenses
90+
python tests/test_spdx_licenses.py
8391
# Test - check PEP8
8492
pytest -v --flake8

0 commit comments

Comments
 (0)