Skip to content

Commit 9c0d10d

Browse files
pedro-psbmdellweg
andcommitted
Add fetch command to clone repositories
Update src/pulp_docs/cli.py Co-authored-by: Matthias Dellweg <[email protected]>
1 parent 051b3c8 commit 9c0d10d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/pulp_docs/cli.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import asyncio
12
import click
3+
import git
4+
from pathlib import Path
25

36
from mkdocs.__main__ import cli as mkdocs_cli
7+
from mkdocs.config import load_config
48
from pulp_docs.context import ctx_blog, ctx_docstrings, ctx_draft
59

610

@@ -43,7 +47,58 @@ def draft_callback(ctx: click.Context, param: click.Parameter, value: bool) -> b
4347
help="Don't fail if repositories are missing.",
4448
)
4549

50+
51+
async def clone_repositories(repositories: list[str], dest_dir: Path) -> None:
52+
"""Clone multiple repositories concurrently."""
53+
54+
async def clone_repository(repo_url: str) -> None:
55+
repo_name = repo_url.split("/")[-1]
56+
repo_path = dest_dir / repo_name
57+
if repo_path.exists():
58+
click.echo(
59+
f"Repository {repo_name} already exists at {repo_path}, skipping."
60+
)
61+
return
62+
click.echo(f"Cloning {repo_url} to {repo_path}...")
63+
loop = asyncio.get_running_loop()
64+
await loop.run_in_executor(
65+
None, lambda: git.Repo.clone_from(repo_url, repo_path, depth=1)
66+
)
67+
click.echo(f"Successfully cloned {repo_name}")
68+
69+
tasks = [clone_repository(repo) for repo in repositories]
70+
await asyncio.gather(*tasks)
71+
72+
73+
@click.command()
74+
@click.option(
75+
"--dest",
76+
required=True,
77+
type=click.Path(file_okay=False),
78+
help="Destination directory for cloned repositories",
79+
)
80+
@click.option(
81+
"-f",
82+
"--config-file",
83+
type=click.Path(exists=True, dir_okay=False),
84+
default="mkdocs.yml",
85+
envvar="PULPDOCS_DIR",
86+
help="Path to mkdocs.yml config file",
87+
)
88+
def fetch(dest, config_file):
89+
"""Fetch repositories to destination dir."""
90+
dest_path = Path(dest)
91+
config = load_config(config_file)
92+
components = config.plugins["PulpDocs"].config.components
93+
repositories_list = list({r.git_url for r in components})
94+
95+
if not dest_path.exists():
96+
dest_path.mkdir(parents=True)
97+
asyncio.run(clone_repositories(repositories_list, dest_path))
98+
99+
46100
main = mkdocs_cli
101+
main.add_command(fetch)
47102

48103
for command_name in ["build", "serve"]:
49104
sub_command = main.commands.get(command_name)

0 commit comments

Comments
 (0)