File tree 4 files changed +37
-1
lines changed
4 files changed +37
-1
lines changed Original file line number Diff line number Diff line change
1
+ Support using "file:" to load a PEP 440-compliant package version
2
+ from a text file.
Original file line number Diff line number Diff line change @@ -2424,7 +2424,7 @@ Metadata
2424
2424
Key Aliases Type
2425
2425
============================== ================= =====
2426
2426
name str
2427
- version attr:, str
2427
+ version attr:, file:, str
2428
2428
url home-page str
2429
2429
download_url download-url str
2430
2430
project_urls dict
@@ -2444,6 +2444,10 @@ requires list-comma
2444
2444
obsoletes list-comma
2445
2445
============================== ================= =====
2446
2446
2447
+ .. note::
2448
+ A version loaded using the ``file:`` directive must comply with PEP 440.
2449
+ It is easy to accidentally put something other than a valid version
2450
+ string in such a file, so validation is stricter in this case.
2447
2451
2448
2452
Options
2449
2453
-------
Original file line number Diff line number Diff line change 7
7
from importlib import import_module
8
8
9
9
from distutils .errors import DistutilsOptionError , DistutilsFileError
10
+ from setuptools .extern .packaging .version import LegacyVersion , parse
10
11
from setuptools .extern .six import string_types
11
12
12
13
@@ -427,6 +428,18 @@ def _parse_version(self, value):
427
428
:rtype: str
428
429
429
430
"""
431
+ version = self ._parse_file (value )
432
+
433
+ if version != value :
434
+ version = version .strip ()
435
+ # Be strict about versions loaded from file because it's easy to
436
+ # accidentally include newlines and other unintended content
437
+ if isinstance (parse (version ), LegacyVersion ):
438
+ raise DistutilsOptionError ('Version loaded from %s does not comply with PEP 440: %s' % (
439
+ value , version
440
+ ))
441
+ return version
442
+
430
443
version = self ._parse_attr (value )
431
444
432
445
if callable (version ):
Original file line number Diff line number Diff line change @@ -268,6 +268,23 @@ def test_version(self, tmpdir):
268
268
with get_dist (tmpdir ) as dist :
269
269
assert dist .metadata .version == '2016.11.26'
270
270
271
+ def test_version_file (self , tmpdir ):
272
+
273
+ _ , config = fake_env (
274
+ tmpdir ,
275
+ '[metadata]\n '
276
+ 'version = file: fake_package/version.txt\n '
277
+ )
278
+ tmpdir .join ('fake_package' , 'version.txt' ).write ('1.2.3\n ' )
279
+
280
+ with get_dist (tmpdir ) as dist :
281
+ assert dist .metadata .version == '1.2.3'
282
+
283
+ tmpdir .join ('fake_package' , 'version.txt' ).write ('1.2.3\n 4.5.6\n ' )
284
+ with pytest .raises (DistutilsOptionError ):
285
+ with get_dist (tmpdir ) as dist :
286
+ _ = dist .metadata .version
287
+
271
288
def test_unknown_meta_item (self , tmpdir ):
272
289
273
290
fake_env (
You can’t perform that action at this time.
0 commit comments