Skip to content

Instantly share code, notes, and snippets.

@datakurre
Created September 10, 2017 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datakurre/7c16f3999e76dbe53e8461a27fe851ac to your computer and use it in GitHub Desktop.
Save datakurre/7c16f3999e76dbe53e8461a27fe851ac to your computer and use it in GitHub Desktop.
Parsing naive install_requires with AST
import ast
tree = ast.parse(open('setup.py').read())
names = {}
def strings(tree):
ret = []
for node in ast.walk(tree):
if isinstance(node, ast.Name):
ret.extend(names.get(node.id) or [])
if isinstance(node, ast.Str):
ret.append(node.s)
return ret
for node in ast.walk(tree):
if isinstance(node, ast.Assign):
value = strings(node.value)
for target in node.targets:
if isinstance(target, ast.Name):
names[target.id] = value
for node in ast.walk(tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
if not node.func.id == 'setup':
continue
for keyword in node.keywords:
if keyword.arg == 'install_requires':
print strings(keyword.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment