Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions lib/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import git
import json, git, os

VRT_FILENAME = 'vulnerability-rating-taxonomy.json'
DEPRECATED_MAPPING_FILENAME = 'deprecated-node-mapping.json'
Expand All @@ -9,7 +8,9 @@
SCW_FILENAME = 'secure-code-warrior-links.json'
SCW_DIR = 'remediation_training'
THIRD_PARTY_MAPPING_DIR = 'third-party-mappings'

CVSS_FILE = "cvss_v3/cvss_v3.json"
CWE_FILE = "cwe/cwe.json"
REMEDIATION_ADVICE_FILE = "remediation_advice/remediation_advice.json"

def get_json(filename):
with open(filename) as f:
Expand Down Expand Up @@ -130,3 +131,31 @@ def _all_id_lists(sub_vrt, prefix):
print(sub_vrt)
raise Exception('unexpected entry found')
return _all_id_lists(vrt['content'], [])

def sort_jsons():
'''
Sort all corresponding JSONs for this project for better readability and
maintaining properly formatted JSON files.
'''
def sort_json(json_data):
def sort_json_blocks(block_data):
sorted_blocks = list(sorted(block_data, key = lambda a: a['id']))
for idx, block in enumerate(sorted_blocks):
if 'children' in block and block['children']!=[]:
sorted_children = sort_json_blocks(block['children'])
sorted_blocks[idx]['children'] = sorted_children
return sorted_blocks
json_data['content'] = sort_json_blocks(json_data['content'])
return json_data

for json_path in [
VRT_FILENAME,
os.path.join(MAPPING_DIR, CVSS_FILE),
os.path.join(MAPPING_DIR, CWE_FILE),
os.path.join(MAPPING_DIR, REMEDIATION_ADVICE_FILE)
]:
data = sort_json(get_json(json_path))
print("`{}` JSON data sorted!".format(json_path))
output = json.dumps(data, indent=2)
open(json_path, "w").write(output)
print("- Writing {} bytes.\n".format(len(output)))
Loading
Loading