Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove ansible-core dependency #230

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ansible_risk_insight/annotators/ansible.builtin/unarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

from typing import List
from ansible_risk_insight.models import Annotation, RiskAnnotation, TaskCall, DefaultRiskType, InboundTransferDetail
from ansible_risk_insight.utils import parse_bool
from ansible_risk_insight.annotators.module_annotator_base import ModuleAnnotator, ModuleAnnotatorResult
from ansible.module_utils.parsing.convert_bool import boolean


class UnarchiveAnnotator(ModuleAnnotator):
Expand All @@ -34,12 +34,12 @@ def run(self, task: TaskCall) -> List[Annotation]:

if isinstance(remote_src.raw, str) or isinstance(remote_src.raw, bool):
try:
is_remote_src = boolean(remote_src.raw)
is_remote_src = parse_bool(remote_src.raw)
except Exception:
pass
if not is_remote_src and (isinstance(remote_src.templated, str) or isinstance(remote_src.templated, bool)):
try:
is_remote_src = boolean(remote_src.templated)
is_remote_src = parse_bool(remote_src.templated)
except Exception:
pass

Expand Down
4 changes: 2 additions & 2 deletions ansible_risk_insight/dependency_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import json
import subprocess
from pathlib import Path
from ansible import constants as C

import ansible_risk_insight.logger as logger
from .safe_glob import safe_glob
Expand All @@ -35,6 +34,7 @@
galaxy_yml = "galaxy.yml"
GALAXY_yml = "GALAXY.yml"
github_workflows_dir = ".github/workflows"
ansible_home = os.getenv("ANSIBLE_HOME", "~/.ansible")


def find_dependency(type, target, dependency_dir, use_ansible_path=False):
Expand Down Expand Up @@ -68,7 +68,7 @@ def find_dependency(type, target, dependency_dir, use_ansible_path=False):
dependencies["file"] = manifestjson

if use_ansible_path and dependencies["dependencies"]:
ansible_dir = Path(C.ANSIBLE_HOME).expanduser()
ansible_dir = Path(ansible_home).expanduser()
paths, metadata = search_ansible_dir(dependencies["dependencies"], str(ansible_dir))
if paths:
dependencies["paths"] = paths
Expand Down
4 changes: 2 additions & 2 deletions ansible_risk_insight/model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
except Exception:
# otherwise, use Python based loader
from yaml import SafeLoader as Loader
from ansible.module_utils.parsing.convert_bool import boolean

import ansible_risk_insight.logger as logger
from ansible_risk_insight.utils import parse_bool
from .safe_glob import safe_glob
from .models import (
ExecutableType,
Expand Down Expand Up @@ -1360,7 +1360,7 @@ def load_module(module_file_path, collection_name="", role_name="", basedir="",
arg_elements_type_str = arg_elements_type.__name__
required = None
try:
required = boolean(arg_spec.get("required", "false"))
required = parse_bool(arg_spec.get("required", "false"))
except Exception:
pass
arg = ModuleArgument(
Expand Down
4 changes: 2 additions & 2 deletions ansible_risk_insight/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import jsonpickle
from rapidfuzz.distance import Levenshtein
import ansible_risk_insight.yaml as ariyaml
from ansible.module_utils.parsing.convert_bool import boolean
from ansible_risk_insight.utils import parse_bool
from .keyutil import (
set_collection_key,
set_module_key,
Expand Down Expand Up @@ -1064,7 +1064,7 @@ def from_options(options: dict):
become = options.get("become", "")
enabled = False
try:
enabled = boolean(become)
enabled = parse_bool(become)
except Exception:
pass
user = options.get("become_user", "")
Expand Down
38 changes: 38 additions & 0 deletions ansible_risk_insight/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import hashlib
import yaml
import json
import codecs
from filelock import FileLock
from copy import deepcopy
from tabulate import tabulate
Expand All @@ -30,6 +31,11 @@
import ansible_risk_insight.logger as logger


bool_values_true = frozenset(("y", "yes", "on", "1", "true", "t", 1, 1.0, True))
bool_values_false = frozenset(("n", "no", "off", "0", "false", "f", 0, 0.0, False))
bool_values = bool_values_true.union(bool_values_false)


def lock_file(fpath, timeout=10):
if not fpath:
return
Expand Down Expand Up @@ -739,3 +745,35 @@ def recursive_copy_dict(src, dst):

def is_test_object(path: str):
return path.startswith("tests/integration/") or path.startswith("molecule/")


def parse_bool(value: any):
value_str = None
use_value_str = False
if isinstance(value, bool):
return value
elif isinstance(value, str):
value_str = value
use_value_str = True
elif isinstance(value, bytes):
surrogateescape_enabled = False
try:
codecs.lookup_error("surrogateescape")
surrogateescape_enabled = True
except Exception:
pass
errors = "surrogateescape" if surrogateescape_enabled else "strict"
value_str = value.decode("utf-8", errors)
use_value_str = True

if use_value_str and isinstance(value_str, str):
value_str = value_str.lower().strip()

target_value = value_str if use_value_str else value

if target_value in bool_values_true:
return True
elif target_value in bool_values_false:
return False
else:
raise TypeError(f'failed to parse the value "{value}" as a boolean.')
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies = [
"smmap",
"tabulate",
"requests",
"ansible-core",
"ruamel.yaml",
"filelock",
"rapidfuzz",
Expand Down
Loading