Skip to content

Commit 0e0dcf6

Browse files
committed
setup.py: source version dynamically to fix build from source
We use the first method from the documentation at https://packaging.python.org/en/latest/guides/single-sourcing-package-version. Thanks to Weiliang Li (Github: https://github.com/kigawas) for pointing out the source build was broken and proposing a (different) fix.
1 parent 4608fcc commit 0e0dcf6

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

setup.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
from setuptools import setup
2-
import bip32
31
import io
2+
import os
3+
4+
from setuptools import setup
5+
6+
7+
# Taken from https://github.com/pypa/pip/blob/003c7ac56b4da80235d4a147fbcef84b6fbc8248/setup.py#L7-L21
8+
def read(rel_path: str) -> str:
9+
here = os.path.abspath(os.path.dirname(__file__))
10+
# intentionally *not* adding an encoding option to open, See:
11+
# https://github.com/pypa/virtualenv/issues/201#issuecomment-3145690
12+
with open(os.path.join(here, rel_path)) as fp:
13+
return fp.read()
14+
15+
16+
# Taken from https://github.com/pypa/pip/blob/003c7ac56b4da80235d4a147fbcef84b6fbc8248/setup.py#L7-L21
17+
def get_version(rel_path: str) -> str:
18+
for line in read(rel_path).splitlines():
19+
if line.startswith("__version__"):
20+
# __version__ = "0.9"
21+
delim = '"' if '"' in line else "'"
22+
return line.split(delim)[1]
23+
raise RuntimeError("Unable to find version string.")
424

525

626
with io.open("README.md", encoding="utf-8") as f:
@@ -10,7 +30,8 @@
1030
requirements = [r for r in f.read().split('\n') if len(r)]
1131

1232
setup(name="bip32",
13-
version=bip32.__version__,
33+
# We use the first approach from https://packaging.python.org/en/latest/guides/single-sourcing-package-version
34+
version=get_version("bip32/__init__.py"),
1435
description="Minimalistic implementation of the BIP32 key derivation scheme",
1536
long_description=long_description,
1637
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)