Unverified Commit 02ca158f authored by Nikita Titov's avatar Nikita Titov Committed by GitHub
Browse files

[python] migrate to pathlib in conf.py (#4427)

parent c359896e
......@@ -20,8 +20,9 @@
import datetime
import os
import sys
from distutils.dir_util import copy_tree
from pathlib import Path
from re import compile
from shutil import copytree
from subprocess import PIPE, Popen
from unittest.mock import Mock
......@@ -31,9 +32,9 @@ from docutils.parsers.rst import Directive
from docutils.transforms import Transform
from sphinx.errors import VersionRequirementError
CURR_PATH = os.path.abspath(os.path.dirname(__file__))
LIB_PATH = os.path.join(CURR_PATH, os.path.pardir, 'python-package')
sys.path.insert(0, LIB_PATH)
CURR_PATH = Path(__file__).parent.absolute()
LIB_PATH = CURR_PATH.parent / 'python-package'
sys.path.insert(0, str(LIB_PATH))
INTERNAL_REF_REGEX = compile(r"(?P<url>\.\/.+)(?P<extension>\.rst)(?P<anchor>$|#)")
......@@ -119,22 +120,20 @@ author = 'Microsoft Corporation'
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
html_logo = os.path.join(CURR_PATH, 'logo', 'LightGBM_logo_grey_text.svg')
html_logo = str(CURR_PATH / 'logo' / 'LightGBM_logo_grey_text.svg')
# The name of an image file (relative to this directory) to use as a favicon of
# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
html_favicon = os.path.join(CURR_PATH, '_static', 'images', 'favicon.ico')
html_favicon = str(CURR_PATH / '_static' / 'images' / 'favicon.ico')
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
with open(os.path.join(CURR_PATH, os.path.pardir, 'VERSION.txt'), 'r') as f:
# The short X.Y version.
version = f.read().strip()
# The full version, including alpha/beta/rc tags.
release = version
# The short X.Y version.
version = (CURR_PATH.parent / 'VERSION.txt').read_text(encoding='utf-8').strip().replace('rc', '-rc')
# The full version, including alpha/beta/rc tags.
release = version
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
......@@ -158,7 +157,7 @@ if C_API:
'breathe',
])
breathe_projects = {
"LightGBM": os.path.join(CURR_PATH, 'doxyoutput', 'xml')
"LightGBM": str(CURR_PATH / 'doxyoutput' / 'xml')
}
breathe_default_project = "LightGBM"
breathe_domain_by_extension = {
......@@ -195,7 +194,7 @@ htmlhelp_basename = 'LightGBMdoc'
# The name of an image file (relative to this directory) to place at the top of
# the title page.
latex_logo = os.path.join(CURR_PATH, 'logo', 'LightGBM_logo_black_text_small.png')
latex_logo = str(CURR_PATH / 'logo' / 'LightGBM_logo_black_text_small.png')
def generate_doxygen_xml(app):
......@@ -206,10 +205,9 @@ def generate_doxygen_xml(app):
app : object
The application object representing the Sphinx process.
"""
input = os.path.join(CURR_PATH, os.path.pardir, 'include', 'LightGBM', 'c_api.h')
doxygen_args = [
f"INPUT={input}",
f"OUTPUT_DIRECTORY={os.path.join(CURR_PATH, 'doxyoutput')}",
f"INPUT={CURR_PATH.parent / 'include' / 'LightGBM' / 'c_api.h'}",
f"OUTPUT_DIRECTORY={CURR_PATH / 'doxyoutput'}",
"GENERATE_HTML=NO",
"GENERATE_LATEX=NO",
"GENERATE_XML=YES",
......@@ -226,8 +224,7 @@ def generate_doxygen_xml(app):
]
doxygen_input = '\n'.join(doxygen_args)
doxygen_input = bytes(doxygen_input, "utf-8")
if not os.path.exists(os.path.join(CURR_PATH, 'doxyoutput')):
os.makedirs(os.path.join(CURR_PATH, 'doxyoutput'))
(CURR_PATH / 'doxyoutput').mkdir(parents=True, exist_ok=True)
try:
# Warning! The following code can cause buffer overflows on RTD.
# Consider suppressing output completely if RTD project silently fails.
......@@ -268,10 +265,10 @@ def generate_r_docs(app):
r-roxygen2=7.1.1=r40h0357c0b_0
source /home/docs/.conda/bin/activate r_env
export TAR=/bin/tar
cd {os.path.join(CURR_PATH, os.path.pardir)}
cd {CURR_PATH.parent}
export R_LIBS="$CONDA_PREFIX/lib/R/library"
Rscript build_r.R || exit -1
cd {os.path.join(CURR_PATH, os.path.pardir, "lightgbm_r")}
cd {CURR_PATH.parent / "lightgbm_r"}
Rscript -e "roxygen2::roxygenize(load = 'installed')" || exit -1
Rscript -e "pkgdown::build_site( \
lazy = FALSE \
......@@ -284,7 +281,7 @@ def generate_r_docs(app):
, new_process = TRUE \
)
" || exit -1
cd {os.path.join(CURR_PATH, os.path.pardir)}
cd {CURR_PATH.parent}
"""
try:
# Warning! The following code can cause buffer overflows on RTD.
......@@ -312,9 +309,9 @@ def setup(app):
app : object
The application object representing the Sphinx process.
"""
first_run = not os.path.exists(os.path.join(CURR_PATH, '_FIRST_RUN.flag'))
first_run = not (CURR_PATH / '_FIRST_RUN.flag').exists()
if first_run and RTD:
open(os.path.join(CURR_PATH, '_FIRST_RUN.flag'), 'w').close()
(CURR_PATH / '_FIRST_RUN.flag').touch()
if C_API:
app.connect("builder-inited", generate_doxygen_xml)
else:
......@@ -323,8 +320,8 @@ def setup(app):
if first_run:
app.connect("builder-inited", generate_r_docs)
app.connect("build-finished",
lambda app, _: copy_tree(os.path.join(CURR_PATH, os.path.pardir, "lightgbm_r", "docs"),
os.path.join(app.outdir, "R"), verbose=0))
lambda app, _: copytree(CURR_PATH.parent / "lightgbm_r" / "docs",
Path(app.outdir) / "R"))
app.add_transform(InternalRefTransform)
add_js_file = getattr(app, 'add_js_file', False) or app.add_javascript
add_js_file("js/script.js")
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