Skip to content

Commit 9041e4a

Browse files
committed
Use DOCKER_* settings for config validation
Remove all the hardcoded valid options/choices from the Config objects and get them from Django settings instead.
1 parent 5a9ae2e commit 9041e4a

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

readthedocs/config/config.py

+33-16
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class BuildConfigBase:
146146
'mkdocs',
147147
'submodules',
148148
]
149+
valid_build_images = [k.split(':')[1] for k in DOCKER_IMAGE_SETTINGS]
150+
default_build_image = DOCKER_DEFAULT_VERSION
149151
version = None
150152

151153
def __init__(self, env_config, raw_config, source_file):
@@ -246,6 +248,24 @@ def python_full_version(self):
246248
)
247249
return ver
248250

251+
def get_valid_python_versions_for_image(self, build_image):
252+
"""
253+
Return all the valid Python versions for a Docker image.
254+
255+
The Docker image (``build_image``) has to be its complete name, already
256+
validated: ``readthedocs/build:4.0``, not just ``4.0``.
257+
258+
Returns supported versions for the ``DOCKER_DEFAULT_VERSION`` if not
259+
``build_image`` found.
260+
"""
261+
262+
if build_image not in DOCKER_IMAGE_SETTINGS:
263+
build_image = '{}:{}'.format(
264+
DOCKER_DEFAULT_IMAGE,
265+
self.default_build_image,
266+
)
267+
return DOCKER_IMAGE_SETTINGS[build_image]['python']['supported_versions']
268+
249269
def as_dict(self):
250270
config = {}
251271
for name in self.PUBLIC_ATTRIBUTES:
@@ -268,18 +288,23 @@ class BuildConfigV1(BuildConfigBase):
268288
'"python.extra_requirements" section must be a list.'
269289
)
270290

271-
PYTHON_SUPPORTED_VERSIONS = [2, 2.7, 3, 3.5]
272-
DOCKER_SUPPORTED_VERSIONS = ['1.0', '2.0', 'latest']
273-
274291
version = '1'
275292

276293
def get_valid_python_versions(self):
277-
"""Get all valid python versions."""
294+
"""
295+
Return all valid Python versions.
296+
297+
.. note::
298+
299+
It does not take current build image used into account.
300+
"""
278301
try:
279302
return self.env_config['python']['supported_versions']
280303
except (KeyError, TypeError):
281-
pass
282-
return self.PYTHON_SUPPORTED_VERSIONS
304+
versions = set()
305+
for name, options in DOCKER_IMAGE_SETTINGS.items():
306+
versions = versions.union(options['python']['supported_versions'])
307+
return versions
283308

284309
def get_valid_formats(self): # noqa
285310
"""Get all valid documentation formats."""
@@ -339,7 +364,7 @@ def validate_build(self):
339364
with self.catch_validation_error('build'):
340365
build['image'] = validate_choice(
341366
str(_build['image']),
342-
self.DOCKER_SUPPORTED_VERSIONS,
367+
self.valid_build_images,
343368
)
344369
if ':' not in build['image']:
345370
# Prepend proper image name to user's image name
@@ -577,8 +602,6 @@ class BuildConfigV2(BuildConfigBase):
577602

578603
version = '2'
579604
valid_formats = ['htmlzip', 'pdf', 'epub']
580-
valid_build_images = ['1.0', '2.0', '3.0', 'stable', 'latest']
581-
default_build_image = 'latest'
582605
valid_install_method = [PIP, SETUPTOOLS]
583606
valid_sphinx_builders = {
584607
'html': 'sphinx',
@@ -793,13 +816,7 @@ def get_valid_python_versions(self):
793816
This should be called after ``validate_build()``.
794817
"""
795818
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']
819+
return self.get_valid_python_versions_for_image(build_image)
803820

804821
def validate_doc_types(self):
805822
"""

readthedocs/settings/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def USE_PROMOS(self): # noqa
272272
# Docker
273273
DOCKER_ENABLE = False
274274
DOCKER_DEFAULT_IMAGE = 'readthedocs/build'
275-
DOCKER_DEFAULT_VERSION = '3.0'
275+
DOCKER_DEFAULT_VERSION = 'stable'
276276
DOCKER_IMAGE = '{}:{}'.format(DOCKER_DEFAULT_IMAGE, DOCKER_DEFAULT_VERSION)
277277
DOCKER_IMAGE_SETTINGS = {
278278
'readthedocs/build:1.0': {
@@ -288,6 +288,8 @@ def USE_PROMOS(self): # noqa
288288
'python': {'supported_versions': [2, 2.7, 3, 3.5, 3.6, 3.7]},
289289
},
290290
}
291+
292+
# Alias tagged via ``docker tag`` on the build servers
291293
DOCKER_IMAGE_SETTINGS.update({
292294
'readthedocs/build:stable': DOCKER_IMAGE_SETTINGS.get('readthedocs/build:3.0'),
293295
'readthedocs/build:latest': DOCKER_IMAGE_SETTINGS.get('readthedocs/build:4.0'),

0 commit comments

Comments
 (0)