Skip to content

Commit

Permalink
refactor!: dir -> path (libvcs 0.27.0 and avoid overshadowing builtin)
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Feb 7, 2024
1 parent cb3fb02 commit 60c883e
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion scripts/generate_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@

config[path][reponame] = {
"name": reponame,
"dir": path / reponame,
"path": path / reponame,
"url": f"git+ssh://{url_to_repo}",
"remotes": {
"origin": GitRemote(
Expand Down
8 changes: 4 additions & 4 deletions src/vcspull/cli/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ def sync(
found_repos = []

for repo_pattern in repo_patterns:
dir, vcs_url, name = None, None, None
path, vcs_url, name = None, None, None
if any(repo_pattern.startswith(n) for n in ["./", "/", "~", "$HOME"]):
dir = repo_pattern
path = repo_pattern
elif any(repo_pattern.startswith(n) for n in ["http", "git", "svn", "hg"]):
vcs_url = repo_pattern
else:
name = repo_pattern

# collect the repos from the config files
found = filter_repos(configs, dir=dir, vcs_url=vcs_url, name=name)
found = filter_repos(configs, path=path, vcs_url=vcs_url, name=name)
if len(found) == 0:
print(NO_REPOS_FOR_TERM_MSG.format(name=name))
found_repos.extend(filter_repos(configs, dir=dir, vcs_url=vcs_url, name=name))
found_repos.extend(filter_repos(configs, path=path, vcs_url=vcs_url, name=name))

for repo in found_repos:
try:
Expand Down
18 changes: 9 additions & 9 deletions src/vcspull/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def extract_repos(
if "name" not in conf:
conf["name"] = repo

if "dir" not in conf:
conf["dir"] = expand_dir(
if "path" not in conf:
conf["path"] = expand_dir(
pathlib.Path(expand_dir(pathlib.Path(directory), cwd=cwd))
/ conf["name"],
cwd,
Expand Down Expand Up @@ -301,10 +301,10 @@ def detect_duplicate_repos(
dupes: list[ConfigDictTuple] = []

repo_dirs = {
pathlib.Path(repo["dir"]).parent / repo["name"]: repo for repo in config1
pathlib.Path(repo["path"]).parent / repo["name"]: repo for repo in config1
}
repo_dirs_2 = {
pathlib.Path(repo["dir"]).parent / repo["name"]: repo for repo in config2
pathlib.Path(repo["path"]).parent / repo["name"]: repo for repo in config2
}

for repo_dir, repo in repo_dirs.items():
Expand Down Expand Up @@ -347,19 +347,19 @@ def in_dir(

def filter_repos(
config: list["ConfigDict"],
dir: t.Union[pathlib.Path, t.Literal["*"], str, None] = None,
path: t.Union[pathlib.Path, t.Literal["*"], str, None] = None,
vcs_url: t.Union[str, None] = None,
name: t.Union[str, None] = None,
) -> list["ConfigDict"]:
"""Return a :py:obj:`list` list of repos from (expanded) config file.
dir, vcs_url and name all support fnmatch.
path, vcs_url and name all support fnmatch.
Parameters
----------
config : dict
the expanded repo config in :py:class:`dict` format.
dir : str, Optional
path : str, Optional
directory of checkout location, fnmatch pattern supported
vcs_url : str, Optional
url of vcs remote, fn match pattern supported
Expand All @@ -373,12 +373,12 @@ def filter_repos(
"""
repo_list: list["ConfigDict"] = []

if dir:
if path:
repo_list.extend(
[
r
for r in config
if fnmatch.fnmatch(str(pathlib.Path(r["dir"]).parent), str(dir))
if fnmatch.fnmatch(str(pathlib.Path(r["path"]).parent), str(path))
]
)

Expand Down
4 changes: 2 additions & 2 deletions src/vcspull/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RawConfigDict(t.TypedDict):

vcs: VCSLiteral
name: str
dir: StrPath
path: StrPath
url: str
remotes: GitSyncRemoteDict

Expand All @@ -26,7 +26,7 @@ class ConfigDict(TypedDict):

vcs: t.Optional[VCSLiteral]
name: str
dir: pathlib.Path
path: pathlib.Path
url: str
remotes: NotRequired[t.Optional[GitSyncRemoteDict]]
shell_command_after: NotRequired[t.Optional[list[str]]]
Expand Down
14 changes: 7 additions & 7 deletions tests/fixtures/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,32 @@
{
"vcs": "git",
"name": "linux",
"dir": pathlib.Path("/home/me/myproject/study/linux"),
"path": pathlib.Path("/home/me/myproject/study/linux"),
"url": "git+git://git.kernel.org/linux/torvalds/linux.git",
},
{
"vcs": "git",
"name": "freebsd",
"dir": pathlib.Path("/home/me/myproject/study/freebsd"),
"path": pathlib.Path("/home/me/myproject/study/freebsd"),
"url": "git+https://github.com/freebsd/freebsd.git",
},
{
"vcs": "git",
"name": "sphinx",
"dir": pathlib.Path("/home/me/myproject/study/sphinx"),
"path": pathlib.Path("/home/me/myproject/study/sphinx"),
"url": "hg+https://bitbucket.org/birkenfeld/sphinx",
},
{
"vcs": "git",
"name": "docutils",
"dir": pathlib.Path("/home/me/myproject/study/docutils"),
"path": pathlib.Path("/home/me/myproject/study/docutils"),
"url": "svn+http://svn.code.sf.net/p/docutils/code/trunk",
},
{
"vcs": "git",
"name": "kaptan",
"url": "[email protected]:tony/kaptan.git",
"dir": pathlib.Path("/home/me/myproject/github_projects/kaptan"),
"path": pathlib.Path("/home/me/myproject/github_projects/kaptan"),
"remotes": {
"upstream": GitRemote(
**{
Expand All @@ -87,14 +87,14 @@
{
"vcs": "git",
"name": ".vim",
"dir": pathlib.Path("/home/me/myproject/.vim"),
"path": pathlib.Path("/home/me/myproject/.vim"),
"url": "[email protected]:tony/vim-config.git",
"shell_command_after": ["ln -sf /home/me/.vim/.vimrc /home/me/.vimrc"],
},
{
"vcs": "git",
"name": ".tmux",
"dir": pathlib.Path("/home/me/myproject/.tmux"),
"path": pathlib.Path("/home/me/myproject/.tmux"),
"url": "[email protected]:tony/tmux-config.git",
"shell_command_after": ["ln -sf /home/me/.tmux/.tmux.conf /home/me/.tmux.conf"],
},
Expand Down
14 changes: 7 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def test_sync_cli_filter_non_existent(
config = {
"~/github_projects/": {
"my_git_project": {
"url": f"git+file://{git_repo.dir}",
"remotes": {"test_remote": f"git+file://{git_repo.dir}"},
"url": f"git+file://{git_repo.path}",
"remotes": {"test_remote": f"git+file://{git_repo.path}"},
},
}
}
Expand Down Expand Up @@ -219,11 +219,11 @@ def test_sync(
config = {
"~/github_projects/": {
"my_git_repo": {
"url": f"git+file://{git_repo.dir}",
"remotes": {"test_remote": f"git+file://{git_repo.dir}"},
"url": f"git+file://{git_repo.path}",
"remotes": {"test_remote": f"git+file://{git_repo.path}"},
},
"broken_repo": {
"url": f"git+file://{git_repo.dir}",
"url": f"git+file://{git_repo.path}",
"remotes": {"test_remote": "git+file://non-existent-remote"},
},
}
Expand Down Expand Up @@ -360,8 +360,8 @@ def test_sync_broken(
config = {
"~/github_projects/": {
"my_git_repo": {
"url": f"git+file://{git_repo.dir}",
"remotes": {"test_remote": f"git+file://{git_repo.dir}"},
"url": f"git+file://{git_repo.path}",
"remotes": {"test_remote": f"git+file://{git_repo.path}"},
},
"my_git_repo_not_found": {
"url": "git+file:///dev/null",
Expand Down
22 changes: 11 additions & 11 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LoadYAMLFn(t.Protocol):
def __call__(
self,
content: str,
dir: str = "randomdir",
path: str = "randomdir",
filename: str = "randomfilename.yaml",
) -> tuple[pathlib.Path, list[t.Union[t.Any, pathlib.Path]], list["ConfigDict"]]:
"""Callable function type signature for load_yaml pytest fixture."""
Expand All @@ -28,10 +28,10 @@ def load_yaml(tmp_path: pathlib.Path) -> LoadYAMLFn:
"""Return a yaml loading function that uses temporary directory path."""

def fn(
content: str, dir: str = "randomdir", filename: str = "randomfilename.yaml"
content: str, path: str = "randomdir", filename: str = "randomfilename.yaml"
) -> tuple[pathlib.Path, list[pathlib.Path], list["ConfigDict"]]:
"""Return vcspull configurations and write out config to temp directory."""
_dir = tmp_path / dir
_dir = tmp_path / path
_dir.mkdir()
_config = _dir / filename
_config.write_text(content, encoding="utf-8")
Expand All @@ -45,7 +45,7 @@ def fn(

def test_simple_format(load_yaml: LoadYAMLFn) -> None:
"""Test simple configuration YAML file for vcspull."""
dir, _, repos = load_yaml(
path, _, repos = load_yaml(
"""
vcspull:
libvcs: git+https://github.com/vcs-python/libvcs
Expand All @@ -55,24 +55,24 @@ def test_simple_format(load_yaml: LoadYAMLFn) -> None:
assert len(repos) == 1
repo = repos[0]

assert dir / "vcspull" == repo["dir"].parent
assert dir / "vcspull" / "libvcs" == repo["dir"]
assert path / "vcspull" == repo["path"].parent
assert path / "vcspull" / "libvcs" == repo["path"]


def test_relative_dir(load_yaml: LoadYAMLFn) -> None:
"""Test configuration files for vcspull support relative directories."""
dir, _, repos = load_yaml(
path, _, repos = load_yaml(
"""
./relativedir:
docutils: svn+http://svn.code.sf.net/p/docutils/code/trunk
"""
)

config_files = config.find_config_files(path=dir)
repos = config.load_configs(config_files, dir)
config_files = config.find_config_files(path=path)
repos = config.load_configs(config_files, path)

assert len(repos) == 1
repo = repos[0]

assert dir / "relativedir" == repo["dir"].parent
assert dir / "relativedir" / "docutils" == repo["dir"]
assert path / "relativedir" == repo["path"].parent
assert path / "relativedir" / "docutils" == repo["path"]
4 changes: 2 additions & 2 deletions tests/test_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ def test_expandenv_and_homevars() -> None:
config1_expanded = extract_repos(config1)
config2_expanded = extract_repos(config2)

paths = [r["dir"].parent for r in config1_expanded]
paths = [r["path"].parent for r in config1_expanded]
assert expand_dir(pathlib.Path("${HOME}/github_projects/")) in paths
assert expand_dir(pathlib.Path("~/study/")) in paths
assert expand_dir(pathlib.Path("~")) in paths

paths = [r["dir"].parent for r in config2_expanded]
paths = [r["path"].parent for r in config2_expanded]
assert expand_dir(pathlib.Path("${HOME}/github_projects/")) in paths
assert expand_dir(pathlib.Path("~/study/")) in paths

Expand Down
12 changes: 6 additions & 6 deletions tests/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def test_filter_dir() -> None:
"""`filter_repos` filter by dir."""
repo_list = filter_repos(fixtures.config_dict_expanded, dir="*github_project*")
repo_list = filter_repos(fixtures.config_dict_expanded, path="*github_project*")

assert len(repo_list) == 1
for r in repo_list:
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_vcs_url_scheme_to_object(tmp_path: pathlib.Path) -> None:
git_repo = create_project(
vcs="git",
url="git+git://git.myproject.org/MyProject.git@da39a3ee5e6b4b",
dir=str(tmp_path / "myproject1"),
path=str(tmp_path / "myproject1"),
)

# TODO cwd and name if duplicated should give an error
Expand All @@ -76,7 +76,7 @@ def test_vcs_url_scheme_to_object(tmp_path: pathlib.Path) -> None:
hg_repo = create_project(
vcs="hg",
url="hg+https://hg.myproject.org/MyProject#egg=MyProject",
dir=str(tmp_path / "myproject2"),
path=str(tmp_path / "myproject2"),
)

assert isinstance(hg_repo, HgSync)
Expand All @@ -85,7 +85,7 @@ def test_vcs_url_scheme_to_object(tmp_path: pathlib.Path) -> None:
svn_repo = create_project(
vcs="svn",
url="svn+svn://svn.myproject.org/svn/MyProject#egg=MyProject",
dir=str(tmp_path / "myproject3"),
path=str(tmp_path / "myproject3"),
)

assert isinstance(svn_repo, SvnSync)
Expand All @@ -101,11 +101,11 @@ def test_to_repo_objects(tmp_path: pathlib.Path) -> None:
assert isinstance(r, BaseSync)
assert r.repo_name
assert r.repo_name == repo_dict["name"]
assert r.dir.parent
assert r.path.parent
assert r.url
assert r.url == repo_dict["url"]

assert r.dir == r.dir / r.repo_name
assert r.path == r.path / r.repo_name

if hasattr(r, "remotes") and isinstance(r, GitSync):
assert isinstance(r.remotes, dict)
Expand Down
Loading

0 comments on commit 60c883e

Please sign in to comment.