U
    Jh*                     @   s  d dl Z d dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
mZmZmZmZmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lm Z  e! Z"e#e$e%Z&ej'e(ee ee( e(e(dddZ)ddddZ*ej+ej,e-ej.e/ddd Z0edej1ej2e0ej3eedej4j5d6e
j7e' dddddedej8j5dddd ed!ej9j5d"d#dd"d$gd"d%ed&ej:dd gd'gd(Z;dS ))    N)AnyDictList)configcontractentitlementsevent_logger
exceptionsmessagesstatusutil)ProgressWrapper)ServiceWithDependencies_dependencies)_enabled_services)cli_util)ProArgumentProArgumentGroup
ProCommand)HelpCategory)CanDisableFailure)cfgserviceall_dependenciesenabled_service_namescalled_nameservice_titlec                 C   s   g }|D ]<}|j |ks|j |kr"q|jD ]}|j |kr(||j  q(q|D ]:}	t| |	}
tjj||
d}tj	|dsJt
j||
dqJd S )N)Zservice_being_disableddependent_service)msg)nameZ
depends_onappendr   Z	get_titler
   ZDEPENDENT_SERVICEformatr   Zprompt_for_confirmationr	   ZDependentServiceStopsDisable)r   r   r   r   r   r   Zdependent_servicessZrequirementr   Zdependent_service_titleZuser_msg r#   6/usr/lib/python3/dist-packages/uaclient/cli/disable.pyprompt_for_dependency_handling   s*    

 r%   T)update_statusc          	      C   s   | j }|dk	r|} |rt }nttj|d}| |\}}|st| j |dk	rt|t	r|j
dk	rt|j
j tj|j
j|j
j| jd nt| j |rtj|d |S )a  Perform the disable action on a named entitlement.

    :param entitlement_name: the name of the entitlement to enable
    :param cfg: the UAConfig to pass to the entitlement
    :param json_output: output should be json only

    @return: True on success, False otherwise
    N
assume_yes)Z	error_msgZ
error_coder   r   )enabled_variantr   r   CLIEnableDisableProgressdisableeventZservice_failedr   
isinstancer   messageinfor   errorZservice_processedr   )	Zentitlementr   json_outputr(   r&   variantprogressretreasonr#   r#   r$   perform_disable>   s.    

r7   zpro disablec                K   s~  g }g }g }g }t jddd}| jdk}| j}	t|}
| jrP|	rPtjdddt	| dg }t
||\}}d	d
 t|jD }t|j}d}|D ]}t
j||| jd}|j}|dk	r|}|	s:zt||j||||jd W nZ tjk
r8 } z8t| |
|j |
tjj|jd d}W Y qW 5 d}~X Y nX |rHt }nttj|	d}| |_z||\}}t j |d |sd}|!| |dk	rt"|t#r|j$dk	r|
|j$j |!d|j|j$j|j$jd n2|!| |% }|rd|d< |
tj&jdd W q tjk
r } zNd}|!| |
|j |
tj'j|jd |!d|j|j|j(|j)d W 5 d}~X Y qX q|r d}dd*t
j+|d d }d*t,j-|dddd}tj.dd*||d}|
|j |!dd|j|j(|j)d t/0|}|1  |rp|2  |2  |r2dnd |d!< ||d"< ||d#< ||d$< ||d%< t3t4j5|t6j7dd& |rzd'S d(S ))zbPerform the disable action on a list of entitlements.

    @return: 0 on success, 1 otherwise
    ZsuccessF)Z_schema_versionresultneeds_rebootjson--purge--assume-yes)Zoption1Zoption2r   c                 S   s   g | ]
}|j qS r#   )r   ).0r"   r#   r#   r$   
<listcomp>   s    z"action_disable.<locals>.<listcomp>T)r   r   purgeN)r   r   )titler'   r)   )typer   r/   message_coder9   zdisable operation)	operation)rA   r   r/   rB   additional_infozTry , .
P   )widthZbreak_long_wordsZbreak_on_hyphensr,   )rC   Zinvalid_serviceservice_msgsystemZfailurer8   processed_servicesfailed_serviceserrorswarnings)clsZ	sort_keysr      )8r   ZJSON_SCHEMA_VERSIONr!   r(   r   Z&create_interactive_only_print_functionr?   r	   ZInvalidOptionCombinationgetattrr   Zget_valid_entitlement_namesr   Zenabled_servicesr   servicesZentitlement_factoryr*   r%   r   r@   ZUbuntuProErrorLOGZ	exceptionr   r
   ZENABLE_FAILEDr   r+   Zcalculate_total_disable_stepsZtotal_stepsr,   r   r    r.   r   r/   Z_check_for_rebootZENABLE_REBOOT_REQUIRED_TMPLZDISABLE_FAILED_TMPLZmsg_coderD   joinvalid_servicestextwrapZwrapZInvalidServiceOpErrorr   ZUAContractClientZupdate_activity_tokensortprintr:   dumpsr   ZDatetimeAwareJSONEncoder)argsr   kwargsrL   rM   rN   rO   Zjson_responser2   r(   Zinteractive_only_printnamesZentitlements_foundZentitlements_not_foundr   r   r5   Zent_nameZentr3   er4   Zdisable_retr6   Zent_reboot_requiredZvalid_namesrJ   errZcontract_clientr#   r#   r$   action_disablel   s(   	

 








 	






r`   r,   r   rE   r)   )ZoptionsZstore+)helpactionnargsr<   )Zcommand
store_true)rb   rc   z--formatZcli)defaultr:   )rb   rc   choicesrf   r;   )Z	arguments)rb   Zdescriptionrc   Zhelp_categoryZargument_groups)<r:   ZloggingrW   typingr   r   r   Zuaclientr   r   r   r   r	   r
   r   r   Zuaclient.apir   Z+uaclient.api.u.pro.services.dependencies.v1r   r   Z-uaclient.api.u.pro.status.enabled_services.v1r   Zuaclient.clir   Zuaclient.cli.commandsr   r   r   Zuaclient.cli.parserr   Z(uaclient.entitlements.entitlement_statusr   Zget_event_loggerr-   Z	getLoggerZreplace_top_level_logger_name__name__rT   ZUAConfigstrr%   r7   Zverify_json_format_argsZassert_rootZassert_attachedZ&_raise_enable_disable_unattached_errorZassert_lock_filer`   ZCLI_ROOT_DISABLEZCLI_DISABLE_DESCZOTHERZCLI_DISABLE_SERVICEr!   rU   rV   ZCLI_ASSUME_YESZCLI_FORMAT_DESCZ	CLI_PURGEZdisable_commandr#   r#   r#   r$   <module>   s   (
 .
 6
