~michel-slm/myrepos-utils

3d77db259ae41895f62f2cbfe1fb49fa85606b65 — Michel Alexandre Salim 1 year, 5 months ago 3399f84 main v0.0.2
Add sort command

Signed-off-by: Michel Alexandre Salim <michel@michel-slm.name>
M README.md => README.md +7 -0
@@ 2,6 2,7 @@

## Usage

### find
Let's say you have the following repositories configured in `~/.mrconfig`:

```


@@ 18,3 19,9 @@ cd (mr-utils find github projA)
```

If there are multiple matches, they will be printed out.

### sort
This will sort `~/.mrconfig` based on sections and write it back out
```
mr-utils sort
```

M myrepos_utils/__init__.py => myrepos_utils/__init__.py +1 -1
@@ 12,4 12,4 @@
# For more information on free software, see
# <https://www.gnu.org/philosophy/free-sw.en.html>.

__version__ = "0.0.1.1"
__version__ = "0.0.2"

M myrepos_utils/cli.py => myrepos_utils/cli.py +8 -2
@@ 14,6 14,7 @@

import click
import json
import os
import sys
from . import utils



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

    matches = utils.find_repo(query)
    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 +26 -11
@@ 12,32 12,47 @@
# For more information on free software, see
# <https://www.gnu.org/philosophy/free-sw.en.html>.

import configparser
import os
from myrepos_utils import utils
from unittest import mock


REPOS = [
    "src/github/owner1/projA",
    "src/gitlab/owner2/projB",
]
REPOS = {
    "src/github/owner1/projA": {"checkout": "github clone owner1/projA"},
    "src/gitlab/owner2/projB": {"checkout": "gitlab clone owner2/projB"},
    "src/github/owner0/projC": {"checkout": "github clone owner0/projC"},
}


def get_mock_config():
    mock_config = mock.Mock()
    mock_config.keys.return_value = iter(REPOS)
    config = configparser.ConfigParser()
    config.read_dict(REPOS)
    mock_config = mock.MagicMock()
    mock_config.keys.return_value = config.keys()
    mock_config.sections.return_value = config.sections()
    mock_config.__getitem__.side_effect = config.__getitem__
    return mock_config


def test_get_repos():
    mock_config = get_mock_config()
    result = utils.get_repos(mock_config)
    mock_config.keys.assert_called_once()
    assert result == REPOS
    # keys would include DEFAULT, it should not be called
    mock_config.keys.assert_not_called()
    mock_config.sections.assert_called_once()
    assert result == list(REPOS.keys())


def test_find_repo():
    mock_config = get_mock_config()
    result = utils.find_repo("github", mock_config)
    mock_config.keys.assert_called_once()
    assert result == [os.path.expanduser(f"~/{REPOS[0]}")]
    result = utils.find_repo("gitlab", mock_config)
    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 set(result.sections()) == set(REPOS.keys())
    assert result.sections() == sorted(result.sections())

M myrepos_utils/utils.py => myrepos_utils/utils.py +10 -1
@@ 20,7 20,7 @@ CONFIG.read(os.path.expanduser("~/.mrconfig"))


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


def find_repo(


@@ 31,3 31,12 @@ def find_repo(
    repos = get_repos(config)
    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()):
        sorted_config.add_section(sec)
        for k, v in config[sec].items():
            sorted_config[sec][k] = v
    return sorted_config