Skip to content

Commit 9afa67d

Browse files
committed
1 parent c0fcaa9 commit 9afa67d

6 files changed

Lines changed: 114 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,6 @@ bazel-*
145145

146146
.DS_Store
147147
.idea/*
148+
149+
# Docs bundled from docsite repo at build time; populated by `uv build` or `smart-tests get docs` in dev
150+
smart_tests/docs/

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ build-backend = "setuptools.build_meta"
6161
packages = ["smart_tests"]
6262

6363
[tool.setuptools.package-data]
64-
smart_tests = ["jar/exe_deploy.jar"]
64+
smart_tests = ["jar/exe_deploy.jar", "docs/**/*"]
6565

6666
[tool.setuptools_scm]

setup.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
import shutil
2+
import subprocess
3+
import tempfile
4+
from pathlib import Path
5+
16
from setuptools import setup
7+
from setuptools.command.build_py import build_py
8+
9+
10+
class BuildPyWithDocs(build_py):
11+
def run(self):
12+
docs_dst = Path(__file__).parent / 'smart_tests' / 'docs'
13+
if docs_dst.exists():
14+
shutil.rmtree(docs_dst)
15+
16+
with tempfile.TemporaryDirectory() as tmpdir:
17+
subprocess.run(
18+
['git', 'clone', '--depth=1',
19+
'git@github.com:cloudbees/docsite-cloudbees-smart-tests.git', tmpdir],
20+
check=True,
21+
)
22+
shutil.copytree(Path(tmpdir) / 'docs', docs_dst)
23+
24+
super().run()
25+
226

3-
setup()
27+
setup(cmdclass={'build_py': BuildPyWithDocs})

smart_tests/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from smart_tests.commands.compare import compare
1010
from smart_tests.commands.detect_flakes import detect_flakes
1111
from smart_tests.commands.gate import gate
12+
from smart_tests.commands.get import get
1213
from smart_tests.commands.inspect import inspect
1314
from smart_tests.commands.record import record
1415
from smart_tests.commands.stats import stats
@@ -25,6 +26,7 @@
2526
cli.add_command(compare)
2627
cli.add_command(detect_flakes)
2728
cli.add_command(gate)
29+
cli.add_command(get)
2830

2931

3032
def _load_test_runners():
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ... import args4p
2+
from ...app import Application
3+
from .docs import docs
4+
5+
6+
@args4p.group(help="Retrieve resources")
7+
def get(app: Application):
8+
return app
9+
10+
11+
get.add_command(docs)

smart_tests/commands/get/docs.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import importlib.resources
2+
import shutil
3+
import subprocess
4+
import sys
5+
import tempfile
6+
from pathlib import Path
7+
8+
import click
9+
10+
from ... import args4p
11+
from ...app import Application
12+
13+
DOCSITE_REPO = 'git@github.com:cloudbees/docsite-cloudbees-smart-tests.git'
14+
OUTPUT_DIR = 'smart-tests-docs'
15+
16+
17+
def _find_bundled_docs() -> Path | None:
18+
"""Return the path to the bundled docs/ directory, or None if not present."""
19+
pkg = importlib.resources.files('smart_tests') / 'docs'
20+
# files() returns a Traversable; check it's a real, non-empty directory
21+
try:
22+
p = Path(str(pkg))
23+
if p.is_dir() and any(p.iterdir()):
24+
return p
25+
except (TypeError, OSError):
26+
pass
27+
return None
28+
29+
30+
def _fetch_docs_via_git() -> Path:
31+
"""
32+
Clone the docsite repo into smart_tests/docs/ (relative to this package's
33+
location) and return the path to the docs/ subdirectory.
34+
35+
If smart_tests/docs/ already exists, run `git pull` instead of cloning.
36+
"""
37+
package_dir = Path(__file__).parent.parent.parent # smart_tests/
38+
docs_dst = package_dir / 'docs'
39+
40+
if docs_dst.is_dir():
41+
click.echo(f"Updating docs from {DOCSITE_REPO} ...")
42+
subprocess.run(['git', '-C', str(docs_dst), 'pull'], check=True)
43+
else:
44+
click.echo(f"Cloning docs from {DOCSITE_REPO} ...")
45+
with tempfile.TemporaryDirectory() as tmpdir:
46+
subprocess.run(
47+
['git', 'clone', '--depth=1', DOCSITE_REPO, tmpdir],
48+
check=True,
49+
)
50+
shutil.copytree(Path(tmpdir) / 'docs', docs_dst)
51+
52+
return docs_dst
53+
54+
55+
@args4p.command(help="Copy product documentation into ./smart-tests-docs")
56+
def docs(app: Application):
57+
output = Path(OUTPUT_DIR)
58+
if output.exists():
59+
click.secho(
60+
f"'{OUTPUT_DIR}' already exists. Please delete it first, then re-run this command.",
61+
fg='red', err=True,
62+
)
63+
sys.exit(1)
64+
65+
docs_src = _find_bundled_docs()
66+
if docs_src is None:
67+
# Dev mode: not running from an installed package
68+
docs_src = _fetch_docs_via_git()
69+
70+
click.echo(f"Copying docs to ./{OUTPUT_DIR} ...")
71+
shutil.copytree(docs_src, output)
72+
click.echo(f"Done. Documentation is in ./{OUTPUT_DIR}")

0 commit comments

Comments
 (0)