|
| 1 | +import asyncio |
1 | 2 | import click
|
| 3 | +import git |
| 4 | +from pathlib import Path |
2 | 5 |
|
3 | 6 | from mkdocs.__main__ import cli as mkdocs_cli
|
| 7 | +from mkdocs.config import load_config |
4 | 8 | from pulp_docs.context import ctx_blog, ctx_docstrings, ctx_draft
|
5 | 9 |
|
6 | 10 |
|
@@ -43,7 +47,58 @@ def draft_callback(ctx: click.Context, param: click.Parameter, value: bool) -> b
|
43 | 47 | help="Don't fail if repositories are missing.",
|
44 | 48 | )
|
45 | 49 |
|
| 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 | + |
46 | 100 | main = mkdocs_cli
|
| 101 | +main.add_command(fetch) |
47 | 102 |
|
48 | 103 | for command_name in ["build", "serve"]:
|
49 | 104 | sub_command = main.commands.get(command_name)
|
|
0 commit comments