Skip to content

Commit 902ddae

Browse files
committed
Simplify setup.py's get_version
1 parent d994427 commit 902ddae

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

setup.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
#!/usr/bin/env python
2-
3-
import codecs
1+
import ast
42
import os.path
3+
import re
54

65
from setuptools import setup
76

87

9-
def read(rel_path):
10-
here = os.path.abspath(os.path.dirname(__file__))
11-
with codecs.open(os.path.join(here, rel_path), 'r') as fp:
12-
return fp.read()
13-
14-
15-
def get_version(rel_path):
16-
for line in read(rel_path).splitlines():
17-
if line.startswith('__version__'):
18-
delim = '"' if '"' in line else "'"
19-
return line.split(delim)[1]
20-
else:
21-
raise RuntimeError("Unable to find version string.")
8+
def get_version(rel_path: str) -> str:
9+
"""
10+
Parse a version string from a __version__ = ... line in the given file.
11+
"""
12+
with open(os.path.join(os.path.dirname(__file__), rel_path)) as infp:
13+
match = re.search("__version__ = (.+?)$", infp.read(), re.M)
14+
if not match:
15+
raise ValueError("No version could be found")
16+
return ast.literal_eval(match.group(1))
2217

2318

2419
with open('README.md') as f:

0 commit comments

Comments
 (0)