Last active
February 2, 2023 21:15
-
-
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.
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 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