Skip to content

Instantly share code, notes, and snippets.

@ZedThree
Last active February 2, 2023 21:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZedThree/7196938 to your computer and use it in GitHub Desktop.
Save ZedThree/7196938 to your computer and use it in GitHub Desktop.
Build a dot graph of the Fortran modules in a list of files.
import re
def build_graph(files,exclude=['hdf5','h5lt','mpi']):
"""Build a dot graph of the Fortran modules in a list of files,
excluding modules named in the list exclude"""
# Start the graph
graph = "digraph G {\n"
deps = {}
p = re.compile("^(?:module|program) ([a-zA-Z0-9_]*)",
re.IGNORECASE+re.MULTILINE)
for mod in files:
with open(mod,'r') as f:
contents = f.read()
# Get the true module name
# Careful! It only gets the first module in the file!
mod_name = re.search(p,contents).group(1)
# Find all the USE statements and get the module
deps[mod_name] = re.findall("USE ([a-zA-Z0-9_]*)",contents)
for k in deps:
# Remove duplicates and unwanted modules
deps[k] = list(set(deps[k]) - set(exclude))
for v in deps[k]:
# Add the edges to the graph
edge = k + " -> " + v + ";\n"
graph = graph + edge
# Close the graph and return it
graph = graph + "}"
return graph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment