Created
March 9, 2018 08:11
-
-
Save prateek/e550142d6bface9d00c1c13cbcfcb7d0 to your computer and use it in GitHub Desktop.
Generate m3db org's package dag structure
This file contains 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
#!/bin/bash | |
if ! [ -x "$(command -v git)" ]; then | |
echo 'Error: git is not installed.' >&2 | |
exit 1 | |
fi | |
if ! [ -x "$(command -v dot)" ]; then | |
echo 'Error: graphviz (dot) is not installed.' >&2 | |
exit 1 | |
fi | |
set -euxo pipefail | |
CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
REPOS=(m3db m3x m3collector m3aggregator m3metrics m3coordinator m3em m3nsch m3cluster) | |
temp_dir=$(mktemp -d) | |
echo "Temp dir: $temp_dir" | |
cd $temp_dir | |
trap "{ cd $CWD; rm -rf $temp_dir ; }" EXIT ERR | |
for repo in "${REPOS[@]}" | |
do | |
echo $repo | |
git clone [email protected]:m3db/$repo.git | |
done | |
function generate_dot_file() { | |
find . -name 'glide.yaml' -not -path '*/vendor/*' \ | |
| xargs -I{} grep -H github.com/m3db {} \ | |
| grep ':-' > $temp_dir/dag.out | |
INPUT_FILE=$temp_dir/dag.out python <<END | |
import os | |
from collections import defaultdict | |
input_file_path = os.environ['INPUT_FILE'] | |
input_file = open(input_file_path) | |
# expected input "./m3aggregator/glide.yaml" | |
def extract_repo(glide_file): | |
end = glide_file.rfind('/') | |
if end == -1: | |
raise "Unknown format: %s" % glide_file | |
start = glide_file.rfind('/', 0, end-1) | |
if start == -1: | |
raise "Unknown format again: %s" % glide_file | |
return glide_file[start+1:end] | |
# expected input github.com/m3db/m3db | |
def extract_repo_from_package(package): | |
end = package.rfind('/') | |
if end == -1: | |
raise "Unknown format: %s" % package | |
return package[end+1:] | |
graph = defaultdict(lambda: set()) | |
for line in input_file.readlines(): | |
line = line.strip() | |
# e.g. line = "./m3aggregator/glide.yaml:- package: github.com/m3db/m3cluster" | |
glide_file, misc, package = line.split(' ') | |
dependent = extract_repo(glide_file) | |
upstream = extract_repo_from_package(package) | |
# print "%s %s | %s" % (dependent, upstream, line) | |
graph[upstream].add(dependent) | |
print 'digraph G {' | |
print ' "ci-scripts" -> "build-tools" ' | |
print ' "build-tools" -> "ci-scripts" ' | |
for key in graph: | |
print ' "ci-scripts" -> "%s"' % key | |
print ' "build-tools" -> "%s"' % key | |
for dep in graph[key]: | |
print ' "%s" -> "%s"' % (key, dep) | |
print '}' | |
END | |
} | |
generate_dot_file > $temp_dir/dag.dot | |
dot -Tpng -o $CWD/dag.png $temp_dir/dag.dot | |
open $CWD/dag.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment