Skip to content

Commit

Permalink
Updated requirements updating logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Jul 22, 2020
1 parent 13d668d commit 40831b3
Showing 1 changed file with 76 additions and 72 deletions.
148 changes: 76 additions & 72 deletions repo_helper/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@

# 3rd party
import jinja2
from domdf_python_tools.paths import clean_writer
from domdf_python_tools.paths import clean_writer, PathPlus

# this package
from packaging.requirements import InvalidRequirement, Requirement

from repo_helper.blocks import (
create_docs_install_block,
create_docs_links_block,
Expand Down Expand Up @@ -76,44 +78,64 @@ def ensure_doc_requirements(repo_path: pathlib.Path, templates: jinja2.Environme
# TODO: preserve extras [] options

target_requirements = {
(templates.globals["sphinx_html_theme"], None),
("sphinxcontrib-httpdomain", "1.7.0"),
("sphinxemoji", "0.1.6"),
("sphinx-notfound-page", None),
("sphinx-tabs", "1.1.13"),
("autodocsumm", None),
# ("sphinx-gitstamp", None),
# ("gitpython", None),
("sphinx_autodoc_typehints", "1.11.0"),
("sphinx-copybutton", "0.2.12"), # https://sphinx-copybutton.readthedocs.io/en/latest/
("sphinx-prompt", "1.2.0"), # ("git+https://github.com/ScriptAutomate/sphinx-tabs-expanded.git", None)
Requirement("sphinxcontrib-httpdomain>=1.7.0"),
Requirement("sphinxemoji>=0.1.6"),
Requirement("sphinx-notfound-page"),
Requirement("sphinx-tabs>=1.1.13"),
Requirement("autodocsumm"),
# Requirement("sphinx-gitstamp"),
# Requirement("gitpython"),
Requirement("sphinx_autodoc_typehints>=1.11.0"),
Requirement("sphinx-copybutton>=0.2.12"), # https://sphinx-copybutton.readthedocs.io/en/latest/
Requirement("sphinx-prompt>=1.2.0"), # ("git+https://github.com/ScriptAutomate/sphinx-tabs-expanded.git", None)
}

if templates.globals["sphinx_html_theme"] == "sphinx_rtd_theme":
target_requirements.add(Requirement("sphinx_rtd_theme<0.5"))
else:
target_requirements.add(Requirement(templates.globals['sphinx_html_theme']))

if templates.globals["pypi_name"] != "extras_require":
target_requirements.add(("extras_require", None))
target_requirements.add(("sphinx", "3.0.3"))
target_requirements.add(Requirement("extras_require"))
target_requirements.add(Requirement("sphinx>=3.0.3"))

# TODO: only require sphinx if not in requirements.txt

req_file = repo_path / templates.globals["docs_dir"] / "requirements.txt"

if not req_file.is_file():
req_file.write_text('')
_target_requirement_names: List[str] = [r.name.casefold() for r in target_requirements]
_target_requirement_names += [r.replace("-", "_").casefold() for r in _target_requirement_names]
_target_requirement_names += [r.replace("_", "-").casefold() for r in _target_requirement_names]

req_file.write_text(
"\n".join(
line for line in req_file.read_text(encoding="UTF-8").splitlines()
if not line.startswith("git+") # FIXME
)
)
target_requirement_names = set(_target_requirement_names)

ensure_requirements(target_requirements, req_file)
req_file = PathPlus(repo_path / templates.globals["docs_dir"] / "requirements.txt")
req_file.parent.maybe_make(parents=True)

# test_req_file.write_text(
# "\n".join(
# line for line in test_req_file.read_text(encoding="UTF-8").splitlines()
# if not line.startswith("sphinx-tabs")
# ))
if not req_file.is_file():
req_file.touch()

comments = []

with req_file.open(encoding="UTF-8") as fp:
for line in fp.readlines():
if line.startswith("#"):
comments.append(line)
elif line:
try:
req = Requirement(line)
if req.name.casefold() not in target_requirement_names:
target_requirements.add(req)
except InvalidRequirement:
# TODO: Show warning to user
pass

with req_file.open('w', encoding="UTF-8") as fp:
for comment in comments:
fp.write(comment)
fp.write("\n")

for req in sorted(target_requirements, key=lambda r: r.name.casefold()):
fp.write(str(req))
fp.write("\n")

return [os.path.join(templates.globals["docs_dir"], "requirements.txt")]

Expand Down Expand Up @@ -173,16 +195,12 @@ def make_docutils_conf(repo_path: pathlib.Path, templates: jinja2.Environment) -

# TODO: use configupdater

docs_dir = repo_path / templates.globals["docs_dir"]

if not docs_dir.is_dir():
docs_dir.mkdir(parents=True)

with (docs_dir / "docutils.conf").open('w', encoding="UTF-8") as fp:
clean_writer(f"""\
docs_dir = PathPlus(repo_path / templates.globals["docs_dir"])
docs_dir.maybe_make(parents=True)
(docs_dir / "docutils.conf").write_clean(f"""\
[restructuredtext parser]
tab_width: 4
""", fp)
""")

return [os.path.join(templates.globals["docs_dir"], "docutils.conf")]

Expand Down Expand Up @@ -240,9 +258,7 @@ def make_conf(repo_path: pathlib.Path, templates: jinja2.Environment) -> List[st
if key not in templates.globals["html_theme_options"]:
templates.globals["html_theme_options"][key] = val

with (repo_path / templates.globals["docs_dir"] / "conf.py").open('w', encoding="UTF-8") as fp:
clean_writer(conf.render(), fp)
clean_writer("\n", fp)
PathPlus(repo_path / templates.globals["docs_dir"] / "conf.py").write_clean(conf.render())

return [os.path.join(templates.globals["docs_dir"], "conf.py")]

Expand All @@ -256,17 +272,15 @@ def copy_docs_styling(repo_path: pathlib.Path, templates: jinja2.Environment) ->
:type templates: jinja2.Environment
"""

dest__static_dir = repo_path / templates.globals["docs_dir"] / "_static"
dest__templates_dir = repo_path / templates.globals["docs_dir"] / "_templates"
dest__static_dir = PathPlus(repo_path / templates.globals["docs_dir"] / "_static")
dest__templates_dir = PathPlus(repo_path / templates.globals["docs_dir"] / "_templates")

for directory in [dest__static_dir, dest__templates_dir]:
if not directory.is_dir():
directory.mkdir(parents=True)
directory.maybe_make(parents=True)

if templates.globals["sphinx_html_theme"] == "sphinx_rtd_theme":
with (dest__static_dir / "style.css").open('w', encoding="UTF-8") as fp:
clean_writer(
f"""/* {templates.globals['managed_message']} */
PathPlus(dest__static_dir / "style.css").write_clean(
f"""/* {templates.globals['managed_message']} */
/* Body width */
.wy-nav-content {{max-width: 1200px !important;}}
Expand All @@ -278,14 +292,11 @@ def copy_docs_styling(repo_path: pathlib.Path, templates: jinja2.Environment) ->
html {{
scroll-behavior: smooth;
}}
""",
fp
)
""")

elif templates.globals["sphinx_html_theme"] == "alabaster":
with (dest__static_dir / "style.css").open('w', encoding="UTF-8") as fp:
clean_writer(
f"""/* {templates.globals['managed_message']} */
PathPlus(dest__static_dir / "style.css").write_clean(
f"""/* {templates.globals['managed_message']} */
li p:last-child {{ margin-bottom: 12px !important;}}
Expand Down Expand Up @@ -416,20 +427,15 @@ def copy_docs_styling(repo_path: pathlib.Path, templates: jinja2.Environment) ->
font-weight: bold;
}}
""",
fp
)
""")

with (dest__templates_dir / "layout.html").open('w', encoding="UTF-8") as fp:
clean_writer(
"""<!--- This file is managed by 'repo_helper'. Don't edit it directly. --->
PathPlus(dest__templates_dir / "layout.html").write_clean(
"""<!--- This file is managed by 'repo_helper'. Don't edit it directly. --->
{% extends "!layout.html" %}
{% block extrahead %}
<link href="{{ pathto("_static/style.css", True) }}" rel="stylesheet" type="text/css">
{% endblock %}
""",
fp
)
""")

if (dest__templates_dir / "footer.html").is_file():
(dest__templates_dir / "footer.html").unlink()
Expand Down Expand Up @@ -479,6 +485,7 @@ def rewrite_docs_index(repo_path: pathlib.Path, templates: jinja2.Environment) -
docker_name=templates.globals["docker_name"],
platforms=templates.globals["platforms"],
pre_commit=templates.globals["enable_pre_commit"],
on_pypi=templates.globals["on_pypi"],
)

if templates.globals["license"] == "GNU General Public License v2 (GPLv2)":
Expand Down Expand Up @@ -526,14 +533,13 @@ def make_404_page(repo_path: pathlib.Path, templates: jinja2.Environment) -> Lis
:type templates: jinja2.Environment
"""

docs_dir = repo_path / templates.globals["docs_dir"]
docs_dir = PathPlus(repo_path / templates.globals["docs_dir"])
_404_rst = docs_dir / "404.rst"
not_found_png = docs_dir / "not-found.png"

if not _404_rst.exists():
_404_template = templates.get_template("404.rst")
with _404_rst.open("w", encoding="UTF-8") as fp:
clean_writer(_404_template.render(), fp)
_404_rst.write_clean(_404_template.render())

if not not_found_png.exists():
shutil.copy2(template_dir / "not-found.png", not_found_png)
Expand All @@ -553,14 +559,13 @@ def make_docs_source_rst(repo_path: pathlib.Path, templates: jinja2.Environment)
:type templates: jinja2.Environment
"""

docs_dir = repo_path / templates.globals["docs_dir"]
docs_dir = PathPlus(repo_path / templates.globals["docs_dir"])
docs_source_rst = docs_dir / "Source.rst"
git_download_png = docs_dir / "git_download.png"

# if not docs_source_rst.exists():
source_template = templates.get_template("Source.rst")
with docs_source_rst.open("w", encoding="UTF-8") as fp:
clean_writer(source_template.render(), fp)
docs_source_rst.write_clean(source_template.render())

if not git_download_png.exists():
shutil.copy2(init_repo_template_dir / "git_download.png", git_download_png)
Expand All @@ -579,12 +584,11 @@ def make_docs_building_rst(repo_path: pathlib.Path, templates: jinja2.Environmen
:type templates: jinja2.Environment
"""

docs_dir = repo_path / templates.globals["docs_dir"]
docs_dir = PathPlus(repo_path / templates.globals["docs_dir"])
docs_building_rst = docs_dir / "Building.rst"

# if not docs_building_rst.exists():
building_template = templates.get_template("Building.rst")
with docs_building_rst.open("w", encoding="UTF-8") as fp:
clean_writer(building_template.render(), fp)
docs_building_rst.write_clean(building_template.render())

return [os.path.join(templates.globals["docs_dir"], "Building.rst")]

0 comments on commit 40831b3

Please sign in to comment.