Skip to content

Commit 80a8cb7

Browse files
committed
Refactor search_galaxy_paths to use pathlib
1 parent 6c27bb6 commit 80a8cb7

File tree

6 files changed

+42
-26
lines changed

6 files changed

+42
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,4 @@ ansible_collections
135135
# Generated by setuptools-scm
136136
src/ansible_compat/_version.py
137137
node_modules
138+
_readthedocs

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ markdown_extensions:
9191
custom_fences:
9292
- name: mermaid
9393
class: mermaid
94-
format: !!python/name:pymdownx.superfences.fence_code_format
94+
format: ""

readthedocs.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ submodules:
66

77
mkdocs:
88
fail_on_warning: true
9+
configuration: mkdocs.yml
910

1011
build:
1112
os: ubuntu-24.04
1213
tools:
1314
python: "3.11"
14-
15+
commands:
16+
- pip install --user tox
17+
- python3 -m tox -e docs
1518
python:
1619
install:
20+
- method: pip
21+
path: tox
1722
- method: pip
1823
path: .
1924
extra_requirements:

src/ansible_compat/runtime.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252

5353
_logger = logging.getLogger(__name__)
5454
# regex to extract the first version from a collection range specifier
55-
version_re = re.compile(":[>=<]*([^,]*)")
56-
namespace_re = re.compile("^[a-z][a-z0-9_]+$")
55+
version_re = re.compile(r":[>=<]*([^,]*)")
56+
namespace_re = re.compile(r"^[a-z][a-z0-9_]+$")
5757

5858

5959
class AnsibleWarning(Warning):
@@ -660,11 +660,10 @@ def prepare_environment( # noqa: C901
660660
if not install_local:
661661
return
662662

663-
for gpath in search_galaxy_paths(self.project_dir):
663+
for item in search_galaxy_paths(self.project_dir):
664664
# processing all found galaxy.yml files
665-
galaxy_path = Path(gpath)
666-
if galaxy_path.exists():
667-
data = yaml_from_file(galaxy_path)
665+
if item.exists():
666+
data = yaml_from_file(item)
668667
if isinstance(data, dict) and "dependencies" in data:
669668
for name, required_version in data["dependencies"].items():
670669
_logger.info(
@@ -685,7 +684,8 @@ def prepare_environment( # noqa: C901
685684
destination=destination,
686685
)
687686

688-
if (self.project_dir / "galaxy.yml").exists():
687+
galaxy_path = self.project_dir / "galaxy.yml"
688+
if (galaxy_path).exists():
689689
if destination:
690690
# while function can return None, that would not break the logic
691691
colpath = Path(
@@ -979,20 +979,27 @@ def _get_galaxy_role_name(galaxy_infos: dict[str, Any]) -> str:
979979
return result
980980

981981

982-
def search_galaxy_paths(search_dir: Path) -> list[str]:
983-
"""Search for galaxy paths (only one level deep)."""
984-
galaxy_paths: list[str] = []
985-
for file in [".", *os.listdir(search_dir)]:
982+
def search_galaxy_paths(search_dir: Path) -> list[Path]:
983+
"""Search for galaxy paths (only one level deep).
984+
985+
Returns:
986+
list[Path]: List of galaxy.yml found.
987+
"""
988+
galaxy_paths: list[Path] = []
989+
for item in [Path(), *search_dir.iterdir()]:
986990
# We ignore any folders that are not valid namespaces, just like
987991
# ansible galaxy does at this moment.
988-
if file != "." and not namespace_re.match(file):
992+
file_path = item.resolve()
993+
if file_path.is_file() and file_path.name == "galaxy.yml":
994+
galaxy_paths.append(file_path)
989995
continue
990-
file_path = search_dir / file / "galaxy.yml"
991-
if file_path.is_file():
992-
galaxy_paths.append(str(file_path))
996+
if file_path.is_dir() and namespace_re.match(file_path.name):
997+
file_path /= "galaxy.yml"
998+
if file_path.exists():
999+
galaxy_paths.append(file_path)
9931000
return galaxy_paths
9941001

9951002

9961003
def is_url(name: str) -> bool:
9971004
"""Return True if a dependency name looks like an URL."""
998-
return bool(re.match("^git[+@]", name))
1005+
return bool(re.match(r"^git[+@]", name))

test/test_runtime.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -937,25 +937,25 @@ def test_runtime_plugins(runtime: Runtime) -> None:
937937
("path", "result"),
938938
(
939939
pytest.param(
940-
"test/assets/galaxy_paths",
941-
["test/assets/galaxy_paths/foo/galaxy.yml"],
940+
Path("test/assets/galaxy_paths"),
941+
[Path("test/assets/galaxy_paths/foo/galaxy.yml").resolve()],
942942
id="1",
943943
),
944944
pytest.param(
945-
"test/collections",
945+
Path("test/collections"),
946946
[], # should find nothing because these folders are not valid namespaces
947947
id="2",
948948
),
949949
pytest.param(
950-
"test/assets/galaxy_paths/foo",
951-
["test/assets/galaxy_paths/foo/galaxy.yml"],
950+
Path("test/assets/galaxy_paths/foo"),
951+
[Path("test/assets/galaxy_paths/foo/galaxy.yml").resolve()],
952952
id="3",
953953
),
954954
),
955955
)
956-
def test_galaxy_path(path: str, result: list[str]) -> None:
956+
def test_galaxy_path(path: Path, result: list[Path]) -> None:
957957
"""Check behavior of galaxy path search."""
958-
assert search_galaxy_paths(Path(path)) == result
958+
assert search_galaxy_paths(path) == result
959959

960960

961961
@pytest.mark.parametrize(

tox.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ commands =
170170
[testenv:docs]
171171
description = Build docs
172172
commands =
173-
mkdocs {posargs:build} --strict
173+
mkdocs {posargs:build --strict --site-dir=_readthedocs/html/}
174+
setenv =
175+
# https://squidfunk.github.io/mkdocs-material/plugins/requirements/image-processing/#troubleshooting
176+
DYLD_FALLBACK_LIBRARY_PATH = /opt/homebrew/lib:{env:LD_LIBRARY_PATH}
174177
extras = docs
175178
passenv = *
176179

0 commit comments

Comments
 (0)