HEX
Server: LiteSpeed
System: Linux shams.tasjeel.ae 5.14.0-611.5.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 08:09:09 EST 2025 x86_64
User: infowars (1469)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: //usr/lib/python3.9/site-packages/ipapython/__pycache__/admintool.cpython-39.pyc
a

�N(i6�@s�dZddlZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZdZdZ
dZe�e�Zd	d
�ZGdd�de�ZGd
d�d�ZdS)z�A common framework for command-line admin tools, e.g. install scripts

Handles common operations like option parsing and logging
�N)�osinfo)�version)�config)�standard_logging_setup��cCs
ddl}ddl}z�|�|j�d��}|jdur4WdS|�|j��|d�}t	|�d}g}dt
|�vrn|��}ndt
|�vr�|j}|D]h}t
|dd	�r�t
||j�}	td|�D]@}
|�||
|j�j�d
�}||	kr�|�||
td�t	|	��q�q�Wnt�yYn0dS)a�Takes option parser and generated options and scrubs sensitive arguments
    from the global program arguments. Note that this only works for GNU GLIBC
    as Python has no generic way to get access to the original argv values to
    modify them in place.

    The code assumes Python behavior, e.g. there are two additional args in the
    list (/path/to/python -I ...) than what's passed as 'argv' here.
    rN�cZ_dl_argvr�_get_all_options�_actionsZ	sensitiveFzutf-8�X)�ctypesZctypes.utilZCDLL�utilZfind_library�_nameZPOINTERZc_voidpZin_dll�len�dirr	r
�getattr�dest�range�castZc_char_p�value�decodeZmemset�ord�	Exception)�
option_parser�options�argvrZ_cZ_argvZ_argcZall_options�opt�v�i�vi�r �7/usr/lib/python3.9/site-packages/ipapython/admintool.py�admin_cleanup_global_argv*s4	


��"r"cs.eZdZdZd�fdd�	Zedd��Z�ZS)	�ScriptErrorzBAn exception that records an error message and a return value
    �rcs&|durd}tt|��|�||_dS)Nr$)�superr#�__init__�rval)�self�msgr'��	__class__r r!r&TszScriptError.__init__cCst|�S�N)�str�r(r r r!r)ZszScriptError.msg)r$r)�__name__�
__module__�__qualname__�__doc__r&�propertyr)�
__classcell__r r r*r!r#Qsr#c@s�eZdZdZdZdZdZdZe�Z	e
dd��Ze
d#dd��Ze
dd	��Z
e
d
d��Ze
dd
��Zdd�Zdd�Zd$dd�Zdd�Zd%dd�Zd&dd�Zdd�Zdd�Zdd �Zd!d"�ZdS)'�	AdminToola~Base class for command-line admin tools

    To run the tool, call the main() classmethod with a list of command-line
    arguments.
    Alternatively, call run_cli() to run with command-line arguments in
    sys.argv, and call sys.exit() with the return value.

    Some commands actually represent multiple related tools, e.g.
    ``ipa-server-install`` and ``ipa-server-install --uninstall`` would be
    represented by separate classes. Only their options are the same.

    To handle this, AdminTool provides classmethods for option parsing
    and selecting the appropriate command class.

    A class-wide option parser is made by calling add_options.
    The options are then parsed into options and arguments, and
    get_command_class is called with those to retrieve the class.
    That class is then instantiated and run.

    Running consists of a few steps:
    - validating options or the environment (validate_options)
    - setting up logging (setup_logging)
    - running the actual command (run)

    Any unhandled exceptions are handled in handle_error.
    And at the end, either log_success or log_failure is called.

    Class attributes to define in subclasses:
    command_name - shown in logs
    log_file_name - if None, logging is to stderr only
    usage - text shown in help
    description - text shown in help

    See the setup_logging method for more info on logging.
    NcCs0tjtj|jt��|jd�}||_|�|�dS)zACreate an option parser shared across all instances of this class)r�usage�	formatter�descriptionN)	rZIPAOptionParserrZVERSIONr6ZIPAFormatterr8r�add_options)�cls�parserr r r!�make_parser�s
�zAdminTool.make_parserFcCstt�|d�}|jddddddd�|r<|jd	d
ddddd�|jdd
ddddd�|jdddddd�|�|�dS)z�Add command-specific options to the option parser

        :param parser: The parser to add options to
        :param debug_option: Add a --debug option as an alias to --verbose
        zLogging and output optionsz-vz	--verbose�verboseF�
store_truezprint debugging information)r�default�action�helpz-dz--debugz alias for --verbose (deprecated)z-qz--quiet�quietzoutput only errorsz
--log-file�log_fileN�FILEzlog to the given file)rr?�metavarrA)rZOptionGroupZ
add_optionZadd_option_group)r:r;Zdebug_option�groupr r r!r9�s���
�zAdminTool.add_optionscCst�|�tj��dS)zKRun this command with sys.argv, exit process with the return value
        N)�sys�exit�mainr)r:r r r!�run_cli�szAdminTool.run_clicCsb||jvr|��|j|j|<|j�|dd��\}}t|j||�|�||�}|||�}|��S)z�The main entry point

        Parses command-line arguments, selects the actual command class to use
        based on them, and runs that command.

        :param argv: Command-line arguments.
        :return: Command exit code
        rN)�_option_parsersr<r�
