File: _debugging.py

package info (click to toggle)
ansible-core 2.19.0~beta6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,628 kB
  • sloc: python: 180,313; cs: 4,929; sh: 4,601; xml: 34; makefile: 21
file content (31 lines) | stat: -rw-r--r-- 1,037 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from __future__ import annotations

import argparse
import pathlib
import sys


def load_params() -> tuple[bytes, str]:
    """Load module arguments and profile when debugging an Ansible module."""
    parser = argparse.ArgumentParser(description="Directly invoke an Ansible module for debugging.")
    parser.add_argument('args', nargs='?', help='module args JSON (file path or inline string)')
    parser.add_argument('--profile', default='legacy', help='profile for JSON decoding/encoding of args/response')

    parsed_args = parser.parse_args()

    args: str | None = parsed_args.args
    profile: str = parsed_args.profile

    if args:
        if (args_path := pathlib.Path(args)).is_file():
            buffer = args_path.read_bytes()
        else:
            buffer = args.encode(errors='surrogateescape')
    else:
        if sys.stdin.isatty():
            sys.stderr.write('Waiting for Ansible module JSON on STDIN...\n')
            sys.stderr.flush()

        buffer = sys.stdin.buffer.read()

    return buffer, profile