Last active
September 18, 2023 08:51
-
-
Save Hosuke/d596431315ae955dd1caf35ee2bb4077 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def get_model_dependencies(manifest_file, model_name): | |
with open(manifest_file, 'r') as f: | |
manifest = json.load(f) | |
# Build the adjacency list | |
adj_list = {} | |
for full_model_name, model in manifest['nodes'].items(): | |
if not full_model_name.startswith('model.'): | |
continue | |
if full_model_name not in adj_list: | |
adj_list[full_model_name] = [] | |
for ref in model['depends_on']['nodes']: | |
if ref.startswith('model.') and 'dunesql' not in manifest['nodes'][ref].get('tags', []): | |
if ref not in adj_list: | |
adj_list[ref] = [] | |
adj_list[full_model_name].append(ref) | |
# Perform a depth-first search to find all dependencies of the specified model | |
def dfs(node): | |
visited.add(node) | |
for neighbour in adj_list[node]: | |
if neighbour not in visited: | |
dfs(neighbour) | |
order.append(node) | |
visited = set() | |
order = [] | |
dfs('model.' + model_name) | |
# Print the models in order | |
for model in order: | |
print(model) | |
# Replace 'manifest.json' with the path to your manifest file | |
# Replace 'my_model' with the name of your model, ie. spellbook.labels_addresses | |
get_model_dependencies('manifest.json', 'my_model') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment