-
Notifications
You must be signed in to change notification settings - Fork 4
/
book_name.py
executable file
·47 lines (32 loc) · 1.2 KB
/
book_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
43
#! /usr/bin/env python3
import os
import re
def find_pyproject_toml(start_path=None):
if start_path is None:
start_path = os.getcwd()
pyproject_path = os.path.join(start_path, "pyproject.toml")
if os.path.isfile(pyproject_path):
return pyproject_path
parent_dir = os.path.dirname(start_path)
if parent_dir == start_path: # Reached root directory
raise AssertionError("could not find pyproject.toml")
return find_pyproject_toml(parent_dir)
def get_project_name():
"""Extract project name from pyproject.toml file.
Args:
pyproject_path: Path to pyproject.toml file
Returns:
Project name as string
Raises:
AssertionError: If project name cannot be found
"""
pyproject_path = find_pyproject_toml()
with open(pyproject_path) as f:
content = f.read()
# Look for name = "something" or name = 'something'
match = re.search(r'''name\s*=\s*["']([^"']+)["']''', content)
if not match:
raise AssertionError("could not find project name in pyproject.toml")
return match.group(1)
if __name__ == "__main__":
print(get_project_name())