-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_project_name.py
42 lines (35 loc) · 1.73 KB
/
get_project_name.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
"""Extract project name from setup.py."""
import ast
import sys
from pathlib import Path
def get_project_name(setup_path='setup.py', default='myproject'):
"""Extract the project name from setup.py using AST parsing."""
try:
setup_content = Path(setup_path).read_text()
module = ast.parse(setup_content, mode='exec')
# Look for setup() call and find 'name' keyword argument
for node in module.body:
if (isinstance(node, ast.Expr) and
isinstance(node.value, ast.Call) and
isinstance(node.value.func, ast.Name) and
node.value.func.id == 'setup'):
for keyword in node.value.keywords:
if keyword.arg == 'name':
if isinstance(keyword.value, ast.Constant):
return keyword.value.value
elif isinstance(keyword.value, ast.Name):
# If name is a variable, try to find its definition
name_id = keyword.value.id
for prior_node in module.body:
if (isinstance(prior_node, ast.Assign) and
len(prior_node.targets) == 1 and
isinstance(prior_node.targets[0], ast.Name) and
prior_node.targets[0].id == name_id and
isinstance(prior_node.value, ast.Constant)):
return prior_node.value.value
return default
except (FileNotFoundError, SyntaxError, AttributeError):
return default
if __name__ == '__main__':
print(get_project_name())