Skip to content

Commit

Permalink
Builders: little refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Sep 22, 2020
1 parent 1b54393 commit b054218
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
19 changes: 9 additions & 10 deletions readthedocs/doc_builder/backends/mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(self, *args, **kwargs):
os.path.dirname(self.yaml_file),
self.build_dir,
)
self.root_path = self.version.project.checkout_path(self.version.slug)

# README: historically, the default theme was ``readthedocs`` but in
# https://github.com/rtfd/readthedocs.org/pull/4556 we change it to
Expand Down Expand Up @@ -85,7 +84,7 @@ def get_yaml_config(self):
if not mkdocs_path:
mkdocs_path = 'mkdocs.yml'
return os.path.join(
self.project.checkout_path(self.version.slug),
self.cwd,
mkdocs_path,
)

Expand Down Expand Up @@ -130,7 +129,7 @@ def load_yaml_config(self):
'possibly due to a syntax error{note}'.format(note=note),
)

def append_conf(self, **__):
def append_conf(self):
"""
Set mkdocs config values.
Expand Down Expand Up @@ -198,7 +197,7 @@ def append_conf(self, **__):

# RTD javascript writing
rtd_data = self.generate_rtd_data(
docs_dir=os.path.relpath(docs_path, self.root_path),
docs_dir=os.path.relpath(docs_path, self.cwd),
mkdocs_config=user_config,
)
with open(os.path.join(docs_path, 'readthedocs-data.js'), 'w') as f:
Expand All @@ -224,8 +223,8 @@ def append_conf(self, **__):
# Write the mkdocs.yml to the build logs
self.run(
'cat',
os.path.relpath(self.yaml_file, self.root_path),
cwd=self.root_path,
os.path.relpath(self.yaml_file, self.cwd),
cwd=self.cwd,
)

def generate_rtd_data(self, docs_dir, mkdocs_config):
Expand Down Expand Up @@ -274,7 +273,6 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
return tmpl.render(data_ctx)

def build(self):
checkout_path = self.project.checkout_path(self.version.slug)
build_command = [
self.python_env.venv_bin(filename='python'),
'-m',
Expand All @@ -284,13 +282,14 @@ def build(self):
'--site-dir',
self.build_dir,
'--config-file',
os.path.relpath(self.yaml_file, self.root_path),
os.path.relpath(self.yaml_file, self.cwd),
]
if self.config.mkdocs.fail_on_warning:
build_command.append('--strict')
cmd_ret = self.run(
*build_command, cwd=checkout_path,
bin_path=self.python_env.venv_bin()
*build_command,
cwd=self.cwd,
bin_path=self.python_env.venv_bin(),
)
return cmd_ret.successful

Expand Down
22 changes: 12 additions & 10 deletions readthedocs/doc_builder/backends/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, *args, **kwargs):
self.config_file = self.project.conf_file(self.version.slug)
else:
self.config_file = os.path.join(
self.project.checkout_path(self.version.slug),
self.cwd,
self.config_file,
)
self.old_artifact_path = os.path.join(
Expand Down Expand Up @@ -86,7 +86,7 @@ def get_config_params(self):
os.path.dirname(
os.path.relpath(
self.config_file,
self.project.checkout_path(self.version.slug),
self.cwd,
),
),
'',
Expand Down Expand Up @@ -192,7 +192,7 @@ def get_config_params(self):

return data

def append_conf(self, **__):
def append_conf(self):
"""
Find or create a ``conf.py`` and appends default content.
Expand Down Expand Up @@ -224,9 +224,9 @@ def append_conf(self, **__):
'cat',
os.path.relpath(
self.config_file,
self.project.checkout_path(self.version.slug),
self.cwd,
),
cwd=self.project.checkout_path(self.version.slug),
cwd=self.cwd,
)

def build(self):
Expand Down Expand Up @@ -255,8 +255,9 @@ def build(self):
self.sphinx_build_dir,
])
cmd_ret = self.run(
*build_command, cwd=os.path.dirname(self.config_file),
bin_path=self.python_env.venv_bin()
*build_command,
cwd=os.path.dirname(self.config_file),
bin_path=self.python_env.venv_bin(),
)
return cmd_ret.successful

Expand Down Expand Up @@ -302,7 +303,7 @@ def venv_sphinx_supports_latexmk(self):
cmd_ret = self.run(
*command,
bin_path=self.python_env.venv_bin(),
cwd=self.project.checkout_path(self.version.slug),
cwd=self.cwd,
escape_command=False, # used on DockerBuildCommand
shell=True, # used on BuildCommand
record=False,
Expand Down Expand Up @@ -397,6 +398,7 @@ def move(self, **__):


class EpubBuilder(BaseSphinx):

type = 'sphinx_epub'
sphinx_builder = 'epub'
sphinx_build_dir = '_build/epub'
Expand All @@ -416,7 +418,7 @@ def move(self, **__):
'-f',
from_file,
to_file,
cwd=self.project.checkout_path(self.version.slug),
cwd=self.cwd,
)


Expand Down Expand Up @@ -636,5 +638,5 @@ def move(self, **__):
'-f',
from_file,
to_file,
cwd=self.project.checkout_path(self.version.slug),
cwd=self.cwd,
)
20 changes: 9 additions & 11 deletions readthedocs/doc_builder/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""Base classes for Builders."""

import logging
Expand Down Expand Up @@ -38,8 +36,7 @@ class BaseBuilder:
_force = False

ignore_patterns = []

# old_artifact_path = ..
old_artifact_path = None

def __init__(self, build_env, python_env, force=False):
self.build_env = build_env
Expand All @@ -48,6 +45,7 @@ def __init__(self, build_env, python_env, force=False):
self.project = build_env.project
self.config = python_env.config if python_env else None
self._force = force
self.cwd = self.project.checkout_path(self.version.slug)
self.target = self.project.artifact_path(
version=self.version.slug,
type_=self.type,
Expand All @@ -62,6 +60,10 @@ def force(self, **__):
log.info('Forcing a build')
self._force = True

def append_conf(self):
"""Set custom configurations for this builder."""
pass

def build(self):
"""Do the actual building of the documentation."""
raise NotImplementedError
Expand Down Expand Up @@ -89,16 +91,12 @@ def clean(self, **__):

def docs_dir(self, docs_dir=None, **__):
"""Handle creating a custom docs_dir if it doesn't exist."""
checkout_path = self.project.checkout_path(self.version.slug)
if not docs_dir:
for doc_dir_name in ['docs', 'doc', 'Doc', 'book']:
possible_path = os.path.join(checkout_path, doc_dir_name)
possible_path = os.path.join(self.cwd, doc_dir_name)
if os.path.exists(possible_path):
docs_dir = possible_path
break
if not docs_dir:
docs_dir = checkout_path
return docs_dir
return possible_path
return docs_dir or self.cwd

def create_index(self, extension='md', **__):
"""Create an index file if it needs it."""
Expand Down

0 comments on commit b054218

Please sign in to comment.