Created
September 10, 2017 19:38
-
-
Save datakurre/7c16f3999e76dbe53e8461a27fe851ac to your computer and use it in GitHub Desktop.
Parsing naive install_requires with AST
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 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