Skip to content

Instantly share code, notes, and snippets.

@Boomatang
Last active September 22, 2020 12:52
Show Gist options
  • Save Boomatang/8b28e457da0606980095079a46c416b7 to your computer and use it in GitHub Desktop.
Save Boomatang/8b28e457da0606980095079a46c416b7 to your computer and use it in GitHub Desktop.
This script will build the dependency tree for the a module and print out a list version of the tree.

This script will build the dependency tree for the a module and print out a list version of the tree. The first item on the list is the direct import into the project and then followed by the sub dependencies till the module been searched for is found. This script does require the use of go mod. Also note if the dependency graph is to large a recursion error with be throw.

To Run

The script is required to be ran from with in the go project folder but does not require to be in the root folder. If go mod graph works this script can work.

python compile_tree.py <MOUDLE NAME>

or create runable script with chmod +x compile_tree.py

Sample Print Out

==================================================


github.com/operator-framework/[email protected] <--Directly imported module
github.com/operator-framework/[email protected]
github.com/operator-framework/[email protected]
k8s.io/[email protected]
github.com/docker/docker <--Searched for dependency


==================================================
#!/usr/bin/env python
import subprocess
import sys
class Node:
def __init__(self, module, data, parent=None):
self.module = module
self.children = []
self.data = data
self.parent = parent
self.build = ''
def find_children(self):
try:
for data in self.data:
if len(data) < 2:
break
if self.module in data[1]:
self.children.append(Node(data[0], self.data, self))
del self.data
if len(self.children) > 0:
for child in self.children:
child.find_children()
except RecursionError as err:
print(err)
print('graph for module to large')
print('try narrow the search first by using')
print(f'\n\tgo mod graph | grep {sys.argv[1]}\n')
sys.exit(1)
def build_list(self):
if len(self.children) > 0:
for child in self.children:
child.build_list()
else:
self.build = self.history()
self.write_out()
def history(self):
history = ''
if self.parent is not None:
history = self.parent.history()
return self.parent.module + '\n' + history
else:
return history
def write_out(self):
print(self.build)
print(f"\n{'='*50}\n\n")
def __repr__(self):
return f"<Module: {self.module}>"
def format_string_to_list(string_data):
output = []
data = string_data.decode()
data = data.split('\n')
for line in data:
line = line.split(' ')
output.append(tuple(line))
return output
def compile_tree(module, data):
print(f"building tree for {module}")
tree = Node(module, data)
tree.find_children()
tree.build_list()
def run():
try:
print(sys.argv[1])
except IndexError:
print("Need module name")
sys.exit(1)
raw_data = subprocess.run(['go', 'mod', 'graph'], capture_output=True)
if raw_data.returncode == 0:
data = format_string_to_list(raw_data.stdout)
else:
print(raw_data)
print("an error of some kind happened")
sys.exit(1)
compile_tree(sys.argv[1].lower(), data)
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment