Skip to content

Commit f3a3a05

Browse files
committed
Load JSON issue fixed #318
* The tool is now able to load the `ScanCode` json output * The tool is now able to load the `AboutCode Manager` json output * The tool is now able to load a `non-list` json file Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 33f95ff commit f3a3a05

File tree

6 files changed

+184
-1
lines changed

6 files changed

+184
-1
lines changed

src/attributecode/util.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,58 @@ def load_json(location, use_mapping=False):
378378
"""
379379
with open(location) as json_file:
380380
results = json.load(json_file)
381+
# If the loaded JSON is not a list,
382+
# - JSON output from AboutCode Manager:
383+
# look for the "components" field as it is the field
384+
# that contain everything the tool needs and ignore other fields.
385+
# For instance,
386+
# {
387+
# "aboutcode_manager_notice":"xyz",
388+
# "aboutcode_manager_version":"xxx",
389+
# "components":
390+
# [{
391+
# "license_expression":"apache-2.0",
392+
# "copyright":"Copyright (c) 2017 nexB Inc.",
393+
# "path":"ScanCode",
394+
# ...
395+
# }]
396+
# }
397+
#
398+
# - JSON output from ScanCode:
399+
# look for the "files" field as it is the field
400+
# that contain everything the tool needs and ignore other fields:
401+
# For instance,
402+
# {
403+
# "scancode_notice":"xyz",
404+
# "scancode_version":"xxx",
405+
# "files":
406+
# [{
407+
# "path": "test",
408+
# "type": "directory",
409+
# "name": "test",
410+
# ...
411+
# }]
412+
# }
413+
#
414+
# - JSON file that is not produced by scancode or aboutcode toolkit
415+
# For instance,
416+
# {
417+
# "path": "test",
418+
# "type": "directory",
419+
# "name": "test",
420+
# ...
421+
# }
422+
if not isinstance(results, list):
423+
if u'aboutcode_manager_notice' in results:
424+
updated_results = results['components']
425+
elif u'scancode_notice' in results:
426+
updated_results = results['files']
427+
else:
428+
updated_results = [results]
429+
else:
430+
updated_results = results
381431
if use_mapping:
382-
results = apply_mapping(results)
432+
results = apply_mapping(updated_results)
383433
return results
384434

385435

tests/test_util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,19 @@ def test_load_json_without_mapping(self):
288288
result = util.load_json(test_file)
289289
assert expected == result
290290

291+
def test_load_json_with_mapping(self):
292+
test_file = get_test_loc('load/expected_need_mapping.json')
293+
expected = [OrderedDict(
294+
[('about_file_path', '/load/this.ABOUT'),
295+
('version', '0.11.0'),
296+
('about_resource', '.'),
297+
('name', 'AboutCode'),
298+
('about_resource_path', '.'),
299+
])
300+
]
301+
result = util.load_json(test_file, use_mapping=True)
302+
assert expected == result
303+
291304
def test_get_about_file_path_from_csv_using_mapping(self):
292305
test_file = get_test_loc('util/about.csv')
293306
expected = ['about.ABOUT']
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"aboutcode_manager_notice":"Exported from AboutCode Manager and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\\nAboutCode Manager should be considered or used as legal advice. Consult an Attorney\\nfor any legal advice.\\nAboutCode Manager is a free software analysis application from nexB Inc. and others.\\nVisit https://github.com/nexB/aboutcode-manager/ for support and download.\"",
3+
"aboutcode_manager_version":"2.4.0",
4+
5+
"components":
6+
[{"license_expression":"apache-2.0",
7+
"copyright":"Copyright (c) 2017 nexB Inc.",
8+
"licenses":[{"key":"apache-2.0"}],
9+
"copyrights":[{"statements":["Copyright (c) 2017 nexB Inc."]}],
10+
"path":"ScanCode",
11+
"review_status":"Analyzed",
12+
"name":"ScanCode",
13+
"version":"2.2.1",
14+
"owner":"nexB Inc.",
15+
"code_type":"Source",
16+
"is_modified":false,
17+
"is_deployed":false,
18+
"feature":"",
19+
"purpose":"",
20+
"homepage_url":null,
21+
"download_url":null,
22+
"license_url":null,
23+
"notice_url":null,
24+
"programming_language":"Python",
25+
"notes":"",
26+
"fileId":8458
27+
}]
28+
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"about_file": "/load/this.ABOUT",
4+
"about_resource_path": ".",
5+
"about_resource": ".",
6+
"name": "AboutCode",
7+
"version": "0.11.0"
8+
}
9+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"path": "/load/this.ABOUT",
3+
"about_resource_path": ".",
4+
"about_resource": ".",
5+
"name": "AboutCode",
6+
"version": "0.11.0"
7+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"scancode_notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
3+
"scancode_version": "2.9.0b1.post32.fea65d3",
4+
"files_count": 1,
5+
"files": [
6+
{
7+
"path": "test",
8+
"type": "directory",
9+
"name": "test",
10+
"base_name": "test",
11+
"extension": "",
12+
"size": 0,
13+
"date": null,
14+
"sha1": null,
15+
"md5": null,
16+
"mime_type": null,
17+
"file_type": null,
18+
"programming_language": null,
19+
"is_binary": false,
20+
"is_text": false,
21+
"is_archive": false,
22+
"is_media": false,
23+
"is_source": false,
24+
"is_script": false,
25+
"licenses": [],
26+
"copyrights": [],
27+
"emails": [],
28+
"urls": [],
29+
"files_count": 1,
30+
"dirs_count": 0,
31+
"size_count": 173,
32+
"scan_errors": []
33+
},
34+
{
35+
"path": "test/ScanCode.ABOUT",
36+
"type": "file",
37+
"name": "ScanCode.ABOUT",
38+
"base_name": "ScanCode",
39+
"extension": ".ABOUT",
40+
"size": 173,
41+
"date": "2018-03-14",
42+
"sha1": "96f9a9baa6d9114a190a68f4364341d5b16a62cd",
43+
"md5": "ef3544d674fa0fe93daf435dacdf6857",
44+
"mime_type": "text/plain",
45+
"file_type": "ASCII text",
46+
"programming_language": "ActionScript 3",
47+
"is_binary": false,
48+
"is_text": true,
49+
"is_archive": false,
50+
"is_media": false,
51+
"is_source": true,
52+
"is_script": false,
53+
"licenses": [],
54+
"copyrights": [
55+
{
56+
"statements": [
57+
"Copyright (c) 2017 nexB Inc. owner nexB Inc."
58+
],
59+
"holders": [
60+
"nexB Inc. owner nexB Inc."
61+
],
62+
"authors": [],
63+
"start_line": 5,
64+
"end_line": 7
65+
}
66+
],
67+
"emails": [],
68+
"urls": [],
69+
"files_count": 0,
70+
"dirs_count": 0,
71+
"size_count": 0,
72+
"scan_errors": []
73+
}
74+
]
75+
}

0 commit comments

Comments
 (0)