4
4
5
5
import copy
6
6
import os
7
+ import re
7
8
from contextlib import contextmanager
8
9
9
10
from django .conf import settings
@@ -146,6 +147,8 @@ class BuildConfigBase:
146
147
'mkdocs' ,
147
148
'submodules' ,
148
149
]
150
+
151
+ default_build_image = DOCKER_DEFAULT_VERSION
149
152
version = None
150
153
151
154
def __init__ (self , env_config , raw_config , source_file ):
@@ -246,6 +249,40 @@ def python_full_version(self):
246
249
)
247
250
return ver
248
251
252
+ @property
253
+ def valid_build_images (self ):
254
+ """
255
+ Return all the valid Docker image choices for ``build.image`` option.
256
+
257
+ The user can use any of this values in the YAML file. These values are
258
+ the keys of ``DOCKER_IMAGE_SETTINGS`` Django setting (without the
259
+ ``readthedocs/build`` part) plus ``stable`` and ``latest``.
260
+ """
261
+ images = {'stable' , 'latest' }
262
+ for k in DOCKER_IMAGE_SETTINGS :
263
+ _ , version = k .split (':' )
264
+ if re .fullmatch (r'^[\d\.]+$' , version ):
265
+ images .add (version )
266
+ return images
267
+
268
+ def get_valid_python_versions_for_image (self , build_image ):
269
+ """
270
+ Return all the valid Python versions for a Docker image.
271
+
272
+ The Docker image (``build_image``) has to be its complete name, already
273
+ validated: ``readthedocs/build:4.0``, not just ``4.0``.
274
+
275
+ Returns supported versions for the ``DOCKER_DEFAULT_VERSION`` if not
276
+ ``build_image`` found.
277
+ """
278
+
279
+ if build_image not in DOCKER_IMAGE_SETTINGS :
280
+ build_image = '{}:{}' .format (
281
+ DOCKER_DEFAULT_IMAGE ,
282
+ self .default_build_image ,
283
+ )
284
+ return DOCKER_IMAGE_SETTINGS [build_image ]['python' ]['supported_versions' ]
285
+
249
286
def as_dict (self ):
250
287
config = {}
251
288
for name in self .PUBLIC_ATTRIBUTES :
@@ -268,18 +305,23 @@ class BuildConfigV1(BuildConfigBase):
268
305
'"python.extra_requirements" section must be a list.'
269
306
)
270
307
271
- PYTHON_SUPPORTED_VERSIONS = [2 , 2.7 , 3 , 3.5 ]
272
- DOCKER_SUPPORTED_VERSIONS = ['1.0' , '2.0' , 'latest' ]
273
-
274
308
version = '1'
275
309
276
310
def get_valid_python_versions (self ):
277
- """Get all valid python versions."""
311
+ """
312
+ Return all valid Python versions.
313
+
314
+ .. note::
315
+
316
+ It does not take current build image used into account.
317
+ """
278
318
try :
279
319
return self .env_config ['python' ]['supported_versions' ]
280
320
except (KeyError , TypeError ):
281
- pass
282
- return self .PYTHON_SUPPORTED_VERSIONS
321
+ versions = set ()
322
+ for _ , options in DOCKER_IMAGE_SETTINGS .items ():
323
+ versions = versions .union (options ['python' ]['supported_versions' ])
324
+ return versions
283
325
284
326
def get_valid_formats (self ): # noqa
285
327
"""Get all valid documentation formats."""
@@ -339,7 +381,7 @@ def validate_build(self):
339
381
with self .catch_validation_error ('build' ):
340
382
build ['image' ] = validate_choice (
341
383
str (_build ['image' ]),
342
- self .DOCKER_SUPPORTED_VERSIONS ,
384
+ self .valid_build_images ,
343
385
)
344
386
if ':' not in build ['image' ]:
345
387
# Prepend proper image name to user's image name
@@ -577,8 +619,6 @@ class BuildConfigV2(BuildConfigBase):
577
619
578
620
version = '2'
579
621
valid_formats = ['htmlzip' , 'pdf' , 'epub' ]
580
- valid_build_images = ['1.0' , '2.0' , '3.0' , 'stable' , 'latest' ]
581
- default_build_image = 'latest'
582
622
valid_install_method = [PIP , SETUPTOOLS ]
583
623
valid_sphinx_builders = {
584
624
'html' : 'sphinx' ,
@@ -793,13 +833,7 @@ def get_valid_python_versions(self):
793
833
This should be called after ``validate_build()``.
794
834
"""
795
835
build_image = self .build .image
796
- if build_image not in DOCKER_IMAGE_SETTINGS :
797
- build_image = '{}:{}' .format (
798
- DOCKER_DEFAULT_IMAGE ,
799
- self .default_build_image ,
800
- )
801
- python = DOCKER_IMAGE_SETTINGS [build_image ]['python' ]
802
- return python ['supported_versions' ]
836
+ return self .get_valid_python_versions_for_image (build_image )
803
837
804
838
def validate_doc_types (self ):
805
839
"""
0 commit comments