parse_argsr"�get_command_class�execute)r:rr�argsZ
command_classZcommandr r r!rI�s


zAdminTool.maincCs|Sr,r )r:rrOr r r!rM�szAdminTool.get_command_classcCs$||_||_d|_|j�|�|_dS)NF)rrO�log_file_initializedrZ
get_safe_opts�safe_options)r(rrOr r r!r&�szAdminTool.__init__c
Cs�|jdd�d}z$|��|��|��|��}Wn�ty�}zjt|t�rd|jrd|j|krd|j}t	�
�d}|�|�\}}|r�|�||||�|WYd}~SWYd}~n
d}~00|�
�|S)z�Do everything needed after options are parsed

        This includes validating options, setting up logging, doing the
        actual work, and handling the result.
        T)�no_filerrN)�_setup_logging�validate_options�ask_for_options�
setup_logging�run�
BaseException�
isinstancer#r'rG�exc_info�handle_error�log_failure�log_success)r(�return_value�	exception�	traceback�
error_messager r r!rN�s(

�&zAdminTool.executecCs<|r t��dkr td|jd��|jjr8|jjr8td��dS)z�Validate self.options

        It's also possible to compute and store information that will be
        useful later, but no changes to the system should be made here.
        rzMust be root to run %srz8The --quiet and --verbose options are mutually exclusiveN)�os�getegidr#�command_namerr=rB)r(Z
needs_rootr r r!rT�s�zAdminTool.validate_optionscCsdS)a*Ask for missing options interactively

        Similar to validate_options. This is separate method because we want
        any validation errors to abort the script before bothering the user
        with prompts.

        Any options that might be asked for should also be validated here.
        Nr r.r r r!rU�szAdminTool.ask_for_options�wcCsVt��}|jD]*}t|tj�r|jtjur|�|�q:q|j	|d�|j
rRd|_dS)a%Set up logging

        :param _to_file: Setting this to false will disable logging to file.
            For internal use.

        If the --log-file option was given or if a filename is in
        self.log_file_name, the tool will log to that file. In this case,
        all messages are logged.

        What is logged to the console depends on command-line options:
        the default is INFO; --quiet sets ERROR; --verbose sets DEBUG.

        Rules of thumb for logging levels:
        - CRITICAL for fatal errors
        - ERROR for critical things that the admin must see, even with --quiet
        - WARNING for things that need to stand out in the log
        - INFO to display normal messages
        - DEBUG to spam about everything the program does
        - a plain print for things that should not be log (for example,
            interactive prompting)

        To log, use a module-level logger.

        Logging to file is only set up after option validation and prompting;
        before that, all output will go to the console only.
        )�
log_file_modeTN)�logging�	getLogger�handlersrYZ
StreamHandler�streamrG�stderrZ
removeHandlerrS�
log_file_namerP)r(rfZroot_loggerZhandlerr r r!rV�s

�
zAdminTool.setup_loggingcCs�|r
d}n|jjr"|jj}||_n|j}|jjr>d}d}d}nd}d}|jjrTd}nd}t|||||d�|r|t�d|�n|s�t�d�dS)Nz$%(name)s: %(levelname)s: %(message)sTz%(message)sF)�console_format�filemode�debugr=z
Logging to %szNot logging to a file)rrCrlr=rBr�loggerro)r(rfrRrlrmr=ror r r!rS%s.�zAdminTool._setup_loggingcCsPt|t�r|j|jfSt|t�rDt|jt�r6d|jfSt|j�dfSt|�dfS)zMGiven an exception, return a message (or None) and process exit code
        Nr)rYr#r)r'�
SystemExit�code�intr-)r(r_r r r!r[As


zAdminTool.handle_errorcCsHt�d|j|j|j�t�dtj�t�dtj�t�dtj	tj�dS)aNActual running of the command

        This is where the hard work is done. The base implementation logs
        the invocation of the command.

        If this method returns (i.e. doesn't raise an exception), the tool is
        assumed to have run successfully, and the return value is used as the
        SystemExit code.
        z0%s was invoked with arguments %s and options: %szIPA version %szIPA platform %szIPA os-release %s %sN)
rprordrOrQrZVENDOR_VERSIONr�platform�namer.r r r!rWMs
�z
AdminTool.runcCs�t�dd�t�|���t�d|jt|�j|�|r@t�d|�|dkrLdSd|j}|j	rr|t
krr|d|j7}t�d|�dS)Nz%sr$z(The %s command failed, exception: %s: %srzThe %s command failed.z See %s for more information)rpro�joinr`�	format_tbrd�typer/�errorrP�SERVER_NOT_CONFIGUREDrl)r(rar^r_Z	backtrace�messager r r!r\]s�
zAdminTool.log_failurecCst�d|j�dS)NzThe %s command was successful)rp�infordr.r r r!r]mszAdminTool.log_success)F)F)re)reF)r/r0r1r2rdrlr6r8�dictrK�classmethodr<r9rJrIrMr&rNrTrUrVrSr[rWr\r]r r r r!r5_s4#






&
r5)r2rgrGrbr`Zipaplatform.osinforZ	ipapythonrrZipapython.ipa_log_managerr�SUCCESSZSERVER_INSTALL_ERRORrzrhr/rpr"rr#r5r r r r!�<module>s
'