mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Use `_StrPath
in
sphinx.cmd.make_mode
`
This commit is contained in:
parent
2137d0d2eb
commit
de24fd0b76
@ -13,11 +13,11 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from contextlib import chdir
|
from contextlib import chdir
|
||||||
from os import path
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import sphinx
|
import sphinx
|
||||||
from sphinx.cmd.build import build_main
|
from sphinx.cmd.build import build_main
|
||||||
|
from sphinx.util._pathlib import _StrPath
|
||||||
from sphinx.util.console import blue, bold, color_terminal, nocolor
|
from sphinx.util.console import blue, bold, color_terminal, nocolor
|
||||||
from sphinx.util.osutil import rmtree
|
from sphinx.util.osutil import rmtree
|
||||||
|
|
||||||
@ -57,30 +57,36 @@ BUILDERS = [
|
|||||||
|
|
||||||
|
|
||||||
class Make:
|
class Make:
|
||||||
def __init__(self, *, source_dir: str, build_dir: str, opts: Sequence[str]) -> None:
|
def __init__(
|
||||||
self.source_dir = source_dir
|
self,
|
||||||
self.build_dir = build_dir
|
*,
|
||||||
|
source_dir: str | os.PathLike[str],
|
||||||
|
build_dir: str | os.PathLike[str],
|
||||||
|
opts: Sequence[str],
|
||||||
|
) -> None:
|
||||||
|
self.source_dir = _StrPath(source_dir)
|
||||||
|
self.build_dir = _StrPath(build_dir)
|
||||||
self.opts = [*opts]
|
self.opts = [*opts]
|
||||||
|
|
||||||
def build_dir_join(self, *comps: str) -> str:
|
def build_dir_join(self, *comps: str | os.PathLike[str]) -> _StrPath:
|
||||||
return path.join(self.build_dir, *comps)
|
return self.build_dir.joinpath(*comps)
|
||||||
|
|
||||||
def build_clean(self) -> int:
|
def build_clean(self) -> int:
|
||||||
source_dir = path.abspath(self.source_dir)
|
source_dir = self.source_dir.resolve()
|
||||||
build_dir = path.abspath(self.build_dir)
|
build_dir = self.build_dir.resolve()
|
||||||
if not path.exists(self.build_dir):
|
if not self.build_dir.exists():
|
||||||
return 0
|
return 0
|
||||||
elif not path.isdir(self.build_dir):
|
elif not self.build_dir.is_dir():
|
||||||
print('Error: %r is not a directory!' % self.build_dir)
|
print("Error: '%s' is not a directory!" % self.build_dir)
|
||||||
return 1
|
return 1
|
||||||
elif source_dir == build_dir:
|
elif source_dir == build_dir:
|
||||||
print('Error: %r is same as source directory!' % self.build_dir)
|
print("Error: '%s' is same as source directory!" % self.build_dir)
|
||||||
return 1
|
return 1
|
||||||
elif path.commonpath([source_dir, build_dir]) == build_dir:
|
elif source_dir.is_relative_to(build_dir):
|
||||||
print('Error: %r directory contains source directory!' % self.build_dir)
|
print("Error: '%s' directory contains source directory!" % self.build_dir)
|
||||||
return 1
|
return 1
|
||||||
print('Removing everything under %r...' % self.build_dir)
|
print("Removing everything under '%s'..." % self.build_dir)
|
||||||
for item in os.listdir(self.build_dir):
|
for item in self.build_dir.iterdir():
|
||||||
rmtree(self.build_dir_join(item))
|
rmtree(self.build_dir_join(item))
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@ -179,7 +185,9 @@ class Make:
|
|||||||
return 1
|
return 1
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def run_generic_build(self, builder: str, doctreedir: str | None = None) -> int:
|
def run_generic_build(
|
||||||
|
self, builder: str, doctreedir: str | os.PathLike[str] | None = None
|
||||||
|
) -> int:
|
||||||
# compatibility with old Makefile
|
# compatibility with old Makefile
|
||||||
paper_size = os.getenv('PAPER', '')
|
paper_size = os.getenv('PAPER', '')
|
||||||
if paper_size in {'a4', 'letter'}:
|
if paper_size in {'a4', 'letter'}:
|
||||||
@ -191,9 +199,9 @@ class Make:
|
|||||||
'--builder',
|
'--builder',
|
||||||
builder,
|
builder,
|
||||||
'--doctree-dir',
|
'--doctree-dir',
|
||||||
doctreedir,
|
str(doctreedir),
|
||||||
self.source_dir,
|
str(self.source_dir),
|
||||||
self.build_dir_join(builder),
|
str(self.build_dir_join(builder)),
|
||||||
]
|
]
|
||||||
return build_main(args + self.opts)
|
return build_main(args + self.opts)
|
||||||
|
|
||||||
|
@ -236,7 +236,7 @@ class FileAvoidWrite:
|
|||||||
return getattr(self._io, name)
|
return getattr(self._io, name)
|
||||||
|
|
||||||
|
|
||||||
def rmtree(path: str) -> None:
|
def rmtree(path: str | os.PathLike[str], /) -> None:
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
shutil.rmtree(path)
|
shutil.rmtree(path)
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user