Skip to content

Commit 5c6f6dd

Browse files
committed
Supports for applying setting from json file
Signed-off-by: SeongjunJo <[email protected]>
1 parent a81b042 commit 5c6f6dd

File tree

6 files changed

+131
-6
lines changed

6 files changed

+131
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ Options:
8484
-r Keep raw data
8585
-t Hide the progress bar
8686
-v Print FOSSLight Scanner version
87+
-s <path> Path to apply setting from json file (check format with 'setting.json' in this repository)
88+
* Direct cli flags have higher priority than setting file
89+
(ex, '-f yaml -s setting.json' - result file extension is .yaml)
8790
```
8891
- Refs.
8992
- Additional arguments for running dependency analysis. See the [FOSSLight Dependency Guide][fd_guide] for instructions.

src/fosslight_scanner/_help.py

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
-r\t\t\t Keep raw data
3434
-t\t\t\t Hide the progress bar
3535
-v\t\t\t Print FOSSLight Scanner version
36+
-s <path>\t Path to apply setting from file (check format with 'setting.json' in this repository)
37+
* Direct cli flags have higher priority than setting file
38+
(ex, '-f yaml -s setting.json' - result file extension is .yaml)
3639
--no_correction\t Enter if you don't want to correct OSS information with sbom-info.yaml
3740
* Correction mode only supported xlsx format.
3841
--correct_fpath <path> Path to the sbom-info.yaml file
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2021 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
7+
8+
def parse_setting_json(data):
9+
# check type, if invalid = init value
10+
mode = data.get('mode', [])
11+
path = data.get('path', [])
12+
dep_argument = data.get('dep_argument', '')
13+
outputDir = data.get('outputDir', '')
14+
outputFile = data.get('outputFile', '')
15+
format = data.get('format', '')
16+
link = data.get('link', [])
17+
db_url = data.get('db_url', '')
18+
timer = data.get('timer', False)
19+
raw = data.get('raw', False)
20+
core = data.get('core', -1)
21+
no_correction = data.get('no_correction', False)
22+
correct_fpath = data.get('correct_fpath', '')
23+
ui = data.get('ui', False)
24+
exclude_path = data.get('exclude_path', [])
25+
26+
str_lists = [mode, path, link, exclude_path]
27+
strings = [dep_argument, outputDir, outputFile, format, db_url, correct_fpath]
28+
booleans = [timer, raw, no_correction, ui]
29+
is_incorrect = False
30+
31+
# check if json file is incorrect format
32+
for i, target in enumerate(str_lists):
33+
if not (isinstance(target, list) and all(isinstance(item, str) for item in target)):
34+
is_incorrect = True
35+
str_lists[i] = []
36+
37+
for i, target in enumerate(strings):
38+
if not isinstance(target, str):
39+
is_incorrect = True
40+
str_lists[i] = ''
41+
42+
for i, target in enumerate(booleans):
43+
if not isinstance(target, bool):
44+
is_incorrect = True
45+
str_lists[i] = False
46+
47+
if not isinstance(core, int):
48+
is_incorrect = True
49+
core = -1
50+
51+
if (is_incorrect):
52+
print('Ignoring some values with incorrect format in the setting file.')
53+
54+
if not mode:
55+
mode = ['all']
56+
final_mode = mode[0].split()
57+
if (not ("compare" in final_mode) and (len(path) > 0)):
58+
path = [path[0]]
59+
link = link[0] if (link and not path) else ''
60+
output = os.path.join(outputDir, outputFile)
61+
62+
return final_mode, path, dep_argument, output, format, link, db_url, timer, \
63+
raw, core, no_correction, correct_fpath, ui, exclude_path

src/fosslight_scanner/cli.py

+42-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,45 @@
33
# Copyright (c) 2022 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
55
import sys
6+
import json
67
from argparse import ArgumentParser
78
from ._help import print_help_msg
89
from .fosslight_scanner import run_main, PKG_NAME
10+
from ._parse_setting import parse_setting_json
911
from fosslight_util.help import print_package_version
1012

1113

14+
def set_args(mode, path, dep_argument, output, format, link, db_url, timer,
15+
raw, core, no_correction, correct_fpath, ui, setting, exclude_path):
16+
if (setting):
17+
try:
18+
with open(setting, 'r', encoding='utf-8') as file:
19+
data = json.load(file)
20+
s_mode, s_path, s_dep_argument, s_output, s_format, s_link, s_db_url, s_timer, s_raw, s_core, \
21+
s_no_correction, s_correct_fpath, s_ui, s_exclude_path = parse_setting_json(data)
22+
23+
# direct cli arguments have higher priority than setting file
24+
mode = mode if mode else s_mode
25+
path = path if path else s_path
26+
dep_argument = dep_argument if dep_argument else s_dep_argument
27+
output = output if output else s_output
28+
format = format if format else s_format
29+
link = link if link else s_link
30+
db_url = db_url if db_url else s_db_url
31+
timer = timer if timer else s_timer
32+
raw = raw if raw else s_raw
33+
core = core if core else s_core
34+
no_correction = no_correction if no_correction else s_no_correction
35+
correct_fpath = correct_fpath if correct_fpath else s_correct_fpath
36+
ui = ui if ui else s_ui
37+
exclude_path = exclude_path if exclude_path else s_exclude_path
38+
39+
except Exception as e:
40+
print(f"Cannot open setting file: {e}")
41+
return mode, path, dep_argument, output, format, link, db_url, timer, \
42+
raw, core, no_correction, correct_fpath, ui, exclude_path
43+
44+
1245
def main():
1346
parser = ArgumentParser(description='FOSSLight Scanner', prog='fosslight_scanner', add_help=False)
1447
parser.add_argument('mode', nargs='*', help='source| dependency| binary| all| compare', default="")
@@ -26,6 +59,7 @@ def main():
2659
parser.add_argument('--version', '-v', help='Print version', action='store_true', dest='version', default=False)
2760
parser.add_argument('--help', '-h', help='Print help message', action='store_true', dest='help')
2861
parser.add_argument('--exclude', '-e', help='Path to exclude from analysis', dest='exclude_path', nargs='*', default=[])
62+
parser.add_argument('--setting', '-s', help='Scanner json setting file', type=str, dest='setting', default="")
2963
parser.add_argument('--no_correction', help='No correction with sbom-info.yaml',
3064
action='store_true', required=False, default=False)
3165
parser.add_argument('--correct_fpath', help='Path to the sbom-info.yaml',
@@ -35,18 +69,20 @@ def main():
3569
try:
3670
args = parser.parse_args()
3771
except SystemExit:
38-
sys.exit(0)
72+
sys.exit(1)
3973

4074
if args.help:
4175
print_help_msg()
4276
elif args.version:
4377
print_package_version(PKG_NAME, "FOSSLight Scanner Version:")
4478
else:
45-
if not args.mode:
46-
args.mode = ['all']
47-
run_main(args.mode, args.path, args.dep_argument, args.output, args.format,
48-
args.link, args.db_url, args.timer, args.raw, args.core,
49-
not args.no_correction, args.correct_fpath, args.ui, args.exclude_path)
79+
mode, path, dep_argument, output, format, link, db_url, timer, raw, core, no_correction, correct_fpath, \
80+
ui, exclude_path = set_args(args.mode, args.path, args.dep_argument, args.output, args.format,
81+
args.link, args.db_url, args.timer, args.raw, args.core, args.no_correction,
82+
args.correct_fpath, args.ui, args.setting, args.exclude_path)
83+
84+
run_main(mode, path, dep_argument, output, format, link, db_url, timer,
85+
raw, core, not no_correction, correct_fpath, ui, exclude_path)
5086

5187

5288
if __name__ == "__main__":

tests/setting.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"mode": ["binary source"],
3+
"path": ["tests"],
4+
"link": [],
5+
"dep_argument": "",
6+
"outputDir": "test_result_parse_setting",
7+
"outputFile": "",
8+
"exclude_path": ["test", "sample_license.txt"],
9+
"format": "excel",
10+
"db_url": "",
11+
"timer": false,
12+
"raw": true,
13+
"core": -1,
14+
"no_correction": false,
15+
"correct_fpath": "",
16+
"ui": false,
17+
"type": "analyze"
18+
}

tox.ini

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ commands =
3131
fosslight binary source -o test_result_multi_mode/test.xlsx -p tests -r
3232
fosslight -o test_result_exclude_path/test.xlsx -p tests -e test sample_license.txt
3333
fosslight dependency -o test_result_wget -w "https://github.com/LGE-OSS/example.git"
34+
fosslight -f yaml -s tests/setting.json
3435
ls test_result_wget
3536

3637
[testenv:release]
@@ -43,4 +44,5 @@ commands =
4344
fosslight binary dependency -o test_result_multi_mode/test.xlsx -p tests -r
4445
fosslight -o test_result_exclude_path/test.xlsx -p tests -e test sample_license.txt
4546
fosslight source -o test_result_wget -w "https://github.com/LGE-OSS/example.git"
47+
fosslight -f yaml -s tests/setting.json
4648
pytest -v --flake8

0 commit comments

Comments
 (0)