~michel-slm/myrepos-utils

9efb3b8c73f5369f6083c7bfe415feceaac08f72 — Michel Lind 4 months ago 7ead682
Add mr-utils config command

Signed-off-by: Michel Lind <salimma@fedoraproject.org>
3 files changed, 95 insertions(+), 4 deletions(-)

M myrepos_utils/cli.py
M myrepos_utils/tests/test_utils.py
M myrepos_utils/utils.py
M myrepos_utils/cli.py => myrepos_utils/cli.py +28 -1
@@ 22,6 22,33 @@ def cli():
    pass


@cli.command(help="configure a new repository")
@click.argument("checkout_dir", type=str)
@click.argument("checkout_cmd", type=str)
@click.option("--checkout/--no-checkout", default=True)
@click.option("--config-path", type=str, default=utils.CONFIG_PATH)
@click.option(
    "--extra-git-remotes", nargs=2, type=click.Tuple([str, str]), multiple=True
)
@click.option("--git-configs", nargs=2, type=click.Tuple([str, str]), multiple=True)
def config(
    checkout_dir: str,
    checkout_cmd: str,
    checkout: bool,
    config_path: str,
    extra_git_remotes: list[(str, str)],
    git_configs: list[(str, str)],
):
    utils.config_repo(
        checkout_dir=checkout_dir,
        checkout_cmd=checkout_cmd,
        checkout=checkout,
        config_path=config_path,
        extra_git_remotes=extra_git_remotes,
        git_configs=git_configs,
    )


@cli.command(help="find repo directories tracked by myrepos")
@click.argument("query", nargs=-1)
def find(query: list[str]):


@@ 29,9 56,9 @@ def find(query: list[str]):
    for repo in matches:
        click.echo(repo)


@cli.command(help="sort ~/.mrconfig")
def sort():
    sorted_config = utils.sort()
    with open(os.path.expanduser("~/.mrconfig"), "w") as f:
        sorted_config.write(f)


M myrepos_utils/tests/test_utils.py => myrepos_utils/tests/test_utils.py +2 -1
@@ 48,9 48,10 @@ def test_find_repo():
    mock_config.sections.assert_called_once()
    assert result == [os.path.expanduser("~/src/gitlab/owner2/projB")]


def test_sort():
    mock_config = get_mock_config()
    result = utils.sort(mock_config)
    assert len(result) == len(REPOS) + 1 # DEFAULT
    assert len(result) == len(REPOS) + 1  # DEFAULT
    assert set(result.sections()) == set(REPOS.keys())
    assert result.sections() == sorted(result.sections())

M myrepos_utils/utils.py => myrepos_utils/utils.py +65 -2
@@ 12,15 12,78 @@

import configparser
import os
import subprocess

CONFIG = configparser.ConfigParser()
CONFIG.read(os.path.expanduser("~/.mrconfig"))
CONFIG_PATH = os.path.expanduser("~/.mrconfig")
CONFIG.read(CONFIG_PATH)


def get_config_repo_cmd(
    checkout_dir: str,
    checkout_cmd: str,
    config_path: str = CONFIG_PATH,
    extra_git_remotes: list[(str, str)] = None,
    git_configs: list[(str, str)] = None,
) -> list[str]:
    relpath = get_relpath(checkout_dir, config_path)
    checkout_full_cmd_list = [checkout_cmd]

    if extra_git_remotes or git_configs:
        repo_name = os.path.basename(relpath)
        checkout_full_cmd_list.append(f"cd {repo_name}")

    if git_configs:
        for key, value in git_configs:
            checkout_full_cmd_list.append(f"git config {key} {value}")

    if extra_git_remotes:
        for remote_name, remote_url in extra_git_remotes:
            checkout_full_cmd_list.append(f"git remote add {remote_name} {remote_url}")

    checkout_full_cmd = " && ".join(checkout_full_cmd_list)

    mr_cmd = ["mr", "config", relpath, f"checkout={checkout_full_cmd}"]
    return mr_cmd


def get_relpath(path: str, config_path: str = CONFIG_PATH) -> str:
    full_path = os.path.join(os.getcwd(), path)
    common_prefix = os.path.commonprefix([full_path, config_path])
    relpath = os.path.relpath(full_path, common_prefix)
    return relpath


def get_repos(config: configparser.ConfigParser = CONFIG) -> list[str]:
    return list(config.sections())


def config_repo(
    checkout_dir: str,
    checkout_cmd: str,
    checkout: bool,
    config_path: str = CONFIG_PATH,
    extra_git_remotes: list[(str, str)] = None,
    git_configs: list[(str, str)] = None,
) -> int:
    mr_cmd = get_config_repo_cmd(
        checkout_dir=checkout_dir,
        checkout_cmd=checkout_cmd,
        config_path=config_path,
        extra_git_remotes=extra_git_remotes,
    )
    res = subprocess.run(mr_cmd)

    if not checkout:
        return res.returncode

    if res.returncode != 0:
        return res.returncode

    res = subprocess.run(["mr", "-d", checkout_dir, "co"])
    return res.returncode


def find_repo(
    query: list[str], config: configparser.ConfigParser = CONFIG
) -> list[str]:


@@ 30,6 93,7 @@ def find_repo(
    r = f".*{'.*'.join(query)}.*"
    return [os.path.expanduser(f"~/{repo}") for repo in repos if re.match(r, repo)]


def sort(config: configparser.ConfigParser = CONFIG) -> configparser.ConfigParser:
    sorted_config = configparser.ConfigParser()
    for sec in sorted(config.sections()):


@@ 37,4 101,3 @@ def sort(config: configparser.ConfigParser = CONFIG) -> configparser.ConfigParse
        for k, v in config[sec].items():
            sorted_config[sec][k] = v
    return sorted_config