Skip to content

Commit 1c007d2

Browse files
committed
Add setuptools.get_version(path, field='__version__')
This allows people who use setuptools to maintain project versions in a single place, for example as attributes in their Python source. Closes pypa#1316 Closes pypa/pip#6004 Closes pypa/packaging.python.org#571
1 parent cdb5eea commit 1c007d2

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

setup.cfg

-2
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,3 @@ universal = 1
2525
[metadata]
2626
license_file = LICENSE
2727

28-
[bumpversion:file:setup.py]
29-

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def pypi_link(pkg_filename):
8989

9090
setup_params = dict(
9191
name="setuptools",
92-
version="40.8.0",
92+
version=setuptools.get_version("setup.cfg", field="current_version"),
9393
description=(
9494
"Easily download, build, install, upgrade, and uninstall "
9595
"Python packages"

setuptools/__init__.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
__all__ = [
2828
'setup', 'Distribution', 'Feature', 'Command', 'Extension', 'Require',
2929
'SetuptoolsDeprecationWarning',
30-
'find_packages'
30+
'find_packages', 'get_version'
3131
]
3232

3333
if PY3:
@@ -224,5 +224,25 @@ def findall(dir=os.curdir):
224224
return list(files)
225225

226226

227+
def get_version(path, field='__version__'):
228+
"""
229+
Get version information from a text file. Version is extracted
230+
from "key = value" format with key matched at the beginning of
231+
a line and specified by `field` parameter. Returns string.
232+
"""
233+
version_file = os.path.abspath(path)
234+
# Using binary matching to avoid possible encoding problems
235+
# when reading arbitrary text files
236+
if type(field) is not bytes:
237+
field = field.encode('utf-8')
238+
for line in open(version_file, 'rb'):
239+
if line.startswith(field):
240+
# __version__ = "0.9"
241+
# current_version = 4.8.0
242+
_, value = line.split(b'=')
243+
version = value.strip(b' \'\"').decode()
244+
return version
245+
246+
227247
# Apply monkey patches
228248
monkey.patch_all()

0 commit comments

Comments
 (0)