|
1 |
| -import json |
2 |
| -import git |
| 1 | +import json, git, os |
3 | 2 |
|
4 | 3 | VRT_FILENAME = 'vulnerability-rating-taxonomy.json'
|
5 | 4 | DEPRECATED_MAPPING_FILENAME = 'deprecated-node-mapping.json'
|
|
9 | 8 | SCW_FILENAME = 'secure-code-warrior-links.json'
|
10 | 9 | SCW_DIR = 'remediation_training'
|
11 | 10 | THIRD_PARTY_MAPPING_DIR = 'third-party-mappings'
|
12 |
| - |
| 11 | +CVSS_FILE = "cvss_v3/cvss_v3.json" |
| 12 | +CWE_FILE = "cwe/cwe.json" |
| 13 | +REMEDIATION_ADVICE_FILE = "remediation_advice/remediation_advice.json" |
13 | 14 |
|
14 | 15 | def get_json(filename):
|
15 | 16 | with open(filename) as f:
|
@@ -130,3 +131,31 @@ def _all_id_lists(sub_vrt, prefix):
|
130 | 131 | print(sub_vrt)
|
131 | 132 | raise Exception('unexpected entry found')
|
132 | 133 | return _all_id_lists(vrt['content'], [])
|
| 134 | + |
| 135 | +def sort_jsons(): |
| 136 | + ''' |
| 137 | + Sort all corresponding JSONs for this project for better readability and |
| 138 | + maintaining properly formatted JSON files. |
| 139 | + ''' |
| 140 | + def sort_json(json_data): |
| 141 | + def sort_json_blocks(block_data): |
| 142 | + sorted_blocks = list(sorted(block_data, key = lambda a: a['id'])) |
| 143 | + for idx, block in enumerate(sorted_blocks): |
| 144 | + if 'children' in block and block['children']!=[]: |
| 145 | + sorted_children = sort_json_blocks(block['children']) |
| 146 | + sorted_blocks[idx]['children'] = sorted_children |
| 147 | + return sorted_blocks |
| 148 | + json_data['content'] = sort_json_blocks(json_data['content']) |
| 149 | + return json_data |
| 150 | + |
| 151 | + for json_path in [ |
| 152 | + VRT_FILENAME, |
| 153 | + os.path.join(MAPPING_DIR, CVSS_FILE), |
| 154 | + os.path.join(MAPPING_DIR, CWE_FILE), |
| 155 | + os.path.join(MAPPING_DIR, REMEDIATION_ADVICE_FILE) |
| 156 | + ]: |
| 157 | + data = sort_json(get_json(json_path)) |
| 158 | + print("`{}` JSON data sorted!".format(json_path)) |
| 159 | + output = json.dumps(data, indent=2) |
| 160 | + open(json_path, "w").write(output) |
| 161 | + print("- Writing {} bytes.\n".format(len(output))) |
0 commit comments