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