62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
import argparse
|
|
from pathlib import Path
|
|
from typing import Sequence
|
|
|
|
from many_repos import common
|
|
from many_repos import source_type
|
|
from many_repos.config import load_config, Config
|
|
|
|
|
|
def _add_specific_args(parser: argparse.ArgumentParser):
|
|
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
|
|
|
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description=(
|
|
"Clone all repositories into `output_dir` from every source, unless specified "
|
|
"otherwise using one or multiple `--source SOURCE` / `-s SOURCE` ."
|
|
)
|
|
)
|
|
|
|
_add_specific_args(parser)
|
|
common.add_common_args(parser)
|
|
|
|
args = parser.parse_args(argv)
|
|
config = load_config(args.config_filename)
|
|
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)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|