Skip to content

Commit

Permalink
reduction script in Python (#92)
Browse files Browse the repository at this point in the history
Python code to remove the unused schema objects.
  • Loading branch information
brian-comply0 authored Apr 11, 2024
1 parent 5089e54 commit eac17e3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@
hs_err_pid*
/.project

node_modules/
node_modules/

# reduction reports
rpt_*
63 changes: 63 additions & 0 deletions src/clear_unused.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import yaml

working_dir = "../"
input_file = working_dir + "/OSCALRestOpenAPI.yaml"
output_file = working_dir + "/OSCALRestOpenAPI_REDUCED.yaml"
in_use_file = working_dir + "/rpt_in_use_list.txt"
removed_file = working_dir + "/rpt_removed_list.txt"

ref_list = []
remove_list = []

def build_ref_list(key_name, item_key, item_value):
if item_key == key_name:
if item_value[0] == "#" and (item_value not in ref_list) :
print("+", end="")
ref_list.append(item_value)
else:
print("_", end="")
else:
print(".", end="")

def lookup(key_name, data):
global ref_list
if isinstance(data, dict):
for item_key, item_value in data.items():
if isinstance(item_value, dict):
lookup(key_name, data[item_key])
else: # isinstance(item_value, list):
build_ref_list(key_name, key_name, item_value)


def interrogate(top_level_key_name):
global ref_list
print("-- Checking " + top_level_key_name)
lookup("$ref", openAPI[top_level_key_name])
print()

with open(input_file, 'r') as file:
openAPI = yaml.safe_load(file)

print("Compiling list of references in use")
interrogate("tags")
interrogate("paths")

with open(in_use_file, 'w') as file:
for line in ref_list:
file.write("%s\n" % line)

print("Identifying schema definitions to remove")
for item in openAPI["components"]["schemas"].keys():
if not "#/components/schemas/" + item in ref_list:
remove_list.append(item)

with open(removed_file, 'w') as file:
for line in remove_list:
file.write("%s\n" % line)

for item in remove_list:
del openAPI["components"]["schemas"][item]

with open(output_file, 'w') as file:
yaml.dump(openAPI, file, sort_keys=False)

0 comments on commit eac17e3

Please sign in to comment.