Commit e0fd60cf authored by Jason R. Coombs's avatar Jason R. Coombs Committed by GitHub

Merge pull request #1359 from jmbowman/jmbowman/version_from_file

Support loading version from a file
parents 5668d151 a960ee1c
Support using "file:" to load a PEP 440-compliant package version
from a text file.
......@@ -2424,7 +2424,7 @@ Metadata
Key Aliases Type
============================== ================= =====
name str
version attr:, str
version attr:, file:, str
url home-page str
download_url download-url str
project_urls dict
......@@ -2444,6 +2444,10 @@ requires list-comma
obsoletes list-comma
============================== ================= =====
.. note::
A version loaded using the ``file:`` directive must comply with PEP 440.
It is easy to accidentally put something other than a valid version
string in such a file, so validation is stricter in this case.
Options
-------
......
......@@ -7,6 +7,7 @@ from functools import partial
from importlib import import_module
from distutils.errors import DistutilsOptionError, DistutilsFileError
from setuptools.extern.packaging.version import LegacyVersion, parse
from setuptools.extern.six import string_types
......@@ -427,6 +428,18 @@ class ConfigMetadataHandler(ConfigHandler):
:rtype: str
"""
version = self._parse_file(value)
if version != value:
version = version.strip()
# Be strict about versions loaded from file because it's easy to
# accidentally include newlines and other unintended content
if isinstance(parse(version), LegacyVersion):
raise DistutilsOptionError('Version loaded from %s does not comply with PEP 440: %s' % (
value, version
))
return version
version = self._parse_attr(value)
if callable(version):
......
......@@ -268,6 +268,23 @@ class TestMetadata:
with get_dist(tmpdir) as dist:
assert dist.metadata.version == '2016.11.26'
def test_version_file(self, tmpdir):
_, config = fake_env(
tmpdir,
'[metadata]\n'
'version = file: fake_package/version.txt\n'
)
tmpdir.join('fake_package', 'version.txt').write('1.2.3\n')
with get_dist(tmpdir) as dist:
assert dist.metadata.version == '1.2.3'
tmpdir.join('fake_package', 'version.txt').write('1.2.3\n4.5.6\n')
with pytest.raises(DistutilsOptionError):
with get_dist(tmpdir) as dist:
_ = dist.metadata.version
def test_unknown_meta_item(self, tmpdir):
fake_env(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment