Skip to content

Commit ac7609b

Browse files
committed
improve configuration
1 parent 0adea7f commit ac7609b

File tree

4 files changed

+72
-32
lines changed

4 files changed

+72
-32
lines changed

docsrc/poly.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
from datetime import datetime
1+
from functools import partial
22
from pathlib import Path
33

44
from sphinx_polyversion.api import apply_overrides
55
from sphinx_polyversion.driver import DefaultDriver
6-
from sphinx_polyversion.git import Git, GitRef, GitRefType, file_predicate, refs_by_type
6+
from sphinx_polyversion.git import Git, file_predicate, refs_by_type, closest_tag
77
from sphinx_polyversion.pyvenv import Pip
88
from sphinx_polyversion.sphinx import SphinxBuilder, Placeholder
99

10-
#: Regex matching the branches to build docs for
11-
BRANCH_REGEX = r"master-doctest"
10+
#: CodeRegex matching the branches to build docs for
11+
BRANCH_REGEX = r"(doc-polyversion)"
1212

1313
#: Regex matching the tags to build docs for
14-
TAG_REGEX = r"v1.1.6"
14+
TAG_REGEX = r"v1.1.6.1"
1515

1616
#: Output dir relative to project root
1717
OUTPUT_DIR = "_polybuild"
1818

1919
#: Source directory
2020
SOURCE_DIR = "docsrc/"
2121

22-
#: Arguments to pass to `pip install`
23-
POETRY_ARGS = "bayesflow"
24-
2522
#: Arguments to pass to `sphinx-build`
2623
SPHINX_ARGS = "-a -v"
2724

28-
#: Mock data used for building local version
29-
MOCK_DATA = {
30-
"revisions": [
31-
GitRef("v1.1.6", "", "", GitRefType.TAG, datetime.fromtimestamp(0)),
32-
GitRef("master-doctest", "", "", GitRefType.BRANCH, datetime.fromtimestamp(3)),
33-
],
34-
"current": GitRef("master-doctest", "", "", GitRefType.TAG, datetime.fromtimestamp(6)),
35-
}
25+
#: Extra packages for building docs
26+
SPHINX_DEPS = [
27+
"sphinx",
28+
"numpydoc",
29+
"myst-nb",
30+
"sphinx_design",
31+
"sphinx-book-theme",
32+
"sphinxcontrib-bibtex",
33+
"sphinx-polyversion==1.0.0",
34+
]
35+
36+
BACKEND_DEPS = [
37+
"jax",
38+
"torch",
39+
"tensorflow",
40+
]
3641

3742

3843
#: Data passed to templates
@@ -60,7 +65,19 @@ def root_data(driver):
6065
apply_overrides(globals())
6166
# Determine repository root directory
6267
root = Git.root(Path(__file__).parent)
63-
print("root", root)
68+
69+
70+
async def selector(f, a, b):
71+
return a.name
72+
73+
74+
# Setup environments for the different versions
75+
76+
ENVIRONMENT = {
77+
None: Pip.factory(venv=Path(".venv"), args=["-vv", "bayesflow==1.1.6"] + SPHINX_DEPS),
78+
"doc-polyversion": Pip.factory(venv=Path(".venv/dev"), args=["-vv", "-e", "."] + SPHINX_DEPS + BACKEND_DEPS),
79+
"v1.1.6": Pip.factory(venv=Path(".venv/v1.1.6"), args=["-vv", "bayesflow==1.1.6"] + SPHINX_DEPS),
80+
}
6481

6582
# Setup driver and run it
6683
src = Path(SOURCE_DIR)
@@ -74,9 +91,12 @@ def root_data(driver):
7491
predicate=file_predicate([src]), # exclude refs without source dir
7592
),
7693
builder=SphinxBuilder(
77-
src / "source", args=SPHINX_ARGS.split(), pre_cmd=["python", "pre-build.py", Placeholder.SOURCE_DIR]
94+
src / "source",
95+
args=SPHINX_ARGS.split(),
96+
pre_cmd=["python", root / src / "pre-build.py", Placeholder.SOURCE_DIR],
7897
),
79-
env=Pip.factory(args=POETRY_ARGS.split(), venv="buildvenv"),
98+
env=ENVIRONMENT,
99+
selector=partial(selector, partial(closest_tag, root)),
80100
template_dir=root / src / "polyversion/templates",
81101
static_dir=root / src / "polyversion/static",
82102
data_factory=data,

