Skip to content

Instantly share code, notes, and snippets.

@bc-lee
Created February 17, 2022 10:31
Show Gist options
  • Save bc-lee/7d83845d112ce5e0a7cc56bd2b262ee1 to your computer and use it in GitHub Desktop.
Save bc-lee/7d83845d112ce5e0a7cc56bd2b262ee1 to your computer and use it in GitHub Desktop.
print_podfile_dependency.py
#!/usr/bin/env python3
import argparse
import sys
import re
# third_party
import graphviz # pip3 install graphviz
import yaml # pip install pyyaml
RE_Package = re.compile(r"(\S+)(\s\(.*\))?")
def parse_podfile_lock(file_path):
primary_dependencies = set() # Standalone Podspec 이 있는 경우
dependencies = set()
relations = []
with open(file_path, "r") as f:
pods = yaml.safe_load(f)
for dep in pods["DEPENDENCIES"]:
primary_dependencies.add(RE_Package.match(dep).group(1))
for relation in pods["PODS"]:
if isinstance(relation, str):
continue
for key, value in relation.items():
match = RE_Package.match(key)
if not match or not match.group(1):
continue
package = match.group(1)
required_packages = (match.group(1) for match in
(RE_Package.match(item) for
item in value) if match)
for required in required_packages:
relations.append([package, required])
dot = graphviz.Digraph('dependencies')
for dep in primary_dependencies:
dot.node(dep, color="chartreuse")
def add_node_if_not_found(name):
if name not in primary_dependencies and name not in dependencies:
dependencies.add(name)
dot.node(name, color="darkorange")
for relation in relations:
for item in relation:
add_node_if_not_found(item)
dot.edge(relation[0], relation[1])
return dot
def main(argc):
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", required=True)
option = parser.parse_args(argc)
input_file = option.input
dot = parse_podfile_lock(input_file)
dot.graph_attr['rankdir'] = 'LR'
# TODO(daniel.l): output path, 파일형식 설정
print("output: " + dot.render(format='svg'))
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment