many-repos/many_repos/clone.py

63 lines
1.9 KiB
Python
Raw Normal View History

2024-05-25 23:43:15 +02:00
import argparse
2024-05-27 08:55:13 +02:00
from pathlib import Path
2024-05-25 23:43:15 +02:00
from typing import Sequence
from many_repos import common
2024-05-27 08:55:13 +02:00
from many_repos import source_type
from many_repos.config import load_config, Config
2024-05-25 23:43:15 +02:00
def _add_specific_args(parser: argparse.ArgumentParser):
2024-05-27 08:55:13 +02:00
parser.add_argument("-s", "--source", help="limit cloning repositores only to these sources", action="append")
def _construct_path(repo: common.Repository, config: Config) -> Path:
return (Path(config.output_dir) / repo.vcs / repo.namespace / repo.name).resolve()
def _create_path(path: Path):
print(f"Creating `{path}`...")
path.mkdir(parents=True, exist_ok=True)
def _get_repositories_to_clone(config: Config, sources: list[str] | None = None) -> list[common.Repository]:
repos = []
for name, source_config in config.sources_configs.items():
if sources and name not in sources:
continue
print(f"Getting {name} repositories..")
source_cls = source_type.get(source_config.source_type)
source = source_cls(source_config)
all_repos = source.get_repositories()
filtered_repos = source.filter_repositories(all_repos)
repos.extend(filtered_repos)
return repos
2024-05-25 23:43:15 +02:00
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description=(
2024-05-27 08:55:13 +02:00
"Clone all repositories into `output_dir` from every source, unless specified "
2024-05-25 23:43:15 +02:00
"otherwise using one or multiple `--source SOURCE` / `-s SOURCE` ."
)
)
_add_specific_args(parser)
common.add_common_args(parser)
2024-05-27 08:55:13 +02:00
args = parser.parse_args(argv)
2024-05-25 23:43:15 +02:00
config = load_config(args.config_filename)
2024-05-27 08:55:13 +02:00
repositories = _get_repositories_to_clone(config, args.source)
for repository in repositories:
repo_path = _construct_path(repository, config)
if not repo_path.exists():
_create_path(repo_path)
2024-05-25 23:43:15 +02:00
return 0
2024-05-27 08:55:13 +02:00
2024-05-25 23:43:15 +02:00
if __name__ == "__main__":
raise SystemExit(main())