Skip to content

Commit 50f447e

Browse files
authored
Merge pull request #94 from scanoss/feature/mdaloia/ES-148-SCANOSS-PY-Do-not-throw-error-if-scanoss.json-does-not-exist
feat: ES-148 Omit settings file if it doesn't exist instead of throwing an error
2 parents 71665d7 + 7f08643 commit 50f447e

File tree

6 files changed

+59
-19
lines changed

6 files changed

+59
-19
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Upcoming changes...
1111

12+
## [1.19.6] - 2025-01-30
13+
### Added
14+
- Omit settings file if it does not exist instead of throwing an error.
15+
- Look settings file inside the folder being scanned instead of the cwd.
16+
1217
## [1.19.5] - 2025-01-14
1318
### Added
1419
- Add Docker image with SCANOSS user

Diff for: src/scanoss/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
THE SOFTWARE.
2323
"""
2424

25-
__version__ = "1.19.5"
25+
__version__ = "1.19.6"

Diff for: src/scanoss/cli.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,11 @@ def scan(parser, args):
575575
scan_settings = ScanossSettings(debug=args.debug, trace=args.trace, quiet=args.quiet)
576576
try:
577577
if args.identify:
578-
scan_settings.load_json_file(args.identify).set_file_type('legacy').set_scan_type('identify')
578+
scan_settings.load_json_file(args.identify, args.scan_dir).set_file_type('legacy').set_scan_type('identify')
579579
elif args.ignore:
580-
scan_settings.load_json_file(args.ignore).set_file_type('legacy').set_scan_type('blacklist')
580+
scan_settings.load_json_file(args.ignore, args.scan_dir).set_file_type('legacy').set_scan_type('blacklist')
581581
else:
582-
scan_settings.load_json_file(args.settings).set_file_type('new').set_scan_type('identify')
582+
scan_settings.load_json_file(args.settings, args.scan_dir).set_file_type('new').set_scan_type('identify')
583583
except ScanossSettingsError as e:
584584
print_stderr(f'Error: {e}')
585585
exit(1)

Diff for: src/scanoss/cyclonedx.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,12 @@ def produce_from_json(self, data: json, output_file: str = None) -> bool:
197197
'name': 'scanoss-py',
198198
'version': __version__,
199199
}
200-
]
201-
},
202-
'component': {
203-
'type': 'application',
204-
'name': 'NOASSERTION',
205-
'version': 'NOASSERTION'
200+
],
201+
'component': {
202+
'type': 'application',
203+
'name': 'NOASSERTION',
204+
'version': 'NOASSERTION'
205+
}
206206
},
207207
'components': [],
208208
'vulnerabilities': []

Diff for: src/scanoss/scanoss_settings.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
from jsonschema import validate
3131

3232
from .scanossbase import ScanossBase
33-
from .utils.file import validate_json_file
33+
from .utils.file import JSON_ERROR_FILE_NOT_FOUND, JSON_ERROR_FILE_EMPTY, validate_json_file
3434

35-
DEFAULT_SCANOSS_JSON_FILE = 'scanoss.json'
35+
DEFAULT_SCANOSS_JSON_FILE = Path('scanoss.json')
3636

3737

3838
class BomEntry(TypedDict, total=False):
@@ -96,16 +96,20 @@ def __init__(
9696
if filepath:
9797
self.load_json_file(filepath)
9898

99-
def load_json_file(self, filepath: 'str | None' = None) -> 'ScanossSettings':
99+
def load_json_file(self, filepath: 'str | None' = None, scan_root: 'str | None' = None) -> 'ScanossSettings':
100100
"""
101101
Load the scan settings file. If no filepath is provided, scanoss.json will be used as default.
102102
103103
Args:
104104
filepath (str): Path to the SCANOSS settings file
105105
"""
106+
106107
if not filepath:
107108
filepath = DEFAULT_SCANOSS_JSON_FILE
108-
json_file = Path(filepath).resolve()
109+
110+
filepath = Path(scan_root) / filepath if scan_root else Path(filepath)
111+
112+
json_file = filepath.resolve()
109113

110114
if filepath == DEFAULT_SCANOSS_JSON_FILE and not json_file.exists():
111115
self.print_debug(f'Default settings file "{filepath}" not found. Skipping...')
@@ -114,7 +118,11 @@ def load_json_file(self, filepath: 'str | None' = None) -> 'ScanossSettings':
114118

115119
result = validate_json_file(json_file)
116120
if not result.is_valid:
117-
raise ScanossSettingsError(f'Problem with settings file. {result.error}')
121+
if result.error_code == JSON_ERROR_FILE_NOT_FOUND or result.error_code == JSON_ERROR_FILE_EMPTY:
122+
self.print_msg(f'WARNING: The supplied settings file "{filepath}" was not found or is empty. Skipping...')
123+
return self
124+
else:
125+
raise ScanossSettingsError(f'Problem with settings file. {result.error}')
118126
try:
119127
validate(result.data, self.schema)
120128
except Exception as e:

Diff for: src/scanoss/utils/file.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,24 @@
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
THE SOFTWARE.
2323
"""
24+
2425
import json
2526
import os
26-
import sys
2727
from dataclasses import dataclass
2828
from typing import Optional
2929

30+
JSON_ERROR_PARSE = 1
31+
JSON_ERROR_FILE_NOT_FOUND = 2
32+
JSON_ERROR_FILE_EMPTY = 3
33+
JSON_ERROR_FILE_SIZE = 4
34+
3035

3136
@dataclass
3237
class JsonValidation:
3338
is_valid: bool
3439
data: Optional[dict] = None
3540
error: Optional[str] = None
41+
error_code: Optional[int] = None
3642

3743

3844
def validate_json_file(json_file_path: str) -> JsonValidation:
@@ -46,12 +52,33 @@ def validate_json_file(json_file_path: str) -> JsonValidation:
4652
Tuple[bool, str]: A tuple containing a boolean indicating if the file is valid and a message
4753
"""
4854
if not json_file_path:
49-
return JsonValidation(is_valid=False, error='No JSON file specified')
55+
return JsonValidation(is_valid=False, error="No JSON file specified")
5056
if not os.path.isfile(json_file_path):
51-
return JsonValidation(is_valid=False, error=f'File not found: {json_file_path}')
57+
return JsonValidation(
58+
is_valid=False,
59+
error=f"File not found: {json_file_path}",
60+
error_code=JSON_ERROR_FILE_NOT_FOUND,
61+
)
62+
try:
63+
if os.stat(json_file_path).st_size == 0:
64+
return JsonValidation(
65+
is_valid=False,
66+
error=f"File is empty: {json_file_path}",
67+
error_code=JSON_ERROR_FILE_EMPTY,
68+
)
69+
except OSError as e:
70+
return JsonValidation(
71+
is_valid=False,
72+
error=f"Problem checking file size: {json_file_path}: {e}",
73+
error_code=JSON_ERROR_FILE_SIZE,
74+
)
5275
try:
5376
with open(json_file_path) as f:
5477
data = json.load(f)
5578
return JsonValidation(is_valid=True, data=data)
5679
except json.JSONDecodeError as e:
57-
return JsonValidation(is_valid=False, error=f'Problem parsing JSON file: "{json_file_path}": {e}')
80+
return JsonValidation(
81+
is_valid=False,
82+
error=f'Problem parsing JSON file: "{json_file_path}": {e}',
83+
error_code=JSON_ERROR_PARSE,
84+
)

0 commit comments

Comments
 (0)