Skip to content

Commit

Permalink
Sorted JSONs + Helper Script
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-nain committed Jan 14, 2025
1 parent 04bd7b1 commit 8011b0a
Show file tree
Hide file tree
Showing 6 changed files with 5,041 additions and 4,645 deletions.
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

0 comments on commit 8011b0a

Please sign in to comment.