Skip to content

Commit 8522010

Browse files
oraNodgotmax23
andauthored
ci: ensure ansible-core files exist before running docs build (#1756) (#1783)
Also, make KEEP_DIRS and KEEP_FILES constants to make the main function cleaner. Fixes: #1056 (cherry picked from commit 0d35022) Co-authored-by: Maxwell G <[email protected]>
1 parent 38df812 commit 8522010

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

docs/bin/clone-core.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,27 @@
1818
DEFAULT_BRANCH = (ROOT / "docs" / "ansible-core-branch.txt").read_text().strip()
1919
DEFAULT_ANSIBLE_CORE_REPO = "https://github.com/ansible/ansible"
2020

21+
KEEP_DIRS = (
22+
"bin",
23+
"lib",
24+
"packaging",
25+
"test/lib",
26+
)
27+
28+
KEEP_FILES = (
29+
"MANIFEST.in",
30+
"pyproject.toml",
31+
"requirements.txt",
32+
"setup.cfg",
33+
"setup.py",
34+
)
35+
2136

2237
@dataclasses.dataclass()
2338
class Args:
2439
branch: str | None
2540
repo: str
41+
check: bool
2642

2743

2844
def parse_args(args: list[str] | None = None) -> Args:
@@ -47,24 +63,25 @@ def parse_args(args: list[str] | None = None) -> Args:
4763
help="ansible-core repository to check out. Default: %(default)s",
4864
default=DEFAULT_ANSIBLE_CORE_REPO,
4965
)
66+
parser.add_argument(
67+
"--check",
68+
action=argparse.BooleanOptionalAction,
69+
help="Ensure that the necessary files exist."
70+
" If they don't clone new ones from ansible-core."
71+
" Otherwise, leave the existing versions alone.",
72+
)
5073
return Args(**vars(parser.parse_args(args)))
5174

5275

5376
def main(args: Args) -> None:
54-
keep_dirs = [
55-
"bin",
56-
"lib",
57-
"packaging",
58-
"test/lib",
59-
]
60-
61-
keep_files = [
62-
"MANIFEST.in",
63-
"pyproject.toml",
64-
"requirements.txt",
65-
"setup.cfg",
66-
"setup.py",
67-
]
77+
if (
78+
args.check
79+
and all(pathlib.Path(file).is_file() for file in KEEP_FILES)
80+
and all(pathlib.Path(directory).is_dir() for directory in KEEP_DIRS)
81+
):
82+
print("The necessary core files already exist.")
83+
print("Run 'nox -e clone-core' without --check to update the core files.")
84+
return
6885

6986
with tempfile.TemporaryDirectory() as temp_dir:
7087
cmd: list[str] = ["git", "clone", args.repo, "--depth=1"]
@@ -73,7 +90,7 @@ def main(args: Args) -> None:
7390
cmd.append(temp_dir)
7491
subprocess.run(cmd, check=True)
7592

76-
for keep_dir in keep_dirs:
93+
for keep_dir in KEEP_DIRS:
7794
src = pathlib.Path(temp_dir, keep_dir)
7895
dst = pathlib.Path.cwd() / keep_dir
7996

@@ -86,7 +103,7 @@ def main(args: Args) -> None:
86103

87104
(dst / ".gitignore").write_text("*")
88105

89-
for keep_file in keep_files:
106+
for keep_file in KEEP_FILES:
90107
src = pathlib.Path(temp_dir, keep_file)
91108
dst = pathlib.Path.cwd() / keep_file
92109

noxfile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def clone_core(session: nox.Session):
124124
]
125125

126126

127+
def _clone_core_check(session: nox.Session) -> None:
128+
"""
129+
Helper function to run the clone-core script with "--check"
130+
"""
131+
session.run("python", "docs/bin/clone-core.py", "--check")
132+
133+
127134
def _relaxed_parser(session: nox.Session) -> ArgumentParser:
128135
"""
129136
Generate an argument parser with a --relaxed option.
@@ -158,6 +165,7 @@ def checkers(session: nox.Session, test: str):
158165
args = _relaxed_parser(session).parse_args(session.posargs)
159166

160167
install(session, req="requirements-relaxed" if args.relaxed else "requirements")
168+
_clone_core_check(session)
161169
session.run("make", "-C", "docs/docsite", "clean", external=True)
162170
session.run("python", "tests/checkers.py", test)
163171

@@ -174,6 +182,7 @@ def make(session: nox.Session):
174182
args = parser.parse_args(session.posargs)
175183

176184
install(session, req="requirements-relaxed" if args.relaxed else "requirements")
185+
_clone_core_check(session)
177186
make_args: list[str] = [
178187
f"PYTHON={_env_python(session)}",
179188
*(args.make_args or ("clean", "coredocs")),

0 commit comments

Comments
 (0)