Skip to content

Commit

Permalink
src/ansiblelint/prerun.py: split _install_galaxy_role
Browse files Browse the repository at this point in the history
Flake8 is complaining that the function is now too complex, so
split it into several ones.

Signed-off-by: Arnaud Patard <apatard@hupstream.com>
  • Loading branch information
apatard committed Mar 30, 2021
1 parent 407393e commit 1635649
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/ansiblelint/prerun.py
Expand Up @@ -6,7 +6,7 @@
import subprocess
import sys
from functools import lru_cache
from typing import List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple

from packaging import version

Expand Down Expand Up @@ -158,29 +158,47 @@ def prepare_environment() -> None:
_prepare_ansible_paths()


def _install_galaxy_role() -> None:
"""Detect standalone galaxy role and installs it."""
if not os.path.exists("meta/main.yml"):
return
yaml = yaml_from_file("meta/main.yml")
if 'galaxy_info' not in yaml:
return
role_name = yaml['galaxy_info'].get('role_name', None)
role_namespace = yaml['galaxy_info'].get('namespace', None)
def _get_galaxy_role_ns(galaxy_infos: Dict[str, Any]) -> str:
"""Compute role namespace from meta/main.yml, including trailing dot."""
role_namespace = galaxy_infos.get('namespace', None)
if not role_namespace:
role_namespace = yaml['galaxy_info'].get('author', None)
role_namespace = galaxy_infos.get('author', None)
# if there's a space in the name space, it's likely author name
# and not the galaxy login, so act as if there was no namespace
if re.match(r"^\w+ \w+", role_namespace):
role_namespace = ""
else:
role_namespace = f"{role_namespace}."
return role_namespace


def _get_galaxy_role_name(galaxy_infos: Dict[str, Any]) -> str:
"""Compute role name from meta/main.yml."""
return galaxy_infos.get('role_name', None)


def _get_role_fqrn(galaxy_infos: Dict[str, Any]) -> str:
"""Compute role fqrn."""
role_namespace = _get_galaxy_role_ns(galaxy_infos)
role_name = _get_galaxy_role_name(galaxy_infos)
if not role_name or os.path.exists(".git") or os.path.exists(".hg"):
role_name = pathlib.Path(".").absolute().name
role_name = re.sub(r'(ansible-|ansible-role-)', '', role_name)

return f"{role_namespace}{role_name}"


def _install_galaxy_role() -> None:
"""Detect standalone galaxy role and installs it."""
if not os.path.exists("meta/main.yml"):
return
yaml = yaml_from_file("meta/main.yml")
if 'galaxy_info' not in yaml:
return

fqrn = _get_role_fqrn(yaml['galaxy_info'])

if 'role-name' not in options.skip_list:
fqrn = f"{role_namespace}{role_name}"
if not re.match(r"[a-z0-9][a-z0-9_]+\.[a-z][a-z0-9_]+$", fqrn):
msg = (
"""\
Expand All @@ -206,7 +224,8 @@ def _install_galaxy_role() -> None:
else:
# when 'role-name' is in skip_list, we stick to plain role names
if 'role_name' in yaml['galaxy_info']:
role_name = yaml['galaxy_info'].get('role_name', None)
role_namespace = _get_galaxy_role_ns(yaml['galaxy_info'])
role_name = _get_galaxy_role_name(yaml['galaxy_info'])
fqrn = f"{role_namespace}{role_name}"
else:
fqrn = pathlib.Path(".").absolute().name
Expand Down

0 comments on commit 1635649

Please sign in to comment.