docsrc/pre-build.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
import shutil
33
import sys
44
from pathlib import Path
5+
from sphinx_polyversion.git import Git
56

67

78
def copy_files(sourcedir):
89
basedir = Path(sourcedir).parent.parent
910
print(basedir, sourcedir)
1011

12+
# copy examples
1113
examples_src = os.path.join(basedir, "examples")
1214
examples_dst = os.path.join(sourcedir, "_examples")
1315
if os.path.exists(examples_src):
@@ -16,6 +18,7 @@ def copy_files(sourcedir):
1618
examples_in_progress = os.path.join(examples_dst, "in_progress")
1719
if os.path.exists(examples_in_progress):
1820
shutil.rmtree(examples_in_progress)
21+
# copy contributing and installation
1922
contributing_src = os.path.join(basedir, "CONTRIBUTING.md")
2023
contributing_dst = os.path.join(sourcedir, "contributing.md")
2124
if os.path.exists(contributing_src):
@@ -24,11 +27,36 @@ def copy_files(sourcedir):
2427
installation_dst = os.path.join(sourcedir, "installation.rst")
2528
if os.path.exists(installation_src):
2629
shutil.copy2(installation_src, installation_dst)
27-
print(os.listdir(sourcedir))
28-
print(os.listdir(examples_dst))
30+
# create empty bibtex file if none exists
31+
bibtex_path = os.path.join(sourcedir, "references.bib")
32+
if not os.path.exists(bibtex_path):
33+
open(bibtex_path, "a").close()
34+
35+
36+
def patch_conf(sourcedir):
37+
root = Git.root(Path(__file__).parent)
38+
cursrc = os.path.join(root, "docsrc", "source")
39+
# copy the configuration file: shared for all versions
40+
conf_src = os.path.join(cursrc, "conf.py")
41+
conf_dst = os.path.join(sourcedir, "conf.py")
42+
if os.path.exists(conf_src):
43+
print("Overwriting old conf.py with current conf.py")
44+
shutil.copy2(conf_src, conf_dst)
45+
# copy HTML and CSS for versioning sidebar
46+
versioning_src = os.path.join(cursrc, "_templates", "versioning.html")
47+
versioning_dst = os.path.join(sourcedir, "_templates", "versioning.html")
48+
if os.path.exists(versioning_src):
49+
os.makedirs(os.path.join(sourcedir, "_templates"), exist_ok=True)
50+
shutil.copy2(versioning_src, versioning_dst)
51+
css_src = os.path.join(cursrc, "_static", "custom.css")
52+
css_dst = os.path.join(sourcedir, "_static", "custom.css")
53+
if os.path.exists(css_src):
54+
os.makedirs(os.path.join(sourcedir, "_static"), exist_ok=True)
55+
shutil.copy2(css_src, css_dst)
2956

3057

3158
if __name__ == "__main__":
3259
print("Running pre-build script") # move files around if necessary
3360
sourcedir = sys.argv[1]
3461
copy_files(sourcedir)
62+
patch_conf(sourcedir)

docsrc/source/_templates/versioning.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<p class="caption" aria-level="2" role="heading"><span class="caption-text">{{ _('Tags') }}</span></p>
55
<ul>
66
{%- for item in tags %}
7-
<li><a href="../{{ item.name }}/index.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
7+
<li><a href="/{{ item.name }}/index.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
88
{%- endfor %}
99
</ul>
1010
{% endif %}
1111
{% if branches %} {# List of branches #}
1212
<p class="caption" aria-level="2" role="heading"><span class="caption-text">{{ _('Branches') }}</span></p>
1313
<ul>
1414
{%- for item in branches %}
15-
<li><a href="../{{ item.name }}/index.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
15+
<li><a href="/{{ item.name }}/index.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
1616
{%- endfor %}
1717
</ul>
1818
{% endif %}

docsrc/source/conf.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,6 @@
133133

134134
autosummmary_generate = True
135135

136-
137-
def cleanup_handler(app, exception):
138-
print("Done, what now?")
139-
140-
141-
def setup(app):
142-
app.connect("build-finished", cleanup_handler)
143-
144-
136+
# versioning data for template
145137
data = load(globals())
146138
current: GitRef = data["current"]

0 commit comments

Comments
 (